muonw / muonw-powertable

▦ PowerTable is a Svelte component that turns JSON data into an interactive HTML table. Inspired by DataTables. Powered by Svelte.
https://muonw.github.io/muonw-powertable/
Other
218 stars 13 forks source link

Cell formatting? #6

Closed ClaytonFarr closed 2 years ago

ClaytonFarr commented 2 years ago

@muonw thanks for sharing all of your work on this.

It's a great option for Svelte data tables with UI options baked-in.

One limitation I'm hitting is sorting out how to format cell contents - e.g. convert a url string into a clickable link.

Is this something that's possible today?

muonw-public commented 2 years ago

Yes, it's possible. You can find an example here: https://muonw.github.io/powertable/examples/example2

Basically, you need to use the customParse property of the userFunctions as demonstrated below.

let ptData = [
    {"id": 1, "url": "https://www.example.com"},
    {"id": 2, "url": "https://google.com"}
];

let ptInstructs = [];

let ptOptions = {
    userFunctions: {
        customParse: url2link
    }
}

function url2link(pageData) {
    pageData.forEach(row => {
        row['url'] = `<a href="${row['url']}">${row['url']}</a>`;
    });

    return pageData;
}

<PowerTable {ptInstructs} {ptOptions} {ptData} />
ClaytonFarr commented 2 years ago

Perfect, that's great to see. I must have missed it. Thank again

ClaytonFarr commented 2 years ago

@muonw Is it possible to pass multiple functions to customParse or should all formatting be contained in one? Not able to sort out right syntax for multiple functions if that does exist.

muonw-public commented 2 years ago

You can put all the operations in a single function, or you can define one function and call any other functions from within that like this:

function myParsingFunction(pageData) {
    pageData = myLinkConverter(pageData);
    pageData = myOtherFunction(pageData);
    //...
    return pageData;
}
ClaytonFarr commented 2 years ago

Makes sense, thanks again @muonw