@progress/kendo-angular-grid
Version:
Kendo UI Grid for Angular - high performance data grid with paging, filtering, virtualization, CRUD, and more.
93 lines (92 loc) • 3.27 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { filterBy } from "@progress/kendo-data-query";
import { parseDate } from "@progress/kendo-angular-intl";
/**
* @hidden
* Converts date strings in a filter to Date objects.
*/
export const convertDateStringsInFilter = (filter) => {
if (!filter) {
return filter;
}
if (filter.filters && Array.isArray(filter.filters)) {
return {
...filter,
filters: filter.filters.map(f => convertDateStringsInFilter(f))
};
}
if (filter.field && filter.value !== undefined) {
if (typeof filter.value === 'string' && isDateOperator(filter.operator)) {
const date = parseDate(filter.value);
return {
...filter,
value: date || filter.value
};
}
}
return filter;
};
/**
* @hidden
*/
export const isDateOperator = (operator) => {
const dateOperators = [
'eq', 'neq', 'lt', 'lte', 'gt', 'gte'
];
return dateOperators.includes(operator);
};
/**
* @hidden
* Processes cell highlights for a specific filter and item.
*/
const processCellHighlights = (filter, rowIndex, columns, highlightItems) => {
Object.keys(filter.cells).forEach((columnField) => {
const actualColumnIndex = Array.from(columns).findIndex((col) => col.field === columnField);
if (actualColumnIndex !== -1) {
highlightItems.push({
itemKey: rowIndex,
columnKey: actualColumnIndex,
});
}
});
};
/**
* @hidden
* Processes filtered results and adds highlight items.
*/
const processFilteredResults = (filteredResults, data, filter, columns, highlightItems) => {
filteredResults?.forEach((item) => {
const rowIndex = data.findIndex((dataItem) => dataItem === item);
if (filter.cells && Object.keys(filter.cells).length > 0) {
processCellHighlights(filter, rowIndex, columns, highlightItems);
}
else {
highlightItems.push({
itemKey: rowIndex,
});
}
});
};
/**
* @hidden
* Highlights items in a grid based on the provided filters and columns.
* @param data - The data to be highlighted.
* @param filters - The composite highlight descriptors containing the filters and logic.
* @param columns - The columns of the grid.
* @returns An array of HighlightItem objects representing the highlighted items.
*/
export const highlightBy = (data, filters, columns) => {
const highlightItems = [];
filters.forEach((filter) => {
const processedFilters = filter.filters.map((filter) => convertDateStringsInFilter(filter));
const filteredResults = filterBy(data, {
logic: filter.logic || "and",
filters: processedFilters,
});
processFilteredResults(filteredResults, data, filter, columns, highlightItems);
});
return highlightItems;
};