Salesforce Apex 自定义排序类(二)

2022-05-30 09:29:58 浏览数 (1)

当Listview中的项目不是从Object中取得,而是自定义时,修改排序类,进行商品名排序。

OpportunityListViewController.cls

代码语言:javascript复制
public with sharing class OpportunityListViewController {
    @AuraEnabled(cacheable=true)
    public static List<OpportunityWrapper> getOpportunityListView(){
        List<OpportunityWrapper> inputWappers = new List<OpportunityWrapper>();
        OpportunityWrapper wapper1 = new OpportunityWrapper();
        wapper1.name = '案件1';
        wapper1.productName = 'C 商品';
        wapper1.stageName = 'Prospecting';
        wapper1.amount = '500';
        inputWappers.add(wapper1);

        OpportunityWrapper wapper2 = new OpportunityWrapper();
        wapper1.name = '案件2';
        wapper2.productName = 'A 商品';
        wapper2.stageName = 'Prospecting';
        wapper2.amount = '400';
        inputWappers.add(wapper2);
        OpportunityWrapper wapper3 = new OpportunityWrapper();
        wapper1.name = '案件3';
        wapper3.productName = 'B 商品';
        wapper3.stageName = 'Prospecting';
        wapper3.amount = '300';
        inputWappers.add(wapper3);
        return inputWappers;
    }
    public class OpportunityWrapper {
        @AuraEnabled
        public String id;
        @AuraEnabled
        public String idLink;
        // 案件名
        @AuraEnabled
        public String name;
        @AuraEnabled
        public String productName;
        // フェーズ
        @AuraEnabled
        public String stageName;
        // 見込み保険料
        @AuraEnabled
        public String amount;
        // 完了予定日
        @AuraEnabled
        public Date closeDate;
    }
}

排序类修改方法

SortableOpportunityWrapper.cls

代码语言:javascript复制
public class SortableOpportunityWrapper implements Comparable {
    public OpportunityListViewController.OpportunityWrapper wrapper;
    public SortableOpportunityWrapper(OpportunityListViewController.OpportunityWrapper t) {
        wrapper = t;
    }
    public Integer compareTo(Object compareTo) {
        SortableOpportunityWrapper compareToWrapper = (SortableOpportunityWrapper) compareTo;
        Integer returnValue = 0;
        if (wrapper.productName < compareToWrapper.wrapper.productName) {
            returnValue = -1;
        }
        if (wrapper.productName > compareToWrapper.wrapper.productName) {
            returnValue = 1;
        }
        return returnValue;
    }
}

调用排序类,进行商品名重新排序

OpportunityListViewController.cls

代码语言:javascript复制
public with sharing class OpportunityListViewController {
    @AuraEnabled(cacheable=true)
    public static List<OpportunityWrapper> getOpportunityListView(){
        List<OpportunityWrapper> inputWappers = new List<OpportunityWrapper>();
        OpportunityWrapper wapper1 = new OpportunityWrapper();
        wapper1.name = '案件1';
        wapper1.productName = 'C 商品';
        wapper1.stageName = 'Prospecting';
        wapper1.amount = '500';
        inputWappers.add(wapper1);

        OpportunityWrapper wapper2 = new OpportunityWrapper();
        wapper1.name = '案件2';
        wapper2.productName = 'A 商品';
        wapper2.stageName = 'Prospecting';
        wapper2.amount = '400';
        inputWappers.add(wapper2);
        OpportunityWrapper wapper3 = new OpportunityWrapper();
        wapper1.name = '案件3';
        wapper3.productName = 'B 商品';
        wapper3.stageName = 'Prospecting';
        wapper3.amount = '300';
        inputWappers.add(wapper3);

        List<OpportunityWrapper> wappersSortResult = new List<OpportunityWrapper>();
        List<SortableOpportunityWrapper> wrapperSortList = new List<SortableOpportunityWrapper>();
        for (OpportunityWrapper wapperItem : inputWappers) {
            wrapperSortList.add(new SortableOpportunityWrapper(wapperItem));
        }
        wrapperSortList.sort();

        for (SortableOpportunityWrapper ta : wrapperSortList) {
            wappersSortResult.add(ta.wrapper);
        }

        return wappersSortResult;
    }
    public class OpportunityWrapper {
        @AuraEnabled
        public String id;
        @AuraEnabled
        public String idLink;
        // 案件名
        @AuraEnabled
        public String name;
        @AuraEnabled
        public String productName;
        // フェーズ
        @AuraEnabled
        public String stageName;
        // 見込み保険料
        @AuraEnabled
        public String amount;
        // 完了予定日
        @AuraEnabled
        public Date closeDate;
    }
}

0 人点赞