Skip to content

vtSortConfig 排序配置 ​

一个面板,配多列排序:一条条规则(按哪一列 + 升还是降),顺序就是优先级。

装了 vtToolbar 时它会挂成工具栏上的「排序」那颗按钮(工具栏一颗都不自带, 要哪颗装哪个);下面的示例不装工具栏。

示例 ​

微应用尚未挂载。

源码 ​

点击查看源码
ts
import { faker } from '@faker-js/faker';
import { VirtTable, type VirtTableColumn } from '@virt-table/vanilla';
import { vtSortConfig } from '@virt-table/vanilla/plugins';
import '@virt-table/vanilla/plugins/sort-config.css';

interface Row {
  id: number;
  name: string;
  dept: string;
  salary: number;
  score: number;
}

const DEPTS = ['研发', '设计', '销售'];

export function bootstrapTableSortConfig(root: HTMLElement): () => void {
  const columns: VirtTableColumn<Row>[] = [
    { key: 'name', title: '姓名', width: 150, sortable: true },
    { key: 'dept', title: '部门', width: 110, sortable: true },
    { key: 'salary', title: '月薪', width: 110, align: 'right', sortable: true },
    { key: 'score', title: '绩效', width: 100, align: 'right', sortable: true },
    // 这一列明确不参与排序 —— 面板的候选里也不会有它
    { key: 'id', title: 'ID', width: 70, sortable: false },
  ];

  const list: Row[] = Array.from({ length: 400 }, (_, i) => ({
    id: i + 1,
    name: faker.person.fullName(),
    dept: DEPTS[i % DEPTS.length]!,
    salary: faker.number.int({ min: 8000, max: 60000 }),
    score: faker.number.int({ min: 60, max: 100 }),
  }));

  root.innerHTML = `
    <div class="virt-table-controls">
      <button class="virt-table-btn virt-table-btn-primary" id="sort">排序</button>
      <span class="demo-note" id="hint">面板里排的序与点列头是同一份状态:列头的箭头会跟着亮</span>
    </div>
    <div style="width:800px;height:420px;" class="demo-container" id="c"></div>`;
  const container = root.querySelector('#c') as HTMLElement;
  const hint = root.querySelector('#hint') as HTMLElement;

  const table = new VirtTable<Row>(container, {
    list,
    columns,
    itemKey: 'id',
    estimatedSize: 40,
    buffer: 6,
    border: true,
    /*
     * **多列排序**:不开这个的话面板只给一条规则并说明原因 ——
     * `setState` 在单列模式下会把多余的规则静默截断,界面上先说清比让人白配一条好。
     */
    sortMode: 'multiple',
    onSortChange: (sort) => {
      hint.textContent = sort.length
        ? `${sort.length} 条规则:${sort.map((s) => `${s.colKey} ${s.order}`).join(',')}`
        : '面板里排的序与点列头是同一份状态:列头的箭头会跟着亮';
    },
    // 没装 vtToolbar:面板由下面那颗按钮唤出
    plugins: [vtSortConfig()],
  });

  (root.querySelector('#sort') as HTMLButtonElement).onclick = (e) =>
    table.toggleSortConfig(e.currentTarget as HTMLElement);

  return () => {
    table.destroy();
    root.innerHTML = '';
  };
}

说明 ​

ts
new VirtTable(el, {
  list, columns, itemKey: 'id', estimatedSize: 40,
  // 不开这个的话面板只给一条规则,并说明原因
  sortMode: 'multiple',
  plugins: [vtSortConfig()],
});

table.toggleSortConfig(btnEl);
选项作用
toolbar装了 vtToolbar 时要不要占那条按钮条的位置,默认 true
texts文案覆盖,不给则跟随 setLocale()

两条如实说出来的边界:

  • sortable: false 的列不进候选 —— 那是使用方明确说过「这列不排序」,面板不该给绕过去的入口。 反过来没写 sortable 的列一律可选(sort() 对任何列都有效,而这个面板的意义正是「按任何一列排」)。
  • 没开 sortMode: 'multiple' 时只允许一条规则,面板不给「添加」并说明原因 —— setState 在单列模式下会静默截断成第一条,界面上先说清比让人加了又看它消失好。

落地走 setState({ sort }),所以与点列头排序是同一份状态:在面板里排的序,列头上的箭头也会亮。

相关 API ​

字段 / 方法作用详见
toggleSortConfig(anchor?)开合面板插件机制
getSortState() / setState({ sort })读写排序状态列排序
sortMode / col.sortable多列排序开关 / 某列参不参与表格配置
onSortChange排序变化回调表格配置

全量字段见 表格配置 / 列配置 / 实例方法。