前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Salesforce Apex 自定义排序类(二)

Salesforce Apex 自定义排序类(二)

原创
作者头像
repick
发布2022-05-30 09:29:58
5260
发布2022-05-30 09:29:58
举报
文章被收录于专栏:Salesforce

当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;
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 排序类修改方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档