【发布时间】:2022-01-22 16:06:43
【问题描述】:
我有一个表格小部件,它是一个网格堆栈项。该表是使用 svelte 作为组件制作的。下拉项目和表格内容的数据从另一个打字稿文件(使用故事书故事文件执行此操作)传递到此组件。最初,绘制下拉项目,然后绘制表格数据。我想在更改下拉项时更改表格行数据。 Image for table widget
下拉列表和表格数据的代码如下所示
<script lang="ts">
let selected
// passed from a stories.ts file as JSON string
export let dropdownitems: any
export let tableData
const handlechange = (item) => {
// expected code for changing table row content
}
<script>
<div class="content">
<div class="select">
<select bind:value={selected} on:change={() => handlechange(selected)}>
{#each JSON.parse(dropdownitems) as item}
<option value={item.id}>
{item.name}
</option>
{/each}
</select>
</div>
<table>
<tr style="background:{color}">
{#each tableData[0].title as item}
<th>{item.label}</th>
{/each}
</tr>
{#each tableData[0].content as data}
<tr style="background:{data.color}">
{#each data.rowcont as datacont}
<td>{datacont}</td>
{/each}
</tr>
{/each}
</table>
</div>
示例 tableData 对象
tableData = [
{
title: [{ label: 'Subgroup' }, { label: '# of Students' }, { label: '% of
population' }],
content: [
{ color: '#e8f0f6', rowcont: ['504', '3732', '5.67'] },
{ color: '#fff', rowcont: ['ELL', '4164', '6.32'] },
{ color: '#e8f0f6', rowcont: ['ESE', '14768', '22.48'] },
{ color: '#fff', rowcont: ['FRL', '32776', '49.75'] },
],
},
],
DropdownItems = [
{
id: 0,
name: 'Calculate based on incident location'
},
{
id: 1,
name: 'Calculate data based on current enrollment'
},
],
选择下拉列表中的第二项时,数据要显示在表格行中
Content =[
{
id: 0,
data: [
{ color: '#e8f0f6', rowcont: ['434', '3732', '5.67'] },
{ color: '#fff', rowcont: ['ELL', '4164', '6.32'] },
{ color: '#e8f0f6', rowcont: ['ESE', '14768', '22.48'] },
{ color: '#fff', rowcont: ['FRL', '32776', '49.75'] },]
},
]
【问题讨论】:
标签: typescript svelte storybook gridstack