Lightweight CRUD table written using Svelte.
No dependencies, reasonable functionality and no bloat.
MIT Licence.
In the interests of no bloat, several things are not implemented -
Much of the above could be implemented by the parent.
The data types date, color and email are edited using <input type="xxx">. text & html use
REPL is here
<script>
import Table from './Table.svelte';
const table_config = {
// remove any of these to prevent that user action
options: ['CREATE', 'EDIT', 'DELETE','DBLCLICKEDIT'],
row_settings : {
firstRow: 0, // pagination is implemented by parent
lastRow:9999,
},
columns_setting: [
{name: 'name', show: true, resizable:true, edit: true, width: 150, type:'text', align:'left', description:'Name'},
{name: 'role', show: true, resizable:true, edit: true, width: 350, type:'html', align:'left', description: 'Role'},
{name: 'email', show: true, resizable:true, edit: true, width: 350, type:'email', align:'right', description: 'Email'},
{name: 'favCol', show: true, resizable:true, edit: true, width: 150, type: 'select', options:[], align:'center', description: 'Fav Colour'},
{name: 'color', show: true, resizable:true, edit: true, width: 100, type: 'color', align:'center', description: 'Assigned Colour', },
{name: 'dt', show: true, resizable:true, edit: true, width: 140, type: 'date', align:'center', description: 'Date', }
],
};
table_config.columns_setting[3].options = [
{value:'r' , text:'Rouge'},
{value:'g' , text:'Green'},
{value:'b' , text:'Bleu'},
{value:'y' , text:'Yuck'}
];
let myData = [
{name:"Name 43 is Fred",email:"fred33@myco.com",role:"DevOps Engineer",favCol:"b",color:"#c99f84",dt:"2009-08-29"},
{name:"Name 44 is Fred",email:"fred44@myco.com",role:"DevOps Engineer",favCol:"g",color:"#a0e3be",dt:"2011-09-27"},
{name:"Name 45 is Fred",email:"fred@myco.com",role:"Application Developer",favCol:"r",color:"#c89aab",dt:"2000-05-10"},
{name:"Name 46 is Fred",email:"fred@myco.com",role:"Data Entry",favCol:"g",color:"#89f0eb",dt:"1997-05-07"},
{name:"Name 47 is Fred",email:"fred@myco.com",role:"UX Designer & UI Developer",favCol:"r",color:"#d7beb9",dt:"2001-08-05"},
{name:"Name 48 is Fred",email:"fred@myco.com",role:"Web Developer",favCol:"y",color:"#c6a2a6",dt:"2003-08-13"},
{name:"Name 49 is Fred",email:"fred@myco.com",role:"SQL Developer",favCol:"y",color:"#b4b6de",dt:"1999-02-27"},
{name:"Name 50 is Fred",email:"fred@myco.com",role:"Web Designer",favCol:"b",color:"#d1d2ad",dt:"1997-07-27"},
{name:"Name 51 is Fred",email:"fred@myco.com",role:"Application Developer",favCol:"r",color:"#febb87",dt:"2001-11-19"},
];
function onStartEdit(e) {console.log('start edit',e.detail);}
function onEndEdit(e) {console.log('end edit ',e.detail);}
function onAddRow() {console.log('addrow ',e.detail);}
function requestdeleterow(e) {console.log('request delete row ',e.detail);}
</script>
<Table
config={table_config}
bind:data={myData}
on:startedit={onStartEdit}
on:endedit={onEndEdit}
on:addrow={onAddRow}
on:requestdeleterow={(e)=>requestdeleterow(e)}
/>
Events dispatched -
The affected row number is passed in event.detail.
See example above.
A column can have type = 'select'. This will allow the values to be selected from a dropdown list. The content and values of this list are specified in columns_setting[n].options[].
The format is -
options = [
{value:'r' , text:'Red'},
{value:'g' , text:'Green'},
{value:'b' , text:'Blue'},
{value:'y' , text:'Yuck'},
];
Raw HTML may be displayed & edited using columns_setting[n].type = 'html'.
Sanitise it first or don't allow edit or don't use type='html'! Or even better, delete that line of code from Table.svelte.