跳至主要内容

setSort()

描述

按指定参数对当前项目的任务进行排序

用法

setSort({
by?: string | ((task: object) => string),
dir?: "asc" | "desc",
tree?: boolean
}): void;

参数

  • by - (可选)搜索条件(任务属性的键名,或返回字符串的搜索函数)
  • dir - (可选)排序方向:"asc""desc"
  • tree - (可选)启用/禁用子任务排序;默认为 false

示例

const { ToDo, Toolbar } = todo;

const list = new ToDo("#root", {
tasks: [
{ id: "a", text: "A" },
{ id: "ac", text: "C", parent: "a" },
{ id: "ad", text: "D", parent: "a" },
{ id: "aa", text: "A", parent: "a" },
{ id: "ab", text: "B", parent: "a" },
{ id: "c", text: "C" },
{ id: "d", text: "D" },
{ id: "b", text: "B" },
],
});

const toolbar = new Toolbar("#toolbar", {
api: list.api,
});

// 按 "text" 参数升序排列任务
list.setSort({
by: task => task.id, // 或 by: "text"
dir: "asc",
// tree: true // 启用树形排序,默认为 false
});

变更日志: 在 v1.1 中新增

相关文章: 任务的排序与过滤