Bỏ qua, đến nội dung

@digiforce-nc/plugin-field-sort ​

Plugin cung cấp kiểu field sắp xếp — cho phép người dùng thay đổi thứ tự bản ghi bằng drag & drop, sử dụng LockManager đảm bảo atomic max+1 và action handler move để reorder.

Plugin này làm gì? ​

Hãy hình dung: bạn cần sắp xếp thủ công menu items, bước quy trình, hoặc danh sách ưu tiên. Plugin này thêm cột sort (BIGINT) vào collection, tự động gán giá trị max+1 khi tạo mới, và cung cấp action move để di chuyển bản ghi lên/xuống hoặc đến vị trí cụ thể — có hỗ trợ scope field để sắp xếp theo nhóm.

Ba nhiệm vụ chính ​

#Nhiệm vụChi tiết
1Auto-assign sortKhi tạo bản ghi, tự gán max(sort) + 1 qua LockManager
2Move actionAction handler cho phép di chuyển bản ghi đến vị trí mới
3Scope sortingSắp xếp trong phạm vi nhóm (ví dụ: sort theo category)

Kiến trúc ​

Tổng quan ​

TầngVai tròThành phần
ClientDrag & drop UI, hiển thị thứ tựSortFieldWidget
ServerXử lý move, tính sort value mớiAction move, LockManager
DatabaseLưu giá trị sort (BIGINT)Cột sort trên collection

Luồng tạo bản ghi mới ​

Luồng move (reorder) ​

Field type sort ​

Thuộc tínhGiá trị
DB column typeBIGINT
Default valuemax(sort) + 1 (trong scope nếu có)
Scope fieldOptional — field để nhóm sort (ví dụ: categoryId)

LockManager & Scope sorting ​

Plugin sử dụng @digiforce-nc/lock-manager để đảm bảo atomic operations. Khi cấu hình scope field, mỗi nhóm có chuỗi sort riêng biệt — ví dụ tasks trong category "Phát triển" có sort 1,2,3 độc lập với "Kiểm thử".

Action handler: move ​

Tham sốKiểuMô tả
sourceIdnumber/stringID bản ghi cần di chuyển
targetIdnumber/stringID bản ghi đích (chèn trước hoặc sau)
methodstringinsertBefore hoặc insertAfter

Ví dụ sử dụng ​

Cấu hình field sort ​

typescript
const sortField = {
  name: 'sort',
  type: 'sort',
  interface: 'sort',
  scopeKey: 'categoryId',
};

Gọi action move ​

typescript
await api.resource('tasks').move({
  sourceId: 10,
  targetId: 3,
  method: 'insertBefore',
});

Thành phần client ​

Thành phầnMô tả
SortFieldInterfaceĐịnh nghĩa interface cho field sort
SortFieldWidgetWidget hiển thị giá trị sort (thường ẩn, dùng cho drag handle)

Dependencies ​

PackageVai trò
@digiforce-nc/serverServer framework — action handler
@digiforce-nc/databaseDatabase ORM — đăng ký field type
@digiforce-nc/actionsAction context (move)
@digiforce-nc/lock-managerĐảm bảo atomic operations
@digiforce-nc/clientClient framework — đăng ký component

Mục lục chi tiết ​