binxie
2023-06-26 b5c5eb130ca0848124f9d136af4be142ad5aac07
force-app/main/default/lwc/lwcDatatableUtility/lwcDatatableUtility.js
@@ -11,24 +11,25 @@
const DEFAULTHEIGHT = '300';
export default class LwcDatatableUtility extends LightningElement {
    static customTypes = {
        customUnit: {
            template: customUnitTemplate,
            standardCellLayout: true,
            typeAttributes: ['Id']
            typeAttributes: ['Id'],
        },
        customShipment: {
            template: customShipmentNumberTemplate,
            standardCellLayout: true,
            typeAttributes: ['Id']
            typeAttributes: ['Id'],
        },
        customShipmentUnitPrice: {
            template: customShippingUnitPriceTemplate,
            standardCellLayout: true,
            typeAttributes: ['Id']
            typeAttributes: ['Id'],
        }
        // Other types here
    };
    }
    // Input Attributes from Parent Componant
    @api keyField = 'Id';
@@ -83,8 +84,7 @@
            this.pageSize = this.totalRecords;
            this.showPagination = false;
        }
        this.paginationVisibility =
            this.showPagination === false ? HIDEDIV : SHOWDIV;
        this.paginationVisibility = this.showPagination === false ? HIDEDIV : SHOWDIV;
        this.filteredRecords = this.records;
        this.filtredNum = this.totalRecords;
        this.setRecordsOnPage();
@@ -114,24 +114,22 @@
    @api
    setRecordsOnPage() {
        this.recordsToDisplay = [];
        if (!this.pageSize) this.pageSize = this.filtredNum;
        if(!this.pageSize)
            this.pageSize = this.filtredNum;
        this.totalPages = Math.ceil(this.filtredNum / this.pageSize);
        this.setPaginationControls();
        for (
            let i = (this.pageNumber - 1) * this.pageSize;
            i < this.pageNumber * this.pageSize;
            i++
        ) {
        for(let i=(this.pageNumber-1)*this.pageSize; i < this.pageNumber*this.pageSize; i++){
            if (i === this.filtredNum) break;
            this.recordsToDisplay.push(this.filteredRecords[i]);
        }
        this.preSelected = [];
        this.selectedRecords.forEach((item) => {
            if (item.selected) this.preSelected.push(item.Id);
        });
            if(item.selected)
                this.preSelected.push(item.Id);
        })
        let paginatedRecords = new Object();
        paginatedRecords.recordsToDisplay = this.recordsToDisplay;
        paginatedRecords.preSelected = this.preSelected;
@@ -179,11 +177,7 @@
                //Use other field name here in place of 'Name' field if you want to search by other field
                //this.recordsToDisplay = this.records.filter(rec => rec.includes(searchKey));
                //Search with any column value (Updated as per the feedback)
                this.filteredRecords = this.records.filter((rec) =>
                    JSON.stringify(rec)
                        .toLowerCase()
                        .includes(searchKey.toLowerCase())
                );
                this.filteredRecords = this.records.filter(rec => JSON.stringify(rec).toLowerCase().includes(searchKey.toLowerCase()));
                this.filtredNum = this.filteredRecords.length;
                this.setRecordsOnPage();
            }, DELAY);
@@ -199,13 +193,9 @@
        console.log(selectedRows.length);
        this.totalSelected = 0;
        this.pageSelectedRecords = [];
        if (
            this.maxRowSelection != '1' &&
            this.recordsToDisplay &&
        if(this.maxRowSelection != '1' && this.recordsToDisplay &&
            this.recordsToDisplay.length > 0 &&
            ((selectedRows.length === 0 && !this.refreshCurrentData) ||
                selectedRows.length > 0)
        ) {
            ((selectedRows.length === 0 && !this.refreshCurrentData) || selectedRows.length > 0) ){
            this.recordsToDisplay.forEach((item) => {
                var row = new Object();
                row.Id = item.Id;
@@ -221,40 +211,26 @@
        if (this.selectedRecords.length == 0) {
            this.selectedRecords = this.pageSelectedRecords;
        }
        this.selectedRecords = this.mergeObjectArray(
            this.selectedRecords,
            this.pageSelectedRecords,
            'Id'
        );
        if (
            this.maxRowSelection === '1' &&
            selectedRows &&
            selectedRows.length > 0
        ) {
        this.selectedRecords = this.mergeObjectArray(this.selectedRecords, this.pageSelectedRecords, "Id");
        if(this.maxRowSelection === '1' && selectedRows && selectedRows.length > 0){
            this.totalSelected = 1;
        } else {
            let i = 0;
            this.selectedRecords.forEach((item) => {
            this.selectedRecords.forEach(item => {
                if (item.selected) {
                    i++;
                    this.totalSelected = i;
                }
            });
            })
            //this.totalSelected = this.totalSelected ===1 && selectedRows.length ===0? 0: this.totalSelected;
        }
        const filterSelected = this.selectedRecords.filter(
            ({ selected }) => selected === true
        );
        this.dispatchEvent(
            new CustomEvent('setselectedrecords', { detail: filterSelected })
        ); //Send records to display on table to the parent component
        const filterSelected = this.selectedRecords.filter(({ selected }) => selected === true );
        this.dispatchEvent(new CustomEvent('setselectedrecords', {detail: filterSelected})); //Send records to display on table to the parent component
        this.refreshCurrentData = false;
    }
    mergeObjectArray(firstArray, secondArray, prop) {
        var reduced = firstArray.filter(
            (aitem) => !secondArray.find((bitem) => aitem[prop] === bitem[prop])
        );
        var reduced =  firstArray.filter( aitem => ! secondArray.find ( bitem => aitem[prop] === bitem[prop]) )
        //let arr3 = arr1.map((item, i) => Object.assign({}, item, arr2[i]));
        return reduced.concat(secondArray);
    }