diff options
Diffstat (limited to 'sdnr/wt/odlux/framework/src/components/material-table/utilities.ts')
-rw-r--r-- | sdnr/wt/odlux/framework/src/components/material-table/utilities.ts | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/sdnr/wt/odlux/framework/src/components/material-table/utilities.ts b/sdnr/wt/odlux/framework/src/components/material-table/utilities.ts index 43c3b5e35..6e8902c07 100644 --- a/sdnr/wt/odlux/framework/src/components/material-table/utilities.ts +++ b/sdnr/wt/odlux/framework/src/components/material-table/utilities.ts @@ -27,7 +27,7 @@ export interface IExternalTableState<TData> { orderBy: string | null; selected: any[] | null; rows: TData[]; - rowCount: number; + total: number; page: number; rowsPerPage: number; loading: boolean; @@ -68,13 +68,13 @@ export function createExternal<TData>(callback: DataCallback<TData>, selectState } class SetPreFilterChangedAction extends TableAction { - constructor(public preFilter: {[key: string]: string}) { + constructor(public preFilter: { [key: string]: string }) { super(); } } class SetFilterChangedAction extends TableAction { - constructor (public filter: { [key: string]: string }) { + constructor(public filter: { [key: string]: string }) { super(); } } @@ -92,7 +92,7 @@ export function createExternal<TData>(callback: DataCallback<TData>, selectState } class SetResultAction extends TableAction { - constructor(public result: { page: number, rowCount: number, rows: TData[] }) { + constructor(public result: { page: number, total: number, rows: TData[] }) { super(); } } @@ -105,7 +105,7 @@ export function createExternal<TData>(callback: DataCallback<TData>, selectState orderBy: null, selected: null, rows: [], - rowCount: 0, + total: 0, page: 0, rowsPerPage: 10, loading: false, @@ -126,14 +126,14 @@ export function createExternal<TData>(callback: DataCallback<TData>, selectState ...state, loading: false, rows: action.result.rows, - rowCount: action.result.rowCount, + total: action.result.total, page: action.result.page, } } else if (action instanceof RequestSortAction) { state = { ...state, loading: true, - orderBy : state.orderBy === action.orderBy && state.order === 'desc' ? null : action.orderBy , + orderBy: state.orderBy === action.orderBy && state.order === 'desc' ? null : action.orderBy, order: state.orderBy === action.orderBy && state.order === 'asc' ? 'desc' : 'asc', } } else if (action instanceof SetShowFilterAction) { @@ -176,9 +176,23 @@ export function createExternal<TData>(callback: DataCallback<TData>, selectState const reloadAction = (dispatch: Dispatch, getAppState: () => IApplicationStoreState) => { dispatch(new RefreshAction()); const ownState = selectState(getAppState()); - const filter = { ...ownState.preFilter, ...(ownState.showFilter && ownState.filter || {})}; - Promise.resolve(callback(ownState.page, ownState.rowsPerPage, ownState.orderBy, ownState.order, filter )).then(result => { - dispatch(new SetResultAction(result)); + const filter = { ...ownState.preFilter, ...(ownState.showFilter && ownState.filter || {}) }; + Promise.resolve(callback(ownState.page, ownState.rowsPerPage, ownState.orderBy, ownState.order, filter)).then(result => { + + if (ownState.page > 0 && ownState.rowsPerPage * ownState.page > result.total) { //if result is smaller than the currently shown page, new search and repaginate + + let newPage = Math.floor(result.total / ownState.rowsPerPage); + + Promise.resolve(callback(newPage, ownState.rowsPerPage, ownState.orderBy, ownState.order, filter)).then(result1 => { + dispatch(new SetResultAction(result1)); + }); + + + } else { + dispatch(new SetResultAction(result)); + } + + }).catch(error => new AddErrorInfoAction(error)); }; @@ -186,8 +200,8 @@ export function createExternal<TData>(callback: DataCallback<TData>, selectState return { onPreFilterChanged: (preFilter: { [key: string]: string }) => { dispatch(new SetPreFilterChangedAction(preFilter)); - (!skipRefresh) && dispatch(reloadAction); - } + (!skipRefresh) && dispatch(reloadAction); + } }; } @@ -236,7 +250,7 @@ export function createExternal<TData>(callback: DataCallback<TData>, selectState const createProperties = (state: IApplicationStoreState) => { return { ...selectState(state) - } + } } return { |