Customize Order
Previous
Customize Cell Size
Next
Custom Collapse/Expand Nodes
Loading...
Although, S2
tables have default sort operations and advanced sort functionality in React Header
component.
But in some business scenarios, we still need custom sorting, which we divide into three parts: custom sorting icon
, custom sorting tooltip
, and custom sorting operations . Next, I will take you to realize the custom sorting function as shown in the 👇 animation.
Related chapters: custom icon
const s2Options = {// 关闭默认 iconshowDefaultHeaderActionIcon: false,...}
const s2Options = {// 自定义 iconcustomSVGIcons: [{name: 'customKingIcon',src: 'https://gw.alipayobjects.com/zos/bmw-prod/f44eb1f5-7cea-45df-875e-76e825a6e0ab.svg',},],...}
const s2Options = {// 配置 icon 展示位置headerActionIcons: [{// 选择 icon, 可以是 S2 自带的,也可以是自定义的 iconicons: [ 'customKingIcon' ],// 通过 belongsCell + displayCondition 设置 icon 的展示位置belongsCell: 'colCell',displayCondition: (meta) => meta.level === 2,...}],...}
before | after |
---|---|
![]() | ![]() |
Related chapters: headerActionIcons configuration instructions
tooltip
is openconst s2Options = {tooltip: {enable: true,},...}
tooltip
display after the icon
is clickedconst items = [{ key: SortMethodType.none, label: '不排序' },{ key: SortMethodType.asc, label: '升序', icon: 'GroupAsc' },{ key: SortMethodType.desc, label: '降序', icon: 'GroupDesc' },{ key: SortMethodType.custom, label: '自定义排序', icon: 'Trend' },];const s2Options = {// 设置自定义 `icon` 的展示条件headerActionIcons: [{// 选择 icon, 可以是 S2 自带的,也可以是自定义的 iconicons: [ 'customKingIcon' ],// 通过 belongsCell + displayCondition 设置 icon 的展示位置belongsCell: 'colCell',displayCondition: (meta) => meta.level === 2,// icon 点击之后的执行函数onClick: (props) => {const { meta, event } = props;const operator = {// 配置 tooltip 中展示的内容menu: {items},};// 自定义 tooltip 配置,展示 toolTipmeta.spreadsheet.showTooltipWithInfo(event, [], {operator,onlyShowCellText: true,onlyShowOperator: true,});},},],...}
before | after |
---|---|
![]() | ![]() |
Related chapter: Custom sorting
// 执行自定义排序回调const handleSortCallback = (meta, key) => {if (key === SortMethodType.custom) {const sortParams = [{ sortFieldId: 'type', sortBy: [ '办公用品', '家具' ] },{ sortFieldId: 'city', sortMethod: 'ASC' },];setSortParams(sortParams)console.log('可以在这里实现你手动排序的交互和逻辑哟', sortParams)} else {// 使用 S2 提供的组内排序方式meta.spreadsheet.groupSortByMethod(key, meta);}}const s2Options = {// 设置自定义 `icon` 的展示条件headerActionIcons: [{onClick: (props) => {const { meta, event } = props;const operator = {onClick: ({ key }) => {// 执行自定义排序回调handleSortCallback(meta, key);meta.spreadsheet.hideTooltip();},menu: {items},};meta.spreadsheet.showTooltipWithInfo(event, [], {operator,onlyShowCellText: true,onlyShowOperator: true,});},...}],...}
Click Custom Sort, and the table will be displayed according to the sort we set.
Categories are manually sorted: [Office Supplies, Furniture], cities 🏙 are arranged in groups in ascending alphabetical order.