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

Salesforce Apex 自定义排序类(一)

原创
作者头像
repick
修改于 2022-05-25 11:31:11
修改于 2022-05-25 11:31:11
73200
代码可运行
举报
文章被收录于专栏:SalesforceSalesforce
运行总次数:0
代码可运行

使用【lightning-datatable】表示ListView数据时,当表示Opportunity表中Amount项目,需要排序功能时,可以直接在SOQL中使用ORDER BY进行排序,也可以在自定义Apex中实现。

1.自定义排序类

SortableOpportunityWrapper

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class SortableOpportunityWrapper implements Comparable {
    public Opportunity opp;
    public SortableOpportunityWrapper(Opportunity t) {
        opp = t;
    }
    public Integer compareTo(Object compareTo) {
        SortableOpportunityWrapper compareToOpportunity = (SortableOpportunityWrapper) compareTo;
        Integer returnValue = 0;

        if (opp.Amount < compareToOpportunity.opp.Amount) {
            returnValue = -1;
        }
        if (opp.Amount > compareToOpportunity.opp.Amount) {
            returnValue = 1;
        }
        return returnValue;
    }
}

2.调用排序类进行排序

OpportunityListViewController.cls

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public with sharing class OpportunityListViewController {
    @AuraEnabled(cacheable=true)
    public static List<OpportunityWrapper> getOpportunityListView(){
        List<OpportunityWrapper> inputWappers = new List<OpportunityWrapper>();

        List<Opportunity> resultList = [SELECT Id,
                                            Name,
                                            StageName,
                                            Amount,
                                            CloseDate,
                                            RecordTypeId
                                            FROM Opportunity
                                            ORDER BY LastModifiedDate DESC
                                            LIMIT 10
                                            ];

        List<SortableOpportunityWrapper> oppSortList = new List<SortableOpportunityWrapper>();
        for (Opportunity t : resultList) {
            oppSortList.add(new SortableOpportunityWrapper(t));
        }
        oppSortList.sort();
        for (SortableOpportunityWrapper opSort : oppSortList) {
            OpportunityWrapper wapper = new OpportunityWrapper();
            wapper.id = opSort.opp.Id;
            wapper.idLink = '/opportunity/'+ opSort.opp.Id;
            wapper.name = opSort.opp.Name;
            wapper.stageName = opSort.opp.StageName;
            wapper.amount = String.valueOf(opSort.opp.Amount);
            wapper.closeDate = opSort.opp.CloseDate;
            inputWappers.add(wapper);
        }

        return inputWappers;
    }
    public class OpportunityWrapper {
        @AuraEnabled
        public String id;
        @AuraEnabled
        public String idLink;
        @AuraEnabled
        public String name;
        @AuraEnabled
        public String stageName;
        @AuraEnabled
        public String amount;
        @AuraEnabled
        public Date closeDate;
    }
}

opportunityListView.html

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<template>
    <div class="slds-card__body">
        <lightning-datatable
            key-field="id"
            data={records}
            show-row-number-column
            row-number-offset={rowOffset}
            hide-checkbox-column
            columns={columns}
            >
        </lightning-datatable>
        <template if:false={loaded}>
            <lightning-spinner alternative-text="Loading"></lightning-spinner>
        </template>
    </div>
</template>

opportunityListView.js

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import { LightningElement, track, wire } from 'lwc';
import getOpportunityListView from '@salesforce/apex/OpportunityListViewController.getOpportunityListView';

const columns = [
    { label: '案件名', fieldName: 'idLink', type: 'url', sortable: true, typeAttributes: { label: { fieldName: 'name' }, target: '_self' }, initialWidth: 300 },
    { label: 'フェーズ', fieldName: 'stageName', type: 'text', editable: true },
    { label: '保険料', fieldName: 'amount', type: 'currency' },
    { label: '完了予定日', fieldName: 'closeDate', type: 'date' },
    ];

export default class OpportunityListView extends LightningElement {

    @track columns = columns;
    @track records;
    @track prospectRecords;

    @track wiredRecordList;
    @track error;
    @track rowOffset = 0;
    @track loaded = false;

    @wire(getOpportunityListView)
    recordList(result) {
        this.wiredRecordList = result;
        if (result.data) {
            this.records = result.data;
            this.error = undefined;
            this.loaded = true;
        } else if (result.error) {
            this.error = result.error;
            this.records = [];
        }
    }
}

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

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

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

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Salesforce Apex 自定义排序类(二)
当Listview中的项目不是从Object中取得,而是自定义时,修改排序类,进行商品名排序。
repick
2022/05/30
5580
Salesforce Apex 自定义排序类(二)
Salesforce学习 Lwc(十三)【InLineEdit】更新数据的三种方式
前边讲过如何使用【lightning-datatable】表示数据,以及一些基本样式,今天讲解在【InLineEdit】模式下,通过Lwc方法和Apex自定义方法进行编辑后的数据更新。
repick
2021/01/11
1K0
Salesforce学习 Lwc(十三)【InLineEdit】更新数据的三种方式
Salesforce LWC学习(四十六) 自定义Datatable实现cell onclick功能
本篇参考:https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable
Zero-Zhang
2023/12/06
3220
Salesforce LWC学习(四十六) 自定义Datatable实现cell onclick功能
Salesforce LWC学习(三十八) lwc下如何更新超过1万的数据
背景: 今天项目组小伙伴问了一个问题,如果更新数据超过1万条的情况下,有什么好的方式来实现呢?我们都知道一个transaction只能做10000条DML数据操作,那客户的操作的数据就是超过10000条的情况下,我们就只能搬出来salesforce government limitation进行拒绝吗?这显然也是不友好的行为。
Zero-Zhang
2022/03/22
7521
Salesforce LWC学习(三十八) lwc下如何更新超过1万的数据
Salesforce lightning datatable 每行表示Link项目
使用LightningDatatable做成的ListView时,有时需要自定义Link项目,例如需要Link式的行删除事件,当点击消除Link时,消除当前行数据,如下
repick
2022/05/20
6130
Salesforce lightning datatable 每行表示Link项目
Salesforce How To Refresh Page Data in Lightning Web Component(一)
Lightning Web组件中通常使用wire取得数据,当条件发生变更时才会刷新,JS中提供另一种方法【refreshApex()】来刷新页面。
repick
2022/04/29
6910
Salesforce How To Refresh Page Data in Lightning Web Component(一)
Salesforce学习 Lwc(十五)【Picklist项目的Label值取得方法】
几乎每个Object中都有Picklist类型的项目,实际开发过程中会遇到画面上需要显示当前Picklist的值,利用Lwc如何开发呢,下边通过简单的例子说明一下。
repick
2021/03/10
1K0
Salesforce学习 Lwc(十五)【Picklist项目的Label值取得方法】
Salesforce学习 Lwc(十)【lightning-datatable】
上一篇详细讲解了增删改查的初期数据取得和更新操作,还有一种场景是我们经常遇到的,就是ListView,在Lightning画面中可以创建一些标准ListView,但毕竟标准的东西有自己的限制,这样我们就可以自定义开发,今天主要讲解如何使用Lwc自定义LIstView。
repick
2020/12/30
1.2K0
Salesforce How To Refresh Page Data in Lightning Web Component(二)
image.png 删除处理同样可以利用refreshApex方法进行页面刷新 handleDeleteClick() { deleteRecord(this.selectedRecord) .then(() => { refreshApex(this.wiredRecordList); }) .catch(error => { }) } 效果展示: image.png image.png basicDatatable.htm
repick
2022/04/29
4830
Salesforce How To Refresh Page Data in Lightning Web Component(二)
Salesforce 如何实现Listview表示项目的画面迁移
image.png 如下当点击Link名称时,如何实现迁移到详细画面 image.png 1.首先在Apex中添加画面迁移用的项目【idLink】 MC_ContactListViewController.cls public with sharing class MC_ContactListViewController { @AuraEnabled(cacheable=true) public static List<ContactWrapper> getContactListView(B
repick
2022/05/03
5920
Salesforce 如何实现Listview表示项目的画面迁移
Salesforce How To Refresh Page Data in Lightning Web Component(三)
使用Lwc打开一个新的浏览器窗口,当关闭窗口时也可以进行刷新操作,如下,点击加号按钮,打开登录画面,当登录成功时刷新ListView列表。
repick
2022/05/03
4640
Salesforce How To Refresh Page Data in Lightning Web Component(三)
Salesforce学习 Lwc(十八)【如何自定义开发关联List】
项目中,我们经常会用到关联List,标准ListView的做成这里就不多说了,今天我们使用Lwc自定义开发一个关联List,完成之后的效果请看下图,开发主要分为两部分,第一部分是使用【lightning-datatable】标签做成ListView,第二部分是使用【lightning-record-form】标签做成表单。
repick
2021/03/17
8200
Salesforce学习 Lwc(十八)【如何自定义开发关联List】
Salesforce LWC学习(三十五) 使用 REST API实现不写Apex的批量创建/更新数据
https://developer.salesforce.com/docs/atlas.en-us.224.0.api_rest.meta/api_rest/resources_composite_composite.htm
Zero-Zhang
2021/07/08
2.3K1
Salesforce lightning datatable inline editing using Lwc
自定义的ListView中项目需要直接编辑的情况下,【lightning-datatable】组件也提供相应方法
repick
2022/05/05
5150
Salesforce lightning datatable inline editing using Lwc
Salesforce Lightning Data Table With Row Actions(二)
下边做成删除子画面,编辑子画面,分别进行消除和编辑操作,如下,点击Edit时,打开编辑子画面进行数据更新,点击Delete时,打开消除子画面进行数据消除。
repick
2022/05/10
7180
Salesforce Lightning Data Table With Row Actions(二)
Salesforce Lightning Data Table With Row Actions(一)
自定义的ListView中项目需要RowAction的情况下,【lightning-datatable】组件也提供相应方法
repick
2022/05/05
4910
Salesforce Lightning Data Table With Row Actions(一)
Salesforce LWC学习(十八) datatable展示 image
https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentation
Zero-Zhang
2020/06/29
1.5K0
Salesforce LWC学习(三十) lwc superbadge项目实现
本篇参考:https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist
Zero-Zhang
2020/12/29
1.7K0
Salesforce LWC学习(三十三) lightning-datatable 翻页bug处理
本来lightning-datatable这种标签,基本上任何的项目都会用到而且很精通,所以当时感觉没有太大的单独一篇写的必要,在Salesforce LWC学习(三十) lwc superbadge项目实现 中也有使用这个标签的demo,所以有类似需要的小伙伴参考一下也可以照猫画虎搞定需求。项目中遇见了两个datatable的问题,解决以后感觉有必要写一下,后期遇见这种坑的小伙伴可以快速对应。话不多说,先弄一个简单的分页效果的UI,UI很丑,旨在实现功能。
Zero-Zhang
2021/03/27
1K0
Salesforce Lwc lightning-datatable标签下,隐藏Header的Action事件
以下是使用Lwc的【lightning-datatable】标签做成的ListView,只需要自定义Title和取得相关数据。
repick
2022/04/29
6760
Salesforce Lwc lightning-datatable标签下,隐藏Header的Action事件
推荐阅读
相关推荐
Salesforce Apex 自定义排序类(二)
更多 >
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验