budibase/packages/standard-components/src/Table.svelte

78 lines
1.7 KiB
Svelte
Raw Normal View History

<script>
2020-02-03 10:50:30 +01:00
export let columns = []
export let data = ""
export let tableClass = ""
export let theadClass = ""
export let tbodyClass = ""
export let trClass = ""
export let thClass = ""
export let onRowClick
2020-02-03 10:50:30 +01:00
export let _bb
2019-10-18 18:32:03 +02:00
2020-02-03 10:50:30 +01:00
const rowClickHandler = row => () => {
2020-09-10 22:11:05 +02:00
// call currently only accepts one argument, so passing row does nothing
// however, we do not expose this event anyway. I am leaving this
// in for the future, as can and probably should hande this
_bb.call("onRowClick", row)
2020-02-03 10:50:30 +01:00
}
2019-10-18 18:32:03 +02:00
2020-02-03 10:50:30 +01:00
const cellValue = (colIndex, row) => {
const val = _bb.getStateOrValue(_bb.props.columns[colIndex].value, row)
return val
}
</script>
2020-02-03 10:50:30 +01:00
<table class={tableClass}>
<thead class={theadClass}>
<tr class={trClass}>
{#each columns as col}
<th class={thClass}>{col.title}</th>
{/each}
</tr>
</thead>
<tbody class={tbodyClass}>
{#if data}
{#each data as row}
<tr class={trClass} on:click={rowClickHandler(row)}>
{#each columns as col, index}
2019-10-18 18:32:03 +02:00
<th class={thClass}>{cellValue(index, row)}</th>
2020-02-03 10:50:30 +01:00
{/each}
</tr>
2020-02-03 10:50:30 +01:00
{/each}
{/if}
</tbody>
</table>
<style>
2020-02-03 10:50:30 +01:00
.table-default {
width: 100%;
margin-bottom: 1rem;
color: #212529;
border-collapse: collapse;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.table-default .thead-default .th-default {
vertical-align: bottom;
border-bottom: 2px solid #dee2e6;
font-weight: bold;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.table-default .th-default {
padding: 0.75rem;
vertical-align: top;
border-top: 1px solid #dee2e6;
font-weight: normal;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.th-default {
text-align: inherit;
2020-02-03 10:50:30 +01:00
}
2020-02-03 10:50:30 +01:00
.table-default .tbody-default .tr-default:hover {
color: #212529;
2020-02-03 10:50:30 +01:00
background-color: rgba(0, 0, 0, 0.075);
cursor: pointer;
2020-02-03 10:50:30 +01:00
}
</style>