UNPKG

@syncfusion/ej2-treegrid

Version:
234 lines (233 loc) 10.7 kB
import { getObject, Filter as GridFilter, Grid } from '@syncfusion/ej2-grids'; import { isNullOrUndefined, setValue, getValue } from '@syncfusion/ej2-base'; import { getParentData } from '../utils'; /** * TreeGrid Filter module will handle filtering action * * @hidden */ var Filter = /** @class */ (function () { /** * Constructor for Filter module * * @param {TreeGrid} parent - Tree Grid instance */ function Filter(parent) { Grid.Inject(GridFilter); this.parent = parent; this.isHierarchyFilter = false; this.filteredResult = []; this.flatFilteredData = []; this.filteredParentRecs = []; this.addEventListener(); } /** * For internal use only - Get the module name. * * @private * @returns {string} Returns Filter module name */ Filter.prototype.getModuleName = function () { return 'filter'; }; /** * To destroy the Filter module * * @returns {void} * @hidden */ Filter.prototype.destroy = function () { this.removeEventListener(); }; /** * @hidden * @returns {void} */ Filter.prototype.addEventListener = function () { this.parent.on('updateFilterRecs', this.updatedFilteredRecord, this); this.parent.on('clearFilters', this.clearFilterLevel, this); }; /** * @hidden * @returns {void} */ Filter.prototype.removeEventListener = function () { if (this.parent.isDestroyed) { return; } this.parent.off('updateFilterRecs', this.updatedFilteredRecord); this.parent.off('clearFilters', this.clearFilterLevel); }; /** * Function to update filtered records * * @param {{data: Object} } dataDetails - Filtered data collection * @param {Object} dataDetails.data - Fliltered data collection * @hidden * @returns {void} */ Filter.prototype.updatedFilteredRecord = function (dataDetails) { setValue('uniqueIDFilterCollection', {}, this.parent); this.flatFilteredData = dataDetails.data; this.filteredParentRecs = []; this.filteredResult = []; this.isHierarchyFilter = false; for (var f = 0; f < this.flatFilteredData.length; f++) { var rec = this.flatFilteredData[parseInt(f.toString(), 10)]; this.addParentRecord(rec); var hierarchyMode = this.parent.grid.searchSettings.key === '' ? this.parent.filterSettings.hierarchyMode : this.parent.searchSettings.hierarchyMode; if (((hierarchyMode === 'Child' || hierarchyMode === 'None') && (this.parent.grid.filterSettings.columns.length !== 0 || this.parent.grid.searchSettings.key !== ''))) { this.isHierarchyFilter = true; } var ischild = getObject('childRecords', rec); if (!isNullOrUndefined(ischild) && ischild.length) { setValue('hasFilteredChildRecords', this.checkChildExsist(rec), rec); } var parent_1 = getObject('parentItem', rec); if (!isNullOrUndefined(parent_1)) { var parRecord = getParentData(this.parent, rec.parentItem.uniqueID, true); //let parRecord: Object = this.flatFilteredData.filter((e: ITreeData) => { // return e.uniqueID === rec.parentItem.uniqueID; })[0]; setValue('hasFilteredChildRecords', true, parRecord); if (parRecord && parRecord.parentItem) { this.updateParentFilteredRecord(parRecord); } } } if (this.flatFilteredData.length > 0 && this.isHierarchyFilter) { this.updateFilterLevel(); } this.parent.notify('updateAction', { result: this.filteredResult }); }; Filter.prototype.updateParentFilteredRecord = function (record) { var parRecord = getParentData(this.parent, record.parentItem.uniqueID, true); var uniqueIDValue = getValue('uniqueIDFilterCollection', this.parent); if (parRecord && Object.prototype.hasOwnProperty.call(uniqueIDValue, parRecord.uniqueID)) { setValue('hasFilteredChildRecords', true, parRecord); } if (parRecord && parRecord.parentItem) { this.updateParentFilteredRecord(parRecord); } }; Filter.prototype.addParentRecord = function (record) { var parent = getParentData(this.parent, record.parentUniqueID); //let parent: Object = this.parent.flatData.filter((e: ITreeData) => {return e.uniqueID === record.parentUniqueID; })[0]; var hierarchyMode = this.parent.grid.searchSettings.key === '' ? this.parent.filterSettings.hierarchyMode : this.parent.searchSettings.hierarchyMode; if (hierarchyMode === 'None' && (this.parent.grid.filterSettings.columns.length !== 0 || this.parent.grid.searchSettings.key !== '')) { if (isNullOrUndefined(parent)) { if (this.flatFilteredData.indexOf(record) !== -1) { if (this.filteredResult.indexOf(record) === -1) { this.filteredResult.push(record); setValue('uniqueIDFilterCollection.' + record.uniqueID, record, this.parent); record.hasFilteredChildRecords = true; } return; } } else { this.addParentRecord(parent); if (this.flatFilteredData.indexOf(parent) !== -1 || this.filteredResult.indexOf(parent) !== -1) { if (this.filteredResult.indexOf(record) === -1) { this.filteredResult.push(record); setValue('uniqueIDFilterCollection.' + record.uniqueID, record, this.parent); } } else { if (this.filteredResult.indexOf(record) === -1 && this.flatFilteredData.indexOf(record) !== -1) { this.filteredResult.push(record); setValue('uniqueIDFilterCollection.' + record.uniqueID, record, this.parent); } } } } else { if (!isNullOrUndefined(parent)) { var hierarchyMode_1 = this.parent.grid.searchSettings.key === '' ? this.parent.filterSettings.hierarchyMode : this.parent.searchSettings.hierarchyMode; if (hierarchyMode_1 === 'Child' && (this.parent.grid.filterSettings.columns.length !== 0 || this.parent.grid.searchSettings.key !== '')) { if (this.flatFilteredData.indexOf(parent) !== -1) { this.addParentRecord(parent); } } else { this.addParentRecord(parent); } } if (this.filteredResult.indexOf(record) === -1) { this.filteredResult.push(record); setValue('uniqueIDFilterCollection.' + record.uniqueID, record, this.parent); } } }; Filter.prototype.checkChildExsist = function (records) { var childRec = getObject('childRecords', records); var isExist = false; for (var count = 0; count < childRec.length; count++) { var ischild = childRec[parseInt(count.toString(), 10)].childRecords; var hierarchyMode = this.parent.grid.searchSettings.key === '' ? this.parent.filterSettings.hierarchyMode : this.parent.searchSettings.hierarchyMode; if (((hierarchyMode === 'Child' || hierarchyMode === 'Both') && (this.parent.grid.filterSettings.columns.length !== 0 || this.parent.grid.searchSettings.key !== ''))) { var uniqueIDValue = getValue('uniqueIDFilterCollection', this.parent); if (!Object.prototype.hasOwnProperty.call(uniqueIDValue, childRec[parseInt(count.toString(), 10)].uniqueID)) { this.filteredResult.push(childRec[parseInt(count.toString(), 10)]); setValue('uniqueIDFilterCollection.' + childRec[parseInt(count.toString(), 10)].uniqueID, childRec[parseInt(count.toString(), 10)], this.parent); isExist = true; } } if ((hierarchyMode === 'None') && (this.parent.grid.filterSettings.columns.length !== 0 || this.parent.grid.searchSettings.key !== '')) { if (this.flatFilteredData.indexOf(childRec[parseInt(count.toString(), 10)]) !== -1) { isExist = true; break; } } if (!isNullOrUndefined(ischild) && ischild.length) { isExist = this.checkChildExsist(childRec[parseInt(count.toString(), 10)]); } if ((hierarchyMode === 'Child' || hierarchyMode === 'Both') && childRec.length) { isExist = true; } } return isExist; }; Filter.prototype.updateFilterLevel = function () { var record = this.filteredResult; var len = this.filteredResult.length; for (var c = 0; c < len; c++) { var parent_2 = getParentData(this.parent, record[parseInt(c.toString(), 10)].parentUniqueID); var isPrst = record.indexOf(parent_2) !== -1; if (isPrst) { var parent_3 = getParentData(this.parent, record[parseInt(c.toString(), 10)].parentUniqueID, true); record[parseInt(c.toString(), 10)].filterLevel = parent_3.filterLevel + 1; } else { record[parseInt(c.toString(), 10)].filterLevel = 0; this.filteredParentRecs.push(record[parseInt(c.toString(), 10)]); } } }; Filter.prototype.clearFilterLevel = function (data) { var count = 0; var flatData = data.flatData; var len = flatData.length; var currentRecord; for (count; count < len; count++) { currentRecord = flatData[parseInt(count.toString(), 10)]; var fLevel = currentRecord.filterLevel; if (fLevel || fLevel === 0 || !isNullOrUndefined(currentRecord.hasFilteredChildRecords)) { currentRecord.hasFilteredChildRecords = null; currentRecord.filterLevel = null; } } this.filteredResult = []; this.parent.notify('updateResults', { result: flatData, count: flatData.length }); }; return Filter; }()); export { Filter };