Skip to content

Commit d703af9

Browse files
authored
Merge pull request #39 from SwapnilTechVariable/bug/data-table-add-node
Data table bugs fixed
2 parents 2e7ec24 + 06bb174 commit d703af9

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

src/components/common/items/data-table/data-table.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Host, h, State, Prop } from '@stencil/core';
1+
import { Component, Host, h, State, Prop, Watch } from '@stencil/core';
22
import formatter from 'format-number';
33

44
const sort = (
@@ -57,6 +57,7 @@ export class DataTable {
5757
@State() isEditingIndex: number = -1;
5858
@State() editingState: { [rowColumnId: string]: { prevValue: TField; newValue: TField } } = {};
5959
// TODO: Need to find a way to use TColumn here
60+
@State() columnNames: string[] = [];
6061
@Prop() columns: {
6162
id: number | string;
6263
key: string;
@@ -88,7 +89,15 @@ export class DataTable {
8889
cellClass?: string;
8990
};
9091
}[] = [];
92+
@Watch('columns')
93+
watchPropHandler(newValue: any, oldValue: any) {
94+
if (newValue !== oldValue) {
95+
const updatedColumns = this.columns.map(item => item.name);
96+
this.columnNames = updatedColumns;
97+
}
98+
}
9199
@Prop() data: Array<any> = [];
100+
@State() processedData: Array<any> = [];
92101
@Prop() showActions: boolean = false;
93102
@Prop() onEdit: (id: number | string, changes: Array<{ prevValue: number | Date | string; newValue: number | Date | string; name: string }>) => Promise<any>;
94103
@Prop() onDelete: (index: number, row: { [field: string]: number | Date | string }) => Promise<any>;
@@ -206,6 +215,23 @@ export class DataTable {
206215
handlePaginate() {
207216
this.onPaginate(this.currentPage, this.limit);
208217
}
218+
dataProcessor(data) {
219+
const newData = data.map(row => {
220+
const processedRow = { ...row };
221+
222+
this.columns
223+
.map(item => item.name)
224+
.forEach(column => {
225+
if (!Object.keys(row).includes(column)) {
226+
processedRow[column] = '';
227+
}
228+
});
229+
230+
return processedRow;
231+
});
232+
233+
return newData;
234+
}
209235

210236
render() {
211237
const renderAction = (row: { [field: string]: TField }, rowId: number) => {
@@ -352,13 +378,15 @@ export class DataTable {
352378
</tr>
353379
</thead>
354380
<tbody class="bg-white divide-y divide-gray-200">
355-
{this.data.map((row, rowId) => {
381+
{this.dataProcessor(this.data).map((row, rowId) => {
356382
return (
357383
<tr class="hover:bg-gray-100 transition">
358384
{renderAction(row, rowId)}
359-
{Object.keys(row).map((fieldKey, columnId) => {
360-
return renderRow(fieldKey, row[fieldKey], rowId, columnId);
361-
})}
385+
{this.columns
386+
.map(item => item.name)
387+
.map((fieldKey, columnId) => {
388+
return renderRow(fieldKey, row[fieldKey], rowId, columnId);
389+
})}
362390
</tr>
363391
);
364392
})}
@@ -381,6 +409,7 @@ export class DataTable {
381409
onChange={e => {
382410
// @ts-expect-error
383411
this.limit = e.target.value;
412+
this.currentPage = 1;
384413
this.handlePaginate();
385414
}}
386415
class="form-select px-3 py-1.5 border-none text-inherit font-inherit text-gray-700 bg-transparent bg-clip-padding bg-no-repeat rounded transition ease-in-out focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"

src/components/editorPage/editor-res/editor-res.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class EditorRes {
6767
chips[id] = state.order[id] === 'desc' ? 'asc' : 'desc';
6868
state.order = chips;
6969
state.queryMode = 'read';
70+
state.page = 1;
7071
state.refreshData();
7172
};
7273

@@ -82,6 +83,7 @@ export class EditorRes {
8283

8384
state.filter = chips;
8485
state.queryMode = 'read';
86+
state.page = 1;
8587
state.refreshData();
8688
}
8789
toggleModalState() {

src/components/editorPage/store.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ onChange('nodes', value => {
147147

148148
state.columnHeaders = [...keys].map((k: string) => {
149149
let dataType = 'string';
150-
151-
value.slice(0, 5).forEach(row => {
152-
dataType = typeof row[k];
150+
value.every(row => {
151+
if (row[k] !== undefined) {
152+
dataType = typeof row[k];
153+
return false;
154+
}
155+
return true;
153156
});
154157

155158
return {

src/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html dir="ltr" lang="en" class="dark">
2+
<html dir="ltr" lang="en" class="light">
33

44
<head>
55
<meta charset="utf-8" />
@@ -27,7 +27,7 @@
2727
<!-- <editor-res></editor-res> -->
2828
<!-- <permission-editor url="http://localhost:3000/api/permissions" rolesurl="http://localhost:3000/api/permissions/all"></permission-editor> -->
2929
<!-- <tabs-component></tabs-component> -->
30-
<!-- <editor-page url="http://localhost:3000/api/editor"></editor-page> -->
30+
<editor-page url="http://localhost:3000/api/editor"></editor-page>
3131
<!-- <query-logs ></query-logs> -->
3232
<!-- <navigators-component></navigators-component> -->
3333
<!-- <users-component></users-component> -->
@@ -43,7 +43,7 @@
4343
<!-- <basic-settings></basic-settings> -->
4444
<!-- <plain-button >Button</plain-button> -->
4545
<!-- <icon-label-submit-button width="auto" size="md" varient="contained" color="secondary" loading>Button</icon-label-submit-button> -->
46-
<icon-button-basic size="sm" color="secondary" title="download"/>
46+
<!-- <icon-button-basic size="sm" color="secondary" title="download"/> -->
4747
</fluid-container>
4848
</div>
4949
</body>

0 commit comments

Comments
 (0)