| | |
| | | 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'; |
| | |
| | | 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(); |
| | |
| | | @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; |
| | |
| | | //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); |
| | |
| | | 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; |
| | |
| | | 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); |
| | | } |