1
1
/*
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
+ */
6
6
import { LightningElement , wire , track , api } from "lwc" ;
7
7
import { NavigationMixin } from "lightning/navigation" ;
8
8
import { deleteRecord } from "lightning/uiRecordApi" ;
@@ -14,7 +14,7 @@ let cols;
14
14
const actions = [
15
15
{ label : "Show details" , name : "show_details" } ,
16
16
{ label : "Edit" , name : "edit" } ,
17
- { label : "Delete" , name : "delete" }
17
+ { label : "Delete" , name : "delete" } ,
18
18
] ;
19
19
export default class LightningDatatable extends NavigationMixin (
20
20
LightningElement
@@ -33,47 +33,61 @@ export default class LightningDatatable extends NavigationMixin(
33
33
@track soql ;
34
34
@track offSet = 0 ;
35
35
@track totalRows = 0 ;
36
+ @track error ;
36
37
37
38
// Do init funtion
38
39
connectedCallback ( ) {
39
- cols = JSON . parse ( this . columns ) ;
40
+ if ( this . columns != null && this . columns != undefined ) {
41
+ cols = JSON . parse ( this . columns ) ;
42
+ }
40
43
cols . push ( {
41
44
type : "action" ,
42
- typeAttributes : { rowActions : actions }
45
+ typeAttributes : { rowActions : actions } ,
43
46
} ) ;
44
47
this . columns = cols ;
45
48
this . buildSOQL ( ) ;
46
- countRecords ( { objectName : this . objectName } ) . then ( result => {
49
+ countRecords ( { objectName : this . objectName } ) . then ( ( result ) => {
47
50
this . totalRows = result ;
48
51
} ) ;
49
52
this . fetchRecords ( ) ;
50
53
}
51
54
52
55
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
+ }
60
65
}
61
66
}
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 ;
62
78
}
63
- } ) ;
64
- this . data = data ;
65
- } else if ( error ) {
66
- }
67
- } ) ;
79
+ console . log ( "error" , this . error ) ;
80
+ }
81
+ } ) ;
68
82
}
69
83
70
84
newRecord ( ) {
71
85
this [ NavigationMixin . Navigate ] ( {
72
86
type : "standard__objectPage" ,
73
87
attributes : {
74
88
objectApiName : this . objectName ,
75
- actionName : "new"
76
- }
89
+ actionName : "new" ,
90
+ } ,
77
91
} ) ;
78
92
}
79
93
@@ -125,7 +139,11 @@ export default class LightningDatatable extends NavigationMixin(
125
139
}
126
140
127
141
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 ;
129
147
}
130
148
131
149
/*********************************************************************
@@ -143,7 +161,7 @@ export default class LightningDatatable extends NavigationMixin(
143
161
. concat ( this . data . slice ( index + 1 ) ) ;
144
162
this . showToast ( "Success" , "Record deleted" , "success" ) ;
145
163
} )
146
- . catch ( error => {
164
+ . catch ( ( error ) => {
147
165
this . showToast ( "Error deleting record" , error . body . message , "error" ) ;
148
166
} ) ;
149
167
}
@@ -167,8 +185,8 @@ export default class LightningDatatable extends NavigationMixin(
167
185
attributes : {
168
186
recordId : row [ "Id" ] ,
169
187
objectApiName : this . objectName ,
170
- actionName : "view"
171
- }
188
+ actionName : "view" ,
189
+ } ,
172
190
} ) ;
173
191
}
174
192
@@ -178,8 +196,8 @@ export default class LightningDatatable extends NavigationMixin(
178
196
attributes : {
179
197
recordId : row [ "Id" ] ,
180
198
objectApiName : this . objectName ,
181
- actionName : "edit"
182
- }
199
+ actionName : "edit" ,
200
+ } ,
183
201
} ) ;
184
202
}
185
203
@@ -189,15 +207,14 @@ export default class LightningDatatable extends NavigationMixin(
189
207
soql += this . appendWhere ( ) ;
190
208
soql += this . appendLimit ( ) ;
191
209
soql += this . appendOffset ( ) ;
192
- console . log ( "soql:::" , soql ) ;
193
210
this . soql = soql ;
194
211
}
195
212
196
213
appendField ( ) {
197
214
let soql = "SELECT Id," ,
198
215
col = [ ] ;
199
216
if ( cols ) {
200
- cols . map ( val => {
217
+ cols . map ( ( val ) => {
201
218
if ( val . hasOwnProperty ( "fieldName" ) ) {
202
219
col . push ( val [ "fieldName" ] ) ;
203
220
}
@@ -230,7 +247,7 @@ export default class LightningDatatable extends NavigationMixin(
230
247
new ShowToastEvent ( {
231
248
title : title ,
232
249
message : message ,
233
- variant : variant
250
+ variant : variant ,
234
251
} )
235
252
) ;
236
253
}
0 commit comments