Skip to content

Commit

Permalink
fix: 修复crud2默认分页不生效
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao07 committed Oct 11, 2024
1 parent 30d6dc4 commit eaea15a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/amis-editor/src/plugin/CRUD2/BaseCRUD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ export class BaseCRUDPlugin extends BasePlugin {
this.addFeatToToolbar(schema, newCompSchema, 'footer', 'right');
}
form.setValues({
perPage: value !== 'more' ? undefined : schema.perPage,
footerToolbar: schema.footerToolbar,
headerToolbar: schema.headerToolbar
});
Expand Down
35 changes: 30 additions & 5 deletions packages/amis/src/renderers/CRUD2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
isApiOutdated,
isPureVariable,
resolveVariableAndFilter,
parsePrimitiveQueryString
parsePrimitiveQueryString,
JSONTraverse
} from 'amis-core';
import pickBy from 'lodash/pickBy';
import {Html, SpinnerExtraProps} from 'amis-ui';
Expand Down Expand Up @@ -285,7 +286,6 @@ export default class CRUD2 extends React.Component<CRUD2Props, any> {
silentPolling: false,
autoFillHeight: false,
showSelection: true,
perPage: 10,
primaryField: 'id',
parsePrimitiveQuery: true
};
Expand Down Expand Up @@ -350,12 +350,19 @@ export default class CRUD2 extends React.Component<CRUD2Props, any> {
}

componentDidMount() {
const {store, pickerMode, loadType, loadDataOnce, perPage} = this.props;
const {store, pickerMode, loadType, loadDataOnce, maxLoadNum} = this.props;

// 初始化分页
let pagination = loadType && !!loadDataOnce;
let pagination = loadType && !loadDataOnce;
if (pagination) {
// crud2的翻页每页条数是翻页组件里单独配置的
let perPage =
loadType === 'more'
? this.props.perPage || 10
: this.getPaginationPerPage();
store.changePage(store.page, perPage);
} else if (!loadType) {
store.changePage(1, maxLoadNum || 500); // 不分页时默认一次最多查询500条(jsonql)
}

// 初始化筛选条件
Expand Down Expand Up @@ -446,6 +453,24 @@ export default class CRUD2 extends React.Component<CRUD2Props, any> {
clearTimeout(this.timer);
}

@autobind
getPaginationPerPage() {
let perPage = 10;
let {headerToolbar, footerToolbar} = this.props;
JSONTraverse(
{
headerToolbar,
footerToolbar
},
(value: any, key: string, host: any) => {
if (key === 'type' && value === 'pagination' && !isNaN(host?.perPage)) {
perPage = +host.perPage;
}
}
);
return perPage;
}

getParseQueryOptions(props: CRUD2Props) {
const {parsePrimitiveQuery} = props;
type PrimitiveQueryObj = Exclude<
Expand Down Expand Up @@ -503,7 +528,7 @@ export default class CRUD2 extends React.Component<CRUD2Props, any> {
* 加载更多动作处理器
*/
handleLoadMore() {
const {store, perPage} = this.props;
const {store, perPage = 10} = this.props;

store.changePage(store.page + 1, perPage);
this.getData(undefined, undefined, undefined, true);
Expand Down

0 comments on commit eaea15a

Please sign in to comment.