vi-grid-builder 2.0.3 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,34 @@
1
1
 
2
2
  # Grid builder plugin for Vue 3
3
3
 
4
+ The grid builder component dynamically generates tables based on the provided `rows` data and a JSON `columns` configuration.
5
+
6
+ ## Key Features
7
+
8
+ ### 1. Column Types
9
+ - **Predefined Types**:
10
+ - `text`: Displays plain text and if sortabel sorted alphabetically.
11
+ - `number`: Displays number that can be formtated with NumberFormatOptions and sorted numerically .
12
+ - `image`: Renders an image.
13
+ - `link`: Displays hyperlinks (with customizable text and URL behavior).
14
+ - `badge`: Displays styled labels or badges.
15
+ - `date`: Formats and displays dates.
16
+ - `actions`: Includes buttons or action elements.
17
+ - **Custom Types**:
18
+ - Register custom field types for unique cell content rendering.
19
+
20
+ ### 2. Data Handling
21
+ - Accepts data in the `rows` prop to populate the table.
22
+ - Supports cell-level customization for data formatting.
23
+
24
+ ### 3. Pagination
25
+ - Built-in pagination for handling large datasets.
26
+ - Configurable to match specific requirements (e.g., rows per page).
27
+
28
+ ### 4. Customizable Styles
29
+ - Provides options to style the table (e.g., striped rows, borders).
30
+ - Supports global configs and per-instance overrides.
31
+
4
32
 
5
33
  ## Installation
6
34
 
@@ -18,6 +46,15 @@ import App from './App.vue'
18
46
  const app = createApp(App).use(ViGridBuilder)
19
47
  ```
20
48
 
49
+ ## Examples
50
+ If you want to see usage examples download the project and run
51
+ ```sh
52
+ cd playgorund
53
+ ```
54
+ ```sh
55
+ npm run dev
56
+ ```
57
+
21
58
  ## Table options
22
59
 
23
60
  | option | type | description | default value |
@@ -63,19 +100,20 @@ const app = createApp(App).use(ViGridBuilder)
63
100
  | align | centre, left, right | How to align the content in the cells for this column | / |
64
101
  | sort | Function | Compare function for sorting data in this column | / |
65
102
 
66
- ##Aditional columsn options for sepcific fields
103
+ ## Aditional columsn options for sepcific fields
67
104
 
68
105
  field type| option | type | description | default value |
69
106
  |------------|------------|-----------------------------|--------------------------------------------------------|---------------|
70
107
  |date | mode| date,time, dateTime| what part of the date shoudl be displayed| dateTime
71
- |date | format| object | format of date field| {date:"yyyy-MM-dd",dateTime:"yyyy-MM-dd HH:mm:ss",time:"HH:mm:ss",}|
108
+ |date | format| object | format of date field| {date:"yyyy-MM-dd",dateTime:"yyyy-MM-dd HH:mm:ss",time:"HH:mm:ss",} `date-fns` is used for formtatiin|
72
109
  |actions |actions|Array of object| Option for field of type action | every object should have unique action field and name for the text displayed for the action |
73
110
  |actions |kebab|boolean| should action menu be 'kebab type'| false |
74
111
  |link |String| link field| If the link field is provided, it is used as the URL, and name is used as the link text. If the link field is not provided, both the URL and link text default to the name field value.| /
112
+ |number | format| object - NumberFormatOptions| format of number field| ex: {style: 'currency',currency: 'USD'}|
75
113
 
76
114
 
77
115
 
78
- ## Example config for columns
116
+ ## Example config for columns
79
117
 
80
118
  ```js
81
119
  columns: [
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vi-grid-builder",
3
3
  "main": "ViGridBuilderPlugin",
4
- "version": "2.0.3",
4
+ "version": "2.0.6",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",
@@ -8,7 +8,6 @@
8
8
  <custom-header/>
9
9
 
10
10
 
11
-
12
11
  <custom-field/>
13
12
 
14
13
  <clickable-row/>
@@ -31,7 +30,6 @@
31
30
  <actions-field/>
32
31
 
33
32
 
34
-
35
33
  <pagination-options/>
36
34
 
37
35
 
@@ -24,16 +24,17 @@ function getRandomDate(startDate, endDate) {
24
24
  const randomTimestamp = Math.floor(Math.random() * (end - start + 1)) + start;
25
25
  return new Date(randomTimestamp).toISOString();
26
26
  }
27
- const posts = Array.from({ length: 25 }, (_, index) => ({
27
+
28
+ const posts = Array.from({length: 25}, (_, index) => ({
28
29
  id: index + 1,
29
30
  title: `Post Title ${index + 1}`,
30
31
  link: `https://example.com/post-${index + 1}`,
31
32
  image: "https://images.pexels.com/photos/2325447/pexels-photo-2325447.jpeg",
32
- status: Math.random() > 0.5 ? "published" : "unpublished",
33
- date:getRandomDate("2020-01-01", "2024-12-31")
34
- }));
33
+ status: Math.random() > 0.5 ? "published":"unpublished",
34
+ date: getRandomDate("2020-01-01", "2024-12-31"),
35
+ price: Math.random() * (1000 - 10) + 10
35
36
 
36
- console.log(posts);
37
+ }));
37
38
 
38
39
 
39
40
  export default {
@@ -41,13 +42,14 @@ export default {
41
42
  data: () => ({
42
43
 
43
44
  loading: true,
44
- rows:posts,
45
+ rows: posts,
45
46
  config: {
46
47
  rowKey: 'id',
47
48
  columns: [{
48
49
  label: 'ID',
49
50
  name: 'id',
50
-
51
+ type: 'number',
52
+ sortable: true,
51
53
  },
52
54
  {
53
55
  label: 'Title',
@@ -56,71 +58,81 @@ export default {
56
58
  {
57
59
  label: 'Link',
58
60
  name: 'link',
59
- type:'link',
61
+ type: 'link',
60
62
  // link:'link'
61
63
  },
62
64
  {
63
65
  label: 'Link with title',
64
66
  name: 'title',
65
- type:'link',
66
- link:'link'
67
+ type: 'link',
68
+ link: 'link'
69
+ },
70
+ {
71
+ label: 'Price',
72
+ name: 'price',
73
+ type: 'number',
74
+ sortable: true,
75
+ format: {
76
+ style: 'currency',
77
+ currency: 'USD',
78
+ }
67
79
  },
68
80
  {
69
81
  label: 'Image',
70
82
  name: 'image',
71
- type:'image'
83
+ type: 'image'
72
84
  },
73
85
  {
74
86
  label: 'Status',
75
87
  name: 'status',
76
- type:'badge'
88
+ type: 'badge'
77
89
  },
78
90
  {
79
91
  label: 'Date',
80
92
  name: 'date',
81
- type:'date',
82
- mode:'date'
93
+ type: 'date',
94
+ mode: 'date'
83
95
  },
84
96
  {
85
97
  label: 'Time',
86
98
  name: 'date',
87
- type:'date',
88
- mode:'time'
99
+ type: 'date',
100
+ mode: 'time'
89
101
  },
90
102
  {
91
103
  label: 'Simple actions',
92
104
  name: 'actions',
93
- type:'actions',
94
- actions:[
105
+ type: 'actions',
106
+ actions: [
95
107
  {
96
- action:'edit',
97
- name:'Edit post'
108
+ action: 'edit',
109
+ name: 'Edit post'
98
110
  },
99
111
  {
100
- action:'remove',
101
- name:'Remove post'
112
+ action: 'remove',
113
+ name: 'Remove post'
102
114
  }
103
115
  ]
104
116
  },
105
117
  {
106
- label: 'Kebap type actions',
118
+ label: 'Kebab type actions',
107
119
  name: 'actions',
108
- type:'actions',
109
- kebab:true,
110
- actions:[
120
+ type: 'actions',
121
+ kebab: true,
122
+ actions: [
111
123
  {
112
- action:'edit',
113
- name:'Edit post'
124
+ action: 'edit',
125
+ name: 'Edit post'
114
126
  },
115
127
  {
116
- action:'remove',
117
- name:'Remove post'
128
+ action: 'remove',
129
+ name: 'Remove post'
118
130
  }
119
131
  ]
120
132
  },
121
133
  ],
122
134
 
123
- pagination: {page: 1, rowsPerPage:15},
135
+ pagination: {page: 1, rowsPerPage: 15},
124
136
 
125
137
  },
126
138
  compact: false,
@@ -25,7 +25,7 @@
25
25
  </div>
26
26
  </div>
27
27
 
28
- <!-- :align="alignment" default aginment for table-->
28
+ <!-- :align="alignment" default aginment for table-->
29
29
  <vi-grid-builder
30
30
  :bordered="bordered"
31
31
  :stripped="stripped"
@@ -1,5 +1,6 @@
1
1
  <template>
2
2
  <div class="vi-grid-builder">
3
+ <div> sortDirection</div>
3
4
  <div class="vi-table-container" :class="{'loading':isDataLoading}">
4
5
  <table class="vi-table" :class="{'stripped': settings.stripped, 'bordered': settings.bordered}">
5
6
 
@@ -8,7 +9,8 @@
8
9
  <!------------------- title -------------->
9
10
 
10
11
  <tr v-if="title" class="vi-header-row vi-table-row vi-title-row">
11
- <vi-grid-head-cell v-if="title" :colspan="visibleColumns.length" :align="settings.align" class="vi-title-cell">
12
+ <vi-grid-head-cell v-if="title" :colspan="visibleColumns.length" :align="settings.align"
13
+ class="vi-title-cell">
12
14
  <template #default>
13
15
  <slot name="title" :title="title">
14
16
  <span>{{ title }}}</span>
@@ -194,7 +196,7 @@ export default {
194
196
 
195
197
  disablePagination: {
196
198
  type: Boolean,
197
- default:undefined
199
+ default: undefined
198
200
  },
199
201
  fetchServerData: {
200
202
  type: Function
@@ -236,11 +238,11 @@ export default {
236
238
  //STYLE PROPS
237
239
  stripped: {
238
240
  type: Boolean,
239
- default:undefined
241
+ default: undefined
240
242
  },
241
243
  bordered: {
242
244
  type: Boolean,
243
- default:undefined
245
+ default: undefined
244
246
  },
245
247
  compactPagination: {
246
248
  type: Boolean,
@@ -279,18 +281,18 @@ export default {
279
281
  const {
280
282
  setSortFieldsFromPagination,
281
283
  getSortableState,
282
- clientSort,
283
284
  setSortFields,
284
285
  sortField,
285
286
  sortDirection,
286
287
  formatSortFields,
288
+ sortedRows
287
289
  } = useSort(props);
288
290
 
289
291
  setSortFieldsFromPagination(props.pagination);
290
292
 
291
293
  const {localPagination, onPaginationChange} = usePagination(props, emit, fetchData)
292
294
 
293
- watch([sortField, sortDirection], () => {
295
+ watch([sortField, sortDirection], (...args) => {
294
296
  if (props.serverMode) {
295
297
  fetchData({
296
298
  pagination: {
@@ -308,14 +310,14 @@ export default {
308
310
  });
309
311
 
310
312
  //ROW SELECT, CLICK AND COMPUTED VALUES
311
- const {currentVisibleRows, totalRows, onRowClicked} = useRowHelper(props, emit, clientSort);
313
+ const {currentVisibleRows, totalRows, onRowClicked} = useRowHelper(props, emit, sortedRows);
312
314
  const {
313
315
  selectedRows,
314
316
  toggleMasterSelect,
315
317
  toggleRowSelect,
316
318
  areAllRowsSelected,
317
319
  isRowSelected
318
- } = useRowSelect(props, emit);
320
+ } = useRowSelect(props, emit, currentVisibleRows);
319
321
 
320
322
 
321
323
  const isDataLoading = computed(() => {
@@ -345,11 +347,12 @@ export default {
345
347
  onRowClicked,
346
348
  isRowSelected,
347
349
  getSortableState,
348
-
350
+ sortDirection,
349
351
  localPagination,
350
352
  onPaginationChange,
351
353
  rowMenuOpened,
352
- settings
354
+ settings,
355
+ sortedRows
353
356
  }
354
357
  },
355
358
 
@@ -1,88 +1,90 @@
1
1
  <template>
2
- <div class="vi-grid-field">
3
- <component
4
- v-if="fieldComponent"
5
- v-bind:is="fieldComponent"
6
- v-bind="field"
7
- :value="filedValueFormatted"
8
- :original-value="fieldValue"
9
- :row="rowValue"
10
- :row-key="rowKey"
11
- :row-menu-opened="rowMenuOpened"
12
- @update:rowMenuOpened="$emit('update:rowMenuOpened',$event)"
13
- @actionClicked="$action => $emit(`action-clicked`, $action)"
2
+ <div class="vi-grid-field">
3
+ <component
4
+ v-if="fieldComponent"
5
+ v-bind:is="fieldComponent"
6
+ v-bind="field"
7
+ :value="filedValueFormatted"
8
+ :original-value="fieldValue"
9
+ :row="rowValue"
10
+ :row-key="rowKey"
11
+ :row-menu-opened="rowMenuOpened"
12
+ @update:rowMenuOpened="$emit('update:rowMenuOpened',$event)"
13
+ @actionClicked="$action => $emit(`action-clicked`, $action)"
14
14
 
15
- />
16
- <slot v-else name="custom-field"
17
- :field="field"
18
- :value="fieldValue"
19
- :original-value="fieldValue"
20
- :row="rowValue"
21
- :row-key="rowKey"
22
- />
23
- </div>
15
+ />
16
+ <slot v-else name="custom-field"
17
+ :field="field"
18
+ :value="fieldValue"
19
+ :original-value="fieldValue"
20
+ :row="rowValue"
21
+ :row-key="rowKey"
22
+ />
23
+ </div>
24
24
  </template>
25
25
 
26
26
  <script>
27
- const snakeToCamel = str =>
27
+ const snakeToCamel = str =>
28
28
  str.toLowerCase().replace(/([-_][a-z])/g, group =>
29
- group
30
- .toUpperCase()
31
- .replace('-', '')
32
- .replace('_', '')
29
+ group
30
+ .toUpperCase()
31
+ .replace('-', '')
32
+ .replace('_', '')
33
33
  );
34
- const uppercaseFirstLetter = str => str.charAt(0).toUpperCase() + str.slice(1);
34
+ const uppercaseFirstLetter = str => str.charAt(0).toUpperCase() + str.slice(1);
35
35
 
36
- import ViGridBadge from "./fields/ViGridBadge.vue";
37
- import ViGridImage from "./fields/ViGridImage.vue";
38
- import ViGridText from "./fields/ViGridText.vue";
39
- import ViGridDate from "./fields/ViGridDate.vue";
40
- import ViGridActions from "./fields/ViGridActions.vue";
41
- import ViGridLink from "./fields/ViGridLink.vue";
36
+ import ViGridBadge from "./fields/ViGridBadge.vue";
37
+ import ViGridImage from "./fields/ViGridImage.vue";
38
+ import ViGridText from "./fields/ViGridText.vue";
39
+ import ViGridDate from "./fields/ViGridDate.vue";
40
+ import ViGridActions from "./fields/ViGridActions.vue";
41
+ import ViGridLink from "./fields/ViGridLink.vue";
42
+ import ViGridNumber from "./fields/ViGridNumber.vue";
42
43
 
43
- export default {
44
- name: "ViGridField",
45
- components: {
46
- ViGridBadge,
47
- ViGridImage,
48
- ViGridText,
49
- ViGridDate,
50
- ViGridActions,
51
- ViGridLink,
44
+ export default {
45
+ name: "ViGridField",
46
+ components: {
47
+ ViGridBadge,
48
+ ViGridImage,
49
+ ViGridText,
50
+ ViGridDate,
51
+ ViGridActions,
52
+ ViGridLink,
53
+ ViGridNumber,
54
+ },
55
+ props: {
56
+ field: {
57
+ type: Object,
58
+ required: true
52
59
  },
53
- props: {
54
- field: {
55
- type: Object,
56
- required: true
57
- },
58
- fieldValue: {},
59
- rowValue: {},
60
- rowKey: String,
61
- rowMenuOpened: {},
62
- noDataLabel: {
63
- type: String,
64
- default: '/'
65
- }
60
+ fieldValue: {},
61
+ rowValue: {},
62
+ rowKey: String,
63
+ rowMenuOpened: {},
64
+ noDataLabel: {
65
+ type: String,
66
+ default: '/'
67
+ }
68
+ },
69
+ computed: {
70
+ filedValueFormatted() {
71
+ return this.fieldValue ?? this.noDataLabel
66
72
  },
67
- computed: {
68
- filedValueFormatted() {
69
- return this.fieldValue ?? this.noDataLabel
70
- },
71
- fieldComponent() {
72
- let type = this.field.type ? `vi-grid-${this.field.type}` : 'vi-grid-text'
73
+ fieldComponent() {
74
+ let type = this.field.type ? `vi-grid-${this.field.type}`:'vi-grid-text'
73
75
 
74
- let component = uppercaseFirstLetter(snakeToCamel(type))
76
+ let component = uppercaseFirstLetter(snakeToCamel(type))
75
77
 
76
- if (this.$options.components[component]) {
77
- return type
78
- } else {
79
- return false;
80
- }
78
+ if (this.$options.components[component]) {
79
+ return type
80
+ } else {
81
+ return false;
81
82
  }
82
- },
83
- }
83
+ }
84
+ },
85
+ }
84
86
  </script>
85
87
 
86
88
  <style lang="scss">
87
- @import "ViGridField";
89
+ @import "ViGridField";
88
90
  </style>
@@ -1,28 +1,27 @@
1
- import {computed} from "vue";
2
-
3
- import useSort from "./useSort";
1
+ import {computed, watch} from "vue";
4
2
  import {globalGridSettings} from "../composables/useSettings.js";
5
3
 
6
- export default function useRowHelper(props, emit) {
4
+ export default function useRowHelper(props, emit, sortedRows) {
7
5
 
8
- const {sortedRows} = useSort(props);
9
6
 
10
7
  const totalRows = computed(() => {
11
8
  return props.serverMode ? props.pagination?.rowsNumber:props.rows.length;
12
9
  });
13
10
 
14
11
  const currentVisibleRows = computed(() => {
12
+
15
13
  if (props.serverMode || props.disablePagination) {
16
14
  return sortedRows.value;
17
15
  }
18
16
 
19
17
  let rowsPerPage = props.pagination?.rowsPerPage ?? globalGridSettings.value.rowsPerPage
20
- if (rowsPerPage === 0) {
18
+ if (rowsPerPage===0) {
21
19
  return sortedRows.value;
22
20
  }
23
21
  const startIndex = (props.pagination.page - 1) * rowsPerPage;
24
22
  const endIndex = startIndex + rowsPerPage;
25
23
 
24
+
26
25
  return sortedRows.value.slice(startIndex, endIndex)
27
26
  });
28
27
 
@@ -1,11 +1,9 @@
1
-
2
1
  import useRowHelper from "../composables/useRowHelper.js";
3
2
  import {computed, ref} from "vue";
4
3
 
5
- export default function useRowSelect(props, emit) {
4
+ export default function useRowSelect(props, emit, currentVisibleRows) {
6
5
 
7
6
  let selectedRows = ref([]);
8
- const {currentVisibleRows} = useRowHelper(props, emit);
9
7
  const toggleRowSelect = (row, selected) => {
10
8
  if (selected) {
11
9
  selectedRows.value.push(row[props.rowKey])
@@ -29,7 +29,6 @@ export default function useSort(props) {
29
29
  sortFieldType.value = nextDirection===null ? null:column.type;
30
30
  sortDirection.value = nextDirection;
31
31
  }
32
-
33
32
  return {
34
33
  field: sortField.value,
35
34
  direction: sortDirection.value
@@ -44,20 +43,43 @@ export default function useSort(props) {
44
43
  }];
45
44
  }
46
45
 
47
- //sort rows on client side
48
- const clientSort = function (rows) {
49
- if (sortDirection.value==='ascending') {
50
-
46
+ const _clientNumberSort = (rows, sortField, sortDirection) => {
47
+ if (sortDirection==='ascending') {
51
48
  return rows.sort((a, b) => {
52
- return a[sortField.value].toString().localeCompare(b[sortField.value].toString())
49
+ return Number(a[sortField]) - Number(b[sortField])
53
50
  });
54
- } else if (sortDirection.value==='descending') {
51
+ } else if (sortDirection==='descending') {
55
52
 
56
- return rows.sort((a, b) => b[sortField.value].toString().localeCompare(a[sortField.value].toString()));
53
+ return rows.sort((a, b) => {
54
+ return Number(b[sortField]) - Number(a[sortField])
55
+ });
57
56
  }
57
+ return rows;
58
+ }
59
+ const _clientAlphabetSort = (rows, sortField, sortDirection) => {
60
+ if (sortDirection==='ascending') {
61
+ return rows.sort((a, b) => {
62
+ return a[sortField].toString().localeCompare(b[sortField].toString())
63
+ });
64
+ } else if (sortDirection==='descending') {
58
65
 
66
+ return rows.sort((a, b) => b[sortField].toString().localeCompare(a[sortField].toString()));
67
+ }
59
68
  return rows;
60
69
  }
70
+ //sort rows on client side
71
+ const clientSort = function (rows, sortField, sortFieldType, sortDirection) {
72
+ if (!sortDirection) {
73
+ return rows;
74
+ }
75
+ if (sortFieldType==='number') {
76
+
77
+ return _clientNumberSort(rows, sortField, sortDirection);
78
+ }
79
+
80
+ return _clientAlphabetSort(rows, sortField, sortDirection);
81
+
82
+ }
61
83
 
62
84
  // Set the sort fields from pagination data from API
63
85
  const setSortFieldsFromPagination = (pagination) => {
@@ -82,10 +104,10 @@ export default function useSort(props) {
82
104
 
83
105
  const sortedRows = computed(() => {
84
106
  if (props.serverMode) {
85
- return props.rows;
107
+ return props.rows ?? [];
86
108
  }
87
109
 
88
- return clientSort([...props.rows])
110
+ return clientSort([...props.rows ?? []], sortField.value, sortFieldType.value, sortDirection.value)
89
111
  });
90
112
 
91
113
 
@@ -6,5 +6,8 @@
6
6
  line-height: 1;
7
7
  //font-weight: 600;
8
8
  text-transform: uppercase;
9
+ background-color: var(--vi-grid-primary-color);
10
+ color: var(--vi-grid-primary-text-color);
11
+ padding: 5px;
9
12
 
10
13
  }
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <div class="vi-grid-badge" v-bind="$attrs">
2
+ <div class="vi-grid-badge" v-bind="$attrs" :class="value">
3
3
  {{value}}
4
4
  </div>
5
5
  </template>
@@ -0,0 +1,3 @@
1
+ .vi-grig-number {
2
+
3
+ }
@@ -0,0 +1,30 @@
1
+ <template>
2
+ <div class="vi-grid-number">
3
+ {{ formattedNumber }}
4
+ </div>
5
+ </template>
6
+
7
+ <script>
8
+ export default {
9
+ name: "ViGridNumber",
10
+ props: {
11
+ value: {},
12
+ format: {
13
+ type: Object,
14
+ }
15
+ },
16
+ computed: {
17
+ formattedNumber() {
18
+ if (!this.format) {
19
+ return this.value
20
+ }
21
+ return new Intl.NumberFormat(undefined, this.format).format(Number(this.value));
22
+
23
+ }
24
+ }
25
+ }
26
+ </script>
27
+
28
+ <style lang="scss">
29
+ @import "ViGridNumber.scss";
30
+ </style>