Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature datatable improvements #5

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Changes from all commits
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
56 changes: 45 additions & 11 deletions components/lib/datatable/DataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -765,18 +765,26 @@ export default {
return this.sortMultiple(value);
}

const stringCompare = new Intl.Collator(undefined, { numeric: true }).compare;

let data = [...value];

let lookupMap = new Map();

for (let item of data) {
lookupMap.set(item, ObjectUtils.resolveFieldData(item, this.d_sortField));
}

data.sort((data1, data2) => {
let value1 = ObjectUtils.resolveFieldData(data1, this.d_sortField);
let value2 = ObjectUtils.resolveFieldData(data2, this.d_sortField);
let value1 = lookupMap(data1);
let value2 = lookupMap(data2);

let result = null;

if (value1 == null && value2 != null) result = -1;
else if (value1 != null && value2 == null) result = 1;
else if (value1 == null && value2 == null) result = 0;
else if (typeof value1 === 'string' && typeof value2 === 'string') result = value1.localeCompare(value2, undefined, { numeric: true });
else if (typeof value1 === 'string' && typeof value2 === 'string') result = stringCompare(value1, value2);
else result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0;

return this.d_sortOrder * result;
Expand Down Expand Up @@ -836,16 +844,39 @@ export default {

this.d_multiSortMeta = [...this.d_multiSortMeta];
},
getActiveFilters(filters) {
const removeEmptyFilters = ([key, value]) => {
if (value.constraints) {
const filteredConstraints = value.constraints.filter((constraint) => constraint.value !== null);

if (filteredConstraints.length > 0) {
return [key, { ...value, constraints: filteredConstraints }];
}
} else if (value.value !== null) {
return [key, value];
}

return undefined;
};

const filterValidEntries = (entry) => entry !== undefined;

const entries = Object.entries(filters).map(removeEmptyFilters).filter(filterValidEntries);

return Object.fromEntries(entries);
},
filter(data) {
if (!data) {
return;
}

let activeFilters = this.getActiveFilters(this.filters);

this.clearEditingMetaData();

let globalFilterFieldsArray;

if (this.filters['global']) {
if (activeFilters['global']) {
globalFilterFieldsArray = this.globalFilterFields || this.columns.map((col) => this.columnProp(col, 'filterField') || this.columnProp(col, 'field'));
}

Expand All @@ -856,11 +887,11 @@ export default {
let globalMatch = false;
let localFiltered = false;

for (let prop in this.filters) {
if (Object.prototype.hasOwnProperty.call(this.filters, prop) && prop !== 'global') {
for (let prop in activeFilters) {
if (Object.prototype.hasOwnProperty.call(activeFilters, prop) && prop !== 'global') {
localFiltered = true;
let filterField = prop;
let filterMeta = this.filters[filterField];
let filterMeta = activeFilters[filterField];

if (filterMeta.operator) {
for (let filterConstraint of filterMeta.constraints) {
Expand All @@ -880,11 +911,11 @@ export default {
}
}

if (this.filters['global'] && !globalMatch && globalFilterFieldsArray) {
if (localMatch && activeFilters['global'] && !globalMatch && globalFilterFieldsArray) {
for (let j = 0; j < globalFilterFieldsArray.length; j++) {
let globalFilterField = globalFilterFieldsArray[j];

globalMatch = FilterService.filters[this.filters['global'].matchMode || FilterMatchMode.CONTAINS](ObjectUtils.resolveFieldData(data[i], globalFilterField), this.filters['global'].value, this.filterLocale);
globalMatch = FilterService.filters[activeFilters['global'].matchMode || FilterMatchMode.CONTAINS](ObjectUtils.resolveFieldData(data[i], globalFilterField), activeFilters['global'].value, this.filterLocale);

if (globalMatch) {
break;
Expand All @@ -894,7 +925,7 @@ export default {

let matches;

if (this.filters['global']) {
if (activeFilters['global']) {
matches = localFiltered ? localFiltered && localMatch && globalMatch : globalMatch;
} else {
matches = localFiltered && localMatch;
Expand All @@ -905,7 +936,7 @@ export default {
}
}

if (filteredValue.length === this.value.length) {
if (filteredValue.length === this.value.length || Object.keys(activeFilters).length == 0) {
filteredValue = data;
}

Expand Down Expand Up @@ -1060,6 +1091,7 @@ export default {
break;

case 'Enter':
case 'NumpadEnter':
this.onEnterKey(event, rowData, rowIndex);
break;

Expand Down Expand Up @@ -1478,6 +1510,8 @@ export default {
!!el && (el.style.width = el.style.minWidth = tableWidth);
};

// Reasoning: resize table cells before updating the table width so that it can use existing computed cell widths and adjust only the one column.
this.resizeTableCells(newColumnWidth);
updateTableWidth(this.$refs.table);

if (!this.virtualScrollerDisabled) {
Expand Down