binxie
2023-06-26 dd004276162a2bf9d042ff0aaa569dc30a95d827
force-app/main/default/lwc/lwcDatatableUtility/lwcDatatableUtility.js
@@ -11,25 +11,24 @@
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';
@@ -84,7 +83,8 @@
            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,22 +114,24 @@
    @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;
@@ -177,7 +179,11 @@
                //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);
@@ -193,9 +199,13 @@
        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;                
@@ -211,26 +221,40 @@
        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);
    }