useTable

Hooks that handle Table-related logic, like frontend pagination, sort and search。

New hook.

Examples

Basic usage

API

const { pagedData, page, total, search } = useTable(
  data,
  defaultParams?: Object
);

Params

PropertyDescriptionTypeDefault
dataTable data array.array-
defaultParamsSee the defaultParams section below.object{page, sort, search}

defaultParams.page

PropertyDescriptionTypeDefault
indexCurrent page numbernumber1
sizeNumber of data items per pagenumber10

defaultParams.sort

PropertyDescriptionTypeDefault
keySort field.string''
directionsupported sort way, could be ascend, descendstringascend
compareFnSort function for local sort.functiondefaultCompareFn
PropertyDescriptionTypeDefault
textSearch content.string''
isRegTrue if Search content is regx.booleanfalse
keysSearch columns. If not set, will search all columns.array[]

defaultParams

const defaultParams = {
  page: {
    index: 1,
    size: 10,
  },
  search: {
    text: '',
    isReg: false,
    keys: [] as string[],
  },
  sort: {
    key: '',
    direction: 'ascend',
    compareFn: defaultCompareFn,
  },
};
function defaultCompareFn(a: any, b: any) {
  return a < b ? -1 : 1;
}

最近更新: