Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/bulma/EnsoTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export default {
fetch() {
return this.ready && this.$refs.table.fetch();
},
fetchRow(dtRowId) {
return this.ready && this.$refs.table.fetchRow(dtRowId);
},
highlight(dtRowId) {
return this.ready && this.$refs.table.highlight(dtRowId);
},
Expand Down
3 changes: 3 additions & 0 deletions src/bulma/VueTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export default {
fetch() {
return this.ready && this.$refs.table.fetch();
},
fetchRow(dtRowId) {
return this.ready && this.$refs.table.fetchRow(dtRowId);
},
highlight(dtRowId) {
return this.ready && this.$refs.table.highlight(dtRowId);
},
Expand Down
68 changes: 60 additions & 8 deletions src/renderless/CoreTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export default {
doButtonAction: this.doButtonAction,
exportData: this.exportData,
fetch: this.fetch,
fetchRow: this.fetchRow,
hasContent: this.hasContent,
hasEntries: this.hasEntries,
hasFooter: this.hasFooter,
Expand Down Expand Up @@ -265,7 +266,7 @@ export default {

this.init();
},
request() {
request(dtRowId = null) {
if (this.ongoingRequest) {
this.ongoingRequest.cancel();
}
Expand All @@ -275,13 +276,13 @@ export default {
return this.template.method === 'GET'
? axios[this.template.method.toLowerCase()](
this.template.readPath, {
...this.readRequest(this.template.method),
...this.readRequest(this.template.method, false, false, dtRowId),
cancelToken: this.ongoingRequest.token,
},
)
: axios[this.template.method.toLowerCase()](
this.template.readPath,
this.readRequest(this.template.method),
this.readRequest(this.template.method, false, false, dtRowId),
{ cancelToken: this.ongoingRequest.token },
);
},
Expand Down Expand Up @@ -320,14 +321,65 @@ export default {
}
});
},
readRequest(method, exportMode = false, selection = false) {
fetchRow(dtRowId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we reuse most part of fetch()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do it but the final code was a bit dirtier. because fetchRow() has something in the middle of the method and for refactoring this we should refactor methods to many tiny methods but it makes our class a little complicated(because it isn't SRP!)

const index = this.state.body.data
.findIndex((item) => item[this.template.dtRowId] === dtRowId);

if (index === -1) {
throw new Error(`${dtRowId} is not exist`);
}

const selectedRow = { ...this.state.body.data[index] };
this.meta.loading = true;
this.state.expanded = [];
this.$emit('fetching');

return this.request(dtRowId).then(({ data }) => {
const body = data;
this.meta.loading = false;
this.meta.forceInfo = false;

if (body.data.length === 0) {
return this.fetch();
}

const proccessdMoney = this.meta.money
? this.processMoney(body)
: body;

this.state.body.data[index] = proccessdMoney.data[0];

if (this.meta.number) {
this.processNumber();
}

const changedTotal = Object.keys(this.state.body.total)
.filter((key) => selectedRow[key] !== this.state.body.data[index][key])
.length > 0;

if (changedTotal) {
return this.fetch();
}

this.$emit('fetched');
this.$nextTick(this.refreshPageSelected);
}).catch(error => {
this.meta.loading = false;

if (!axios.isCancel(error)) {
this.errorHandler(error);
}
});
},
readRequest(method, exportMode = false, selection = false, dtRowId = null) {
const params = selection
? { selection: this.state.selected }
: {
withMeta: !dtRowId,
internalFilters: this.internalFilters,
filters: this.filters,
intervals: this.intervals,
params: this.params,
filters: dtRowId ? { [`${this.template.table}`]: { [`${this.template.dtRowId}`]: dtRowId } } : this.filters,
intervals: dtRowId ? {} : this.intervals,
params: dtRowId ? this.params : {},
columns: this.requestColumns(exportMode),
meta: {
start: this.meta.start,
Expand Down Expand Up @@ -367,7 +419,7 @@ export default {
.filter(column => !!column.money)
.forEach(column => {
const total = this.meta.total
&& Object.keys(body.total).includes(column.name);
&& Object.keys(body?.total ?? []).includes(column.name);

let money = body.data.map(row => Number.parseFloat(row[column.name]) || 0);

Expand Down
6 changes: 5 additions & 1 deletion src/renderless/services/NumberFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ class NumberFormatter {
}

format(number) {
const { template, symbol } = this.column.number;
const cleanUp = (value) => value.replace(symbol, '').trim();
const max = (max, value) => Math.max(value.length, max);

number = number.map(cleanUp);

const length = number.reduce(max, 0);
const { template, symbol } = this.column.number;
const pad = value => value.padStart(length, ' ');
const formatter = value => template.replace('%s', symbol)
.replace('%v', pad(value));
Expand Down