Skip to content

Commit bdf4612

Browse files
committed
crud-table sort and displayName
1 parent 67e6ab1 commit bdf4612

File tree

8 files changed

+124
-104
lines changed

8 files changed

+124
-104
lines changed

README.md

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
- or Svelte-component: `import SvelteGenericCrudTable from 'svelte-generic-crud-table'`
44

55
A self-containing sortable table component with inline edit option.
6-
See `<table-pager>` with integrated paginator for pagination.
7-
[table-pager](https://www.npmjs.com/package/svelte-generic-table-pager/ "CrudTable with paginator Example")
86

97
Allows CRUD-operations for Object-Arrays.
108

@@ -20,10 +18,6 @@ npm install svelte-generic-crud-table --save-dev
2018
```
2119

2220

23-
### State (master):
24-
[![Build Status](https://travis-ci.com/ivosdc/svelte-generic-crud-table.svg?branch=master)](https://travis-ci.com/ivosdc/svelte-generic-crud-table)
25-
[![Coverage Status](https://coveralls.io/repos/github/ivosdc/svelte-generic-crud-table/badge.svg?branch=master)](https://coveralls.io/github/ivosdc/svelte-generic-crud-table?branch=master)
26-
2721
# Usage
2822
Use the svelte-generic-crud-table in your component to show and, if you like, edit,update and delete it's content.
2923
Just include the table as seen in the example below.
@@ -39,15 +33,16 @@ Only wanted keys of your source array have to be mapped by columns_settings *nam
3933
options: ['CREATE', 'EDIT', 'DELETE', 'DETAILS'],
4034
columns_setting: [
4135
{name: 'id', show: false, edit: true, width: '0px'},
42-
{name: 'job', show: true, edit: true, width: '150px', description: 'The job'},
43-
{name: 'name', show: true, edit: true, width: '150px', tooltip: true},
36+
{name: 'job', displayName: 'Top-Jobs', show: true, edit: true, width: '150px', description: 'The job'},
37+
{name: 'name', displayName: 'Account-ID', show: true, edit: true, width: '150px', tooltip: true},
4438
{name: 'private', show: true, edit: false, width: '200px', description: 'your things', tooltip: true},
4539
{name: 'html', show: true, edit: true, width: '500px', type: 'html', description: 'You can use HTML', tooltip: true}
4640
],
4741
details_text: 'detail' // replace the standard icon with an text-link
4842
}
4943
```
5044
- <b>*name*</b>: the key from your data-array. This is used as column name.
45+
- *displayName*: An alternative column header if *name* is not matching the needs.
5146
- *show*: true/false; Should this column displayed? (optional, default: false)
5247
- *edit*: true/false; Set this field editable or not. (optional, default: false)
5348
- *width*: px/em; set the field width. (optional, default: 100px)
@@ -93,8 +88,6 @@ Only wanted keys of your source array have to be mapped by columns_settings *nam
9388
import {onMount} from 'svelte';
9489
import {goto} from "@sapper/app";
9590
96-
const sortStore = [];
97-
9891
let myData = [];
9992
10093
onMount(reload);
@@ -129,30 +122,7 @@ Only wanted keys of your source array have to be mapped by columns_settings *nam
129122
goto('/project/' + event.detail.body.id);
130123
}
131124
132-
function handleSort(event) {
133-
const column = event.detail.column;
134-
if (sortStore[column] === undefined || sortStore[column] === 'DESC') {
135-
sortStore[column] = 'ASC';
136-
} else {
137-
sortStore[column] = 'DESC';
138-
}
139-
140-
const tableSort = (a, b) => {
141-
var keyA = a[column];
142-
var keyB = b[column];
143-
if (sortStore[column] === 'ASC') {
144-
if (keyA < keyB) return -1;
145-
if (keyA > keyB) return 1;
146-
} else {
147-
if (keyA < keyB) return 1;
148-
if (keyA > keyB) return -1;
149-
}
150-
return 0;
151-
};
152-
153-
myData = myData.sort(tableSort);
154-
}
155-
125+
156126
const table_config = {
157127
name: 'Awesome',
158128
options: ['CREATE', 'EDIT', 'DELETE', 'DETAILS'],
@@ -173,7 +143,6 @@ Only wanted keys of your source array have to be mapped by columns_settings *nam
173143
on:update={handleUpdate}
174144
on:create={handleCreate}
175145
on:details={handleDetail}
176-
on:sort={handleSort}
177146
table_config={table_config}
178147
table_data={myData}/>
179148
</main>

dist/crud-table-1-config-html.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ let table_config = {
1414

1515
// table-1
1616
let genericCrudTable = document.getElementById('crud-table-1');
17-
const sortStore = [];
1817

1918
genericCrudTable.setAttribute('shadowed', 'true');
2019
genericCrudTable.setAttribute('table_config', JSON.stringify(table_config));
@@ -55,28 +54,3 @@ genericCrudTable.addEventListener('delete', (e) => {
5554
genericCrudTable.setAttribute('table_data', JSON.stringify(myData));
5655
});
5756

58-
genericCrudTable.addEventListener('sort', (e) => {
59-
console.log('sort: ' + e.detail.column);
60-
const column = e.detail.column;
61-
if (sortStore[column] === undefined || sortStore[column] === 'DESC') {
62-
sortStore[column] = 'ASC';
63-
} else {
64-
sortStore[column] = 'DESC';
65-
}
66-
67-
const tableSort = (a, b) => {
68-
var keyA = a[column];
69-
var keyB = b[column];
70-
if (sortStore[column] === 'ASC') {
71-
if (keyA < keyB) return -1;
72-
if (keyA > keyB) return 1;
73-
} else {
74-
if (keyA < keyB) return 1;
75-
if (keyA > keyB) return -1;
76-
}
77-
return 0;
78-
};
79-
80-
myData = myData.sort(tableSort);
81-
genericCrudTable.setAttribute('table_data', JSON.stringify(myData));
82-
});

dist/crud-table-2-config-html.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ let table_config2 = {
1414

1515
// table-2
1616
let genericCrudTable2 = document.getElementById('crud-table-2');
17-
const sortStore2 = [];
1817

1918
genericCrudTable2.setAttribute('shadowed', 'true');
2019
genericCrudTable2.setAttribute('table_config', JSON.stringify(table_config2));
@@ -54,29 +53,3 @@ genericCrudTable2.addEventListener('delete', (e) => {
5453
}
5554
genericCrudTable2.setAttribute('table_data', JSON.stringify(myData2));
5655
});
57-
58-
genericCrudTable2.addEventListener('sort', (e) => {
59-
console.log('sort: ' + e.detail.column);
60-
const column = e.detail.column;
61-
if (sortStore2[column] === undefined || sortStore2[column] === 'DESC') {
62-
sortStore2[column] = 'ASC';
63-
} else {
64-
sortStore2[column] = 'DESC';
65-
}
66-
67-
const tableSort = (a, b) => {
68-
var keyA = a[column];
69-
var keyB = b[column];
70-
if (sortStore2[column] === 'ASC') {
71-
if (keyA < keyB) return -1;
72-
if (keyA > keyB) return 1;
73-
} else {
74-
if (keyA < keyB) return 1;
75-
if (keyA > keyB) return -1;
76-
}
77-
return 0;
78-
};
79-
80-
myData2 = myData2.sort(tableSort);
81-
genericCrudTable2.setAttribute('table_data', JSON.stringify(myData2));
82-
});

dist/global.css

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pre {
3939
}
4040

4141

42+
/* crud-table from here */
4243
:root {
4344
--lightgrey1: #f4f4f4;
4445
--lightgrey2: #efefef;
@@ -53,7 +54,7 @@ pre {
5354
--button2: #4A849F;
5455
--button3: #A4C8D8;
5556
--textarea-font-size: 1em;
56-
--textarea-background-color: #ffffff
57+
--textarea-background-color: white;
5758
}
5859

5960
main {
@@ -94,10 +95,10 @@ main {
9495

9596
.thead {
9697
display: inline-flex;
97-
padding: .5em 2em .3em;
98+
padding: 0 2em;
9899
border-radius: inherit;
99100
border-bottom: 1px solid var(--grey1);
100-
line-height: 1em;
101+
min-height: 2em;
101102
}
102103

103104
.row {
@@ -144,7 +145,7 @@ main {
144145
}
145146

146147
.td-disabled:hover {
147-
background-color: white;
148+
background-color: var(--lightgrey1);
148149
}
149150

150151
.headline {
@@ -157,11 +158,15 @@ main {
157158
.headline-name:hover {
158159
color: var(--darkgrey3);
159160
font-weight: bolder;
161+
border-top: 1px solid var(--grey1);
162+
height: 2em;
160163
}
161164

162165
.headline-name {
163166
cursor: pointer;
164-
padding-left: .5em;
167+
padding: .5em;
168+
border-radius: .2em;
169+
border-top: 1px solid transparent;
165170
}
166171

167172
.options-field {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-generic-crud-table",
3-
"version": "1.5.101",
3+
"version": "1.6.0",
44
"description": "<crud-table> renders object-arrays with options-panel and inline edit per row. Dispatches events for ongoing data handling. As self-containing webcomponent or for Svelte in 60KB",
55
"main": "dist/build/crud-table.js",
66
"module": "dist/build/crud-table.mjs",

src/ArrayService.mjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function setOrder(sortStore, column, order) {
2+
if (order !== undefined) {
3+
return order;
4+
} else {
5+
return (sortStore[column] === undefined || sortStore[column] === 'DESC') ? 'ASC' : 'DESC';
6+
}
7+
}
8+
9+
export function defaultSort(column, sortStore, arr, order) {
10+
sortStore[column] = setOrder(sortStore, column, order);
11+
12+
const tableSort = (a, b) => {
13+
let res = 0;
14+
if (a[column] < b[column]) {
15+
res = -1;
16+
}
17+
if (a[column] > b[column]) {
18+
res = 1;
19+
}
20+
if (sortStore[column] === 'DESC') {
21+
res = res * -1;
22+
}
23+
return res;
24+
};
25+
return arr.sort(tableSort);
26+
}
27+
28+
export function arrayRemove(arr, value) {
29+
let temp = clone(arr);
30+
return temp.filter(function (_ele, i) {
31+
return i !== value;
32+
});
33+
}
34+
35+
export function clone(arr) {
36+
return JSON.parse(JSON.stringify(arr))
37+
}
38+
39+
export default {
40+
defaultSort,
41+
arrayRemove,
42+
clone
43+
};

src/ArrayService.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {defaultSort, arrayRemove, clone} from './ArrayService.mjs';
2+
import {describe, test} from "@jest/globals";
3+
import "regenerator-runtime/runtime";
4+
5+
describe('Test ArrayService', () => {
6+
7+
let mySortStore = [];
8+
9+
10+
test('defaultSort() ASC', async () => {
11+
let arr = [{A_COLUMN_NAME: '3'},{A_COLUMN_NAME: '2'},{A_COLUMN_NAME:'4'},{A_COLUMN_NAME:'1'}];
12+
let actual = defaultSort('A_COLUMN_NAME', mySortStore, arr, 'ASC')
13+
14+
expect(actual).toEqual([{A_COLUMN_NAME: '1'},{A_COLUMN_NAME: '2'},{A_COLUMN_NAME:'3'},{A_COLUMN_NAME:'4'}]);
15+
})
16+
17+
test('defaultSort() DESC', async () => {
18+
let arr = [{A_COLUMN_NAME: '3'},{A_COLUMN_NAME: '2'},{A_COLUMN_NAME:'4'},{A_COLUMN_NAME:'1'},{A_COLUMN_NAME:'4'}];
19+
let actual = defaultSort('A_COLUMN_NAME', mySortStore, arr, 'DESC')
20+
21+
expect(actual).toEqual([{A_COLUMN_NAME:'4'},{A_COLUMN_NAME: '4'},{A_COLUMN_NAME: '3'},{A_COLUMN_NAME:'2'},{A_COLUMN_NAME:'1'}]);
22+
})
23+
24+
test('defaultSort() undefined', async () => {
25+
let arr = [{A_COLUMN_NAME: '3'},{A_COLUMN_NAME: '2'},{A_COLUMN_NAME:'4'},{A_COLUMN_NAME:'1'},{A_COLUMN_NAME:'4'}];
26+
let actual = defaultSort('A_COLUMN_NAME', mySortStore, arr)
27+
28+
expect(actual).toEqual([{A_COLUMN_NAME: '1'},{A_COLUMN_NAME: '2'},{A_COLUMN_NAME:'3'},{A_COLUMN_NAME:'4'},{A_COLUMN_NAME:'4'}]);
29+
})
30+
31+
test('arrayRemove()', async () => {
32+
let arr = [{A_COLUMN_NAME: '3'},{A_COLUMN_NAME: '2'},{A_COLUMN_NAME:'4'},{A_COLUMN_NAME:'1'}];
33+
34+
let actual = arrayRemove(arr, 1)
35+
36+
expect(actual).toEqual([{A_COLUMN_NAME: '3'},{A_COLUMN_NAME:'4'},{A_COLUMN_NAME:'1'}]);
37+
})
38+
39+
test('clone()', async () => {
40+
let arr = [{A_COLUMN_NAME: '3'},{A_COLUMN_NAME: '2'},{A_COLUMN_NAME:'4'},{A_COLUMN_NAME:'1'}];
41+
42+
let actual = clone(arr)
43+
44+
expect(actual).toStrictEqual(arr);
45+
expect(actual === arr).toBeFalsy();
46+
expect(actual[0] === arr[0]).toBeFalsy();
47+
})
48+
49+
})

0 commit comments

Comments
 (0)