const LOG_MSG_PREFIX = "[flex-table/examples.js] ";
function handleRecordsForTablePageRequested(e) {
const { detail, target: flexTableElem } = e;
console.log(`${LOG_MSG_PREFIX}recordsForTablePageRequested(e): e.detail: `, detail);
if (! detail)
return;
let
offsetOfFirstRecordForReturnedPage = -1,
recordsForPage = [],
availableRecordCount = 0;
if (dataRecords.length) {
const { filterSpec, sortSpec } = detail;
let filteredRecords = [ ...dataRecords ];
if (filterSpec) {
const
{ caseSensitive, filterValue, targetFieldNames } = filterSpec,
targetFieldNameCount = targetFieldNames ? targetFieldNames.length : 0;
if (filterValue) {
const caseAdaptedFilterValue = caseSensitive ? filterValue : filterValue.toLocaleLowerCase();
filteredRecords = filteredRecords.filter(dataRecord => {
for(let iFieldName = 0; iFieldName < targetFieldNameCount; ++iFieldName) {
const filterableStringForRecordAndField =
flexTableElem.getFilterableStringForDataRecordAndFieldName(dataRecord, targetFieldNames[iFieldName]);
if (filterableStringForRecordAndField == null)
continue;
if (filterableStringForRecordAndField.length < caseAdaptedFilterValue.length)
continue;
const caseAdaptedFieldValueString = caseSensitive
? filterableStringForRecordAndField
: filterableStringForRecordAndField.toLocaleLowerCase();
if (caseAdaptedFieldValueString.indexOf(caseAdaptedFilterValue) >= 0)
return true;
}
return false;
});
}
}
const filteredRecordsSorted = filteredRecords;
if (filteredRecords.length && sortSpec && Object.keys(sortSpec).length) {
const fieldComparatorFunctionsArray = [];
Object.entries(sortSpec).forEach(([fieldName, columnSortDirection]) => {
let sortDirectionMultiplier = 0;
switch(columnSortDirection) {
case "ascending":
sortDirectionMultiplier = 1;
break;
case "descending":
sortDirectionMultiplier = -1;
break;
}
const dataCellIconSpecGenerator = flexTableElem.getIconSpecGeneratorFunctionForFieldName(fieldName);
const compareFn = dataCellIconSpecGenerator
? (dataRecord1, dataRecord2) => {
const
sortKey1 = flexTableElem.extractDataCellIconSortKey(dataCellIconSpecGenerator(dataRecord1)),
sortKey2 = flexTableElem.extractDataCellIconSortKey(dataCellIconSpecGenerator(dataRecord2));
return sortKey1.localeCompare(sortKey2) * sortDirectionMultiplier;
}
: (dataRecord1, dataRecord2) => {
const
fieldValue1 = dataRecord1[fieldName] || "",
fieldValue2 = dataRecord2[fieldName] || "";
if (fieldValue1 > fieldValue2)
return 1 * sortDirectionMultiplier;
if (fieldValue1 < fieldValue2)
return -1 * sortDirectionMultiplier;
return 0;
};
fieldComparatorFunctionsArray.push(compareFn);
});
const numFieldComparatorFunctions = fieldComparatorFunctionsArray.length;
const recordComparatorFn = (dataRecord1, dataRecord2) => {
for(let i = 0; i < numFieldComparatorFunctions; ++i) {
const compareResult = fieldComparatorFunctionsArray[i](dataRecord1, dataRecord2);
if (compareResult)
return compareResult;
}
return 0;
}
filteredRecordsSorted.sort(recordComparatorFn);
}
availableRecordCount = filteredRecordsSorted.length;
if (availableRecordCount) {
const
{ offsetOfFirstRequestedRecord, maxRecordsPerPage } = detail,
indexOfFinalAvailableRecord = availableRecordCount - 1;
offsetOfFirstRecordForReturnedPage = (offsetOfFirstRequestedRecord <= indexOfFinalAvailableRecord)
? offsetOfFirstRequestedRecord
: Math.floor(indexOfFinalAvailableRecord / maxRecordsPerPage) * maxRecordsPerPage;
recordsForPage = filteredRecordsSorted.slice(offsetOfFirstRecordForReturnedPage, offsetOfFirstRecordForReturnedPage + maxRecordsPerPage);
}
}
const simulateRecordCountUnavailable = flexTableElem.classList.contains(CSSClassNames.NO_AVAILABLE_RECORD_COUNT);
setTimeout(
() => {
flexTableElem.showTablePage(
recordsForPage,
offsetOfFirstRecordForReturnedPage,
simulateRecordCountUnavailable ? undefined : availableRecordCount
)
},
500
);
}