Skip to content

Commit 9194409

Browse files
Merge pull request #166 from mickaelgudin/dev
Picklist edit #164
2 parents ea69ca0 + 759ec82 commit 9194409

17 files changed

+254
-37
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ This is generic lighting data table , which is build in lwc.
1212
The customization are done by design attributes.
1313

1414
Main features
15-
The data table has following features.
1615

1716
- Show records for both custom and standard object.
1817
- Add cols as per the fields exist in object in JSON format.
@@ -23,6 +22,11 @@ The data table has following features.
2322
- Configurable actions buttons (for developers, see [Buttons configuration](#buttons-configuration) )
2423
- Sorting by field (Note: sort will not work on search).
2524

25+
Custom Data types (the component extendedDatatable extends lightning:datatable) :
26+
27+
- picklist
28+
29+
2630
## Steps to Customization through Design Attribute
2731

2832
### Design Attribute
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public class IconUtils {
22
public static String getIconName(String sObjectName) {
3-
String iconName;
3+
String iconName = '';
44
List<Schema.DescribeTabSetResult> tabSetDesc = Schema.describeTabs();
55
List<Schema.DescribeTabResult> tabDesc = new List<Schema.DescribeTabResult>();
66
List<Schema.DescribeIconResult> iconDesc = new List<Schema.DescribeIconResult>();
@@ -15,21 +15,18 @@ public class IconUtils {
1515
iconDesc.addAll(tr.getIcons());
1616
} else {
1717
iconName = 'standard:' + sObjectName.toLowerCase();
18+
break;
1819
}
1920
}
2021
}
2122

2223
for (Schema.DescribeIconResult ir : iconDesc) {
2324
if (ir.getContentType() == 'image/svg+xml') {
24-
iconName =
25-
'custom:' +
26-
ir.getUrl()
27-
.substringBetween('custom/', '.svg')
28-
.substringBefore('_');
25+
iconName = 'custom:' + ir.getUrl().substringBetween('custom/', '.svg').substringBefore('_');
2926
break;
3027
}
3128
}
3229

3330
return iconName;
3431
}
35-
}
32+
}

force-app/main/default/classes/IconUtils.cls-meta.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
33
<apiVersion>48.0</apiVersion>
44
<status>Active</status>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public with sharing class PicklistColumnUtils {
2+
public class PicklistValue {
3+
@AuraEnabled public String label;
4+
@AuraEnabled public String value;
5+
6+
public PicklistValue(String label, String value) {
7+
this.label = label;
8+
this.value = value;
9+
}
10+
}
11+
12+
public static List<PicklistValue> getPicklistValues(String objectName, String fieldName) {
13+
List<Schema.PicklistEntry> entries = new List<Schema.PicklistEntry>();
14+
Schema.DescribeSobjectResult objDescription = Schema.describeSObjects(new List<String>{ objectName })[0];
15+
Schema.SObjectField field = objDescription.fields.getMap().get(fieldName);
16+
17+
entries = field.getDescribe().getPickListValues();
18+
entries = getActivePicklistEntries(entries);
19+
20+
List<PicklistValue> picklistValues = new List<PicklistValue>();
21+
22+
for(Schema.PicklistEntry pvalue: entries) {
23+
picklistValues.add(new PicklistValue(pvalue.getLabel(), pvalue.getValue() ) );
24+
}
25+
26+
return picklistValues;
27+
}
28+
29+
private static List<Schema.PicklistEntry> getActivePicklistEntries(List<Schema.PicklistEntry> entries) {
30+
List<Schema.PicklistEntry> activeEntries = new List<Schema.PicklistEntry>();
31+
for (Schema.PicklistEntry entry : entries) {
32+
if (entry.isActive()) {
33+
activeEntries.add(entry);
34+
}
35+
}
36+
return activeEntries;
37+
}
38+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>55.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

force-app/main/default/classes/RelatedList.cls

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ public with sharing class RelatedList {
1414
public Boolean editable;
1515
@AuraEnabled
1616
public Boolean sortable;
17+
@AuraEnabled
18+
public Boolean wrapText = false;
19+
@AuraEnabled
20+
public TypeAttributeColumnJsonWithPicklist typeAttributes = null;
21+
22+
public void setTypeAttributes(List<PicklistColumnUtils.PicklistValue> options) {
23+
this.typeAttributes = new TypeAttributeColumnJsonWithPicklist();
24+
this.typeAttributes.options = options;
25+
this.typeAttributes.fieldName = this.fieldName;
26+
}
27+
}
28+
29+
public class TypeAttributeColumnJsonWithPicklist {
30+
@AuraEnabled public List<PicklistColumnUtils.PicklistValue> options = new List<PicklistColumnUtils.PicklistValue>();
31+
@AuraEnabled public ContextTypeAttributeColumnJsonWithPicklist context = new ContextTypeAttributeColumnJsonWithPicklist();
32+
@AuraEnabled public String fieldName;
33+
}
34+
35+
public class ContextTypeAttributeColumnJsonWithPicklist {
36+
@AuraEnabled public string fieldName = 'Id';
1737
}
1838

1939
public class ListResults {
@@ -65,7 +85,13 @@ public with sharing class RelatedList {
6585
//Check if values predefined in LWC
6686
if (mapPreCols.containsKey(fieldName)) {
6787
mapPreCols = checkOverride(mapPreCols, fieldName, mfields);
88+
89+
//set picklistValues for picklist type
90+
if(mapPreCols.get(fieldName).type == 'picklist') {
91+
RelatedList.initPicklistColumn(mapPreCols, objectName, fieldName);
92+
}
6893
}
94+
6995
}
7096
return new ListResults(
7197
getRecords(soql),
@@ -77,6 +103,12 @@ public with sharing class RelatedList {
77103
throw new AuraHandledException(e.getMessage());
78104
}
79105
}
106+
107+
public static void initPicklistColumn(Map<String, ColumnJson> mapPreCols, String objectName, String fieldName) {
108+
List<PicklistColumnUtils.PicklistValue> picklistValues = PicklistColumnUtils.getPicklistValues(objectName, fieldName);
109+
mapPreCols.get(fieldName).wrapText = true;
110+
mapPreCols.get(fieldName).setTypeAttributes(picklistValues);
111+
}
80112

81113
@AuraEnabled
82114
public static List<SObject> getRecords(String soql) {
@@ -111,16 +143,10 @@ public with sharing class RelatedList {
111143

112144
@AuraEnabled(cacheable=true)
113145
public static Integer countRecords(String objectName, String whereClause) {
114-
String formattedWhere = (String.isBlank(whereClause))
115-
? ''
116-
: ' ' + whereClause;
117-
if (String.isNotEmpty(objectName)) {
118-
return database.countQuery(
119-
'SELECT count() FROM ' +
120-
objectName +
121-
formattedWhere +
122-
' WITH SECURITY_ENFORCED'
123-
);
146+
String formattedWhere = (String.isBlank(whereClause)) ? '' : ' ' + whereClause;
147+
148+
if(String.isNotEmpty(objectName)) {
149+
return database.countQuery('SELECT count() FROM ' +objectName + formattedWhere +' WITH SECURITY_ENFORCED');
124150
}
125151
return 0;
126152
}
@@ -140,9 +166,7 @@ public with sharing class RelatedList {
140166
Boolean editable = mapPreCols.get(fieldName).editable != null
141167
? mapPreCols.get(fieldName).editable
142168
: mfields.get(fieldName).getDescribe().isUpdateable(),
143-
sortable = mapPreCols.get(fieldName).sortable != null
144-
? mapPreCols.get(fieldName).sortable
145-
: mfields.get(fieldName).getDescribe().isSortable();
169+
sortable = true;
146170
//Update Map Json
147171
mapPreCols.get(fieldName).label = label;
148172
mapPreCols.get(fieldName).type = mapFieldToLwcDataType.containskey(type)
@@ -151,5 +175,5 @@ public with sharing class RelatedList {
151175
mapPreCols.get(fieldName).editable = editable;
152176
mapPreCols.get(fieldName).sortable = sortable;
153177
return mapPreCols;
154-
}
155-
}
178+
}
179+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>48.0</apiVersion>
3+
<apiVersion>55.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>

force-app/main/default/classes/RelatedList_Test.cls

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,15 @@ public with sharing class RelatedList_Test {
6969
System.assertEquals(results.size() > 0, true);
7070
Test.stopTest();
7171
}
72-
}
72+
73+
@IsTest
74+
static void initPicklistColumn_Test() {
75+
Map<String, RelatedList.ColumnJson> colsJson = new Map<String, RelatedList.ColumnJson>{'StageName' => new RelatedList.ColumnJson() };
76+
77+
RelatedList.initPicklistColumn(colsJson,
78+
'Opportunity',
79+
'StageName');
80+
81+
System.assert(colsJson.get('StageName').typeAttributes.options.size() >= 0);
82+
}
83+
}

force-app/main/default/classes/RelatedList_Test.cls-meta.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
33
<apiVersion>48.0</apiVersion>
44
<status>Active</status>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template>
2+
<div>
3+
<div if:true={showPicklist}>
4+
<lightning-combobox name="picklist"
5+
data-id="Picklist Combobox"
6+
variant="label-hidden"
7+
value={value}
8+
options={options}
9+
onchange={handleChange}>
10+
</lightning-combobox>
11+
</div>
12+
13+
<div if:false={showPicklist} class="slds-table_edit_container slds-is-relative">
14+
<span class="slds-grid slds-grid_align-spread slds-cell-edit" ondblclick={editPicklist}>
15+
<span class="slds-truncate" title={value}>{value}</span>
16+
<button data-id={context}
17+
class="slds-button slds-button_icon slds-cell-edit__button slds-m-left_x-small"
18+
tabindex="-1"
19+
title="Edit"
20+
name="tes"
21+
onclick={editPicklist}
22+
data-name="myButtonName">
23+
<svg class="slds-button__icon slds-button__icon_hint slds-button__icon_lock slds-button__icon_small slds-button__icon_edit slds-icon slds-icon-text-default slds-icon_xx-small"
24+
aria-hidden="true">
25+
<use xlink:href="/_slds/icons/utility-sprite/svg/symbols.svg?cache=9.37.1#edit"></use>
26+
</svg>
27+
<span class="slds-assistive-text">Edit</span>
28+
</button>
29+
</span>
30+
</div>
31+
</div>
32+
</template>

0 commit comments

Comments
 (0)