如何在 Svelte 代码中添加自定义函数调用?例如。在 DataTableTest.svelte 中,我想添加 cellFormatter 函数并使其自动调用并在 . 以下是代码:
ABC.svelte
import DataTableTest from "./DataTableTest.svelte";
let columns = [
{
label: "ABC",
property: "abc"
},
{
label: "Items",
property: "items"
},
{
label: "cellFormatter",
formatter: function(rowIndex, rowData) {
return "<div>" + rowData[rowIndex] + "</div>";
}
}
];
let data = [
{
"abc": "dsaaads",
"items": "dsadsads",
}
</script>
<DataTableTest title="Test" {data} {columns} />
DataTableTest.svelte
<script>
export let title;
export let data;
export let columns = [];
</script>
{title}
<table>
{#if columns}
<tr>
{#each columns as c}
<td>{c.label}</td>
{/each}
</tr>
{/if}
{#if data}
<tbody>
{#each data as d, i}
<tr>
{#each columns as c}
{#if c.formatter}
<td on:load=c.formatter(i, d)></td>
{:else}
<td>
{@html d[c.property] ? d[c.property] : ''}
</td>
{/if}
{/each}
</tr>
{/each}
</tbody>
{/if}
</table>
我试了一下
<td on:load=c.formatter(i, d)></td>
但这不起作用?有人能告诉我在这里怎么做吗?