11/*
2- * Author : Sarvesh
3- * Description : The component to show records of custom/standard of Object as a table.
4- * Production Ready : Yes
5- */
2+ * Author : Sarvesh
3+ * Description : The component to show records of custom/standard of Object as a table.
4+ * Production Ready : Yes
5+ */
66import { LightningElement , wire , track , api } from "lwc" ;
77import { NavigationMixin } from "lightning/navigation" ;
88import { deleteRecord } from "lightning/uiRecordApi" ;
@@ -14,7 +14,7 @@ let cols;
1414const actions = [
1515 { label : "Show details" , name : "show_details" } ,
1616 { label : "Edit" , name : "edit" } ,
17- { label : "Delete" , name : "delete" }
17+ { label : "Delete" , name : "delete" } ,
1818] ;
1919export default class LightningDatatable extends NavigationMixin (
2020 LightningElement
@@ -33,47 +33,61 @@ export default class LightningDatatable extends NavigationMixin(
3333 @track soql ;
3434 @track offSet = 0 ;
3535 @track totalRows = 0 ;
36+ @track error ;
3637
3738 // Do init funtion
3839 connectedCallback ( ) {
39- cols = JSON . parse ( this . columns ) ;
40+ if ( this . columns != null && this . columns != undefined ) {
41+ cols = JSON . parse ( this . columns ) ;
42+ }
4043 cols . push ( {
4144 type : "action" ,
42- typeAttributes : { rowActions : actions }
45+ typeAttributes : { rowActions : actions } ,
4346 } ) ;
4447 this . columns = cols ;
4548 this . buildSOQL ( ) ;
46- countRecords ( { objectName : this . objectName } ) . then ( result => {
49+ countRecords ( { objectName : this . objectName } ) . then ( ( result ) => {
4750 this . totalRows = result ;
4851 } ) ;
4952 this . fetchRecords ( ) ;
5053 }
5154
5255 fetchRecords ( ) {
53- getRecords ( { soql : this . soql } ) . then ( data => {
54- if ( data ) {
55- data . map ( e => {
56- for ( let key in e ) {
57- if ( typeof e [ key ] === "object" ) {
58- for ( let onLevel in e [ key ] ) {
59- e [ key + "." + onLevel ] = e [ key ] [ onLevel ] ;
56+ getRecords ( { soql : this . soql } )
57+ . then ( ( data ) => {
58+ if ( data ) {
59+ data . map ( ( e ) => {
60+ for ( let key in e ) {
61+ if ( typeof e [ key ] === "object" ) {
62+ for ( let onLevel in e [ key ] ) {
63+ e [ key + "." + onLevel ] = e [ key ] [ onLevel ] ;
64+ }
6065 }
6166 }
67+ } ) ;
68+ this . data = data ;
69+ }
70+ } )
71+ . catch ( ( error ) => {
72+ if ( error ) {
73+ this . error = "Unknown error" ;
74+ if ( Array . isArray ( error . body ) ) {
75+ this . error = error . body . map ( ( e ) => e . message ) . join ( ", " ) ;
76+ } else if ( typeof error . body . message === "string" ) {
77+ this . error = error . body . message ;
6278 }
63- } ) ;
64- this . data = data ;
65- } else if ( error ) {
66- }
67- } ) ;
79+ console . log ( "error" , this . error ) ;
80+ }
81+ } ) ;
6882 }
6983
7084 newRecord ( ) {
7185 this [ NavigationMixin . Navigate ] ( {
7286 type : "standard__objectPage" ,
7387 attributes : {
7488 objectApiName : this . objectName ,
75- actionName : "new"
76- }
89+ actionName : "new" ,
90+ } ,
7791 } ) ;
7892 }
7993
@@ -125,7 +139,11 @@ export default class LightningDatatable extends NavigationMixin(
125139 }
126140
127141 get isDisableNext ( ) {
128- return this . offSet + this . limit >= this . totalRows ? true : this . totalRows <= this . limit ? false : false ;
142+ return this . offSet + this . limit >= this . totalRows
143+ ? true
144+ : this . totalRows <= this . limit
145+ ? false
146+ : false ;
129147 }
130148
131149 /*********************************************************************
@@ -143,7 +161,7 @@ export default class LightningDatatable extends NavigationMixin(
143161 . concat ( this . data . slice ( index + 1 ) ) ;
144162 this . showToast ( "Success" , "Record deleted" , "success" ) ;
145163 } )
146- . catch ( error => {
164+ . catch ( ( error ) => {
147165 this . showToast ( "Error deleting record" , error . body . message , "error" ) ;
148166 } ) ;
149167 }
@@ -167,8 +185,8 @@ export default class LightningDatatable extends NavigationMixin(
167185 attributes : {
168186 recordId : row [ "Id" ] ,
169187 objectApiName : this . objectName ,
170- actionName : "view"
171- }
188+ actionName : "view" ,
189+ } ,
172190 } ) ;
173191 }
174192
@@ -178,8 +196,8 @@ export default class LightningDatatable extends NavigationMixin(
178196 attributes : {
179197 recordId : row [ "Id" ] ,
180198 objectApiName : this . objectName ,
181- actionName : "edit"
182- }
199+ actionName : "edit" ,
200+ } ,
183201 } ) ;
184202 }
185203
@@ -189,15 +207,14 @@ export default class LightningDatatable extends NavigationMixin(
189207 soql += this . appendWhere ( ) ;
190208 soql += this . appendLimit ( ) ;
191209 soql += this . appendOffset ( ) ;
192- console . log ( "soql:::" , soql ) ;
193210 this . soql = soql ;
194211 }
195212
196213 appendField ( ) {
197214 let soql = "SELECT Id," ,
198215 col = [ ] ;
199216 if ( cols ) {
200- cols . map ( val => {
217+ cols . map ( ( val ) => {
201218 if ( val . hasOwnProperty ( "fieldName" ) ) {
202219 col . push ( val [ "fieldName" ] ) ;
203220 }
@@ -230,7 +247,7 @@ export default class LightningDatatable extends NavigationMixin(
230247 new ShowToastEvent ( {
231248 title : title ,
232249 message : message ,
233- variant : variant
250+ variant : variant ,
234251 } )
235252 ) ;
236253 }
0 commit comments