修复 picker 没有保持用户选择顺序问题

This commit is contained in:
2betop 2020-04-14 14:55:02 +08:00
parent 160848fa22
commit 40d35e3010

View File

@ -26,6 +26,7 @@ import {evalExpression, filter} from '../utils/tpl';
import {isValidApi, buildApi, isEffectiveApi} from '../utils/api'; import {isValidApi, buildApi, isEffectiveApi} from '../utils/api';
import omit from 'lodash/omit'; import omit from 'lodash/omit';
import find from 'lodash/find'; import find from 'lodash/find';
import findIndex from 'lodash/findIndex';
import Html from '../components/Html'; import Html from '../components/Html';
import {Spinner} from '../components'; import {Spinner} from '../components';
@ -996,18 +997,57 @@ export default class CRUD extends React.Component<CRUDProps, any> {
let newUnSelectedItems = unSelectedItems; let newUnSelectedItems = unSelectedItems;
if (keepItemSelectionOnPageChange && store.selectedItems.length) { if (keepItemSelectionOnPageChange && store.selectedItems.length) {
const thisBatch = items.concat(unSelectedItems); const oldItems = store.selectedItems.concat();
let notInThisBatch = (item: any) => const oldUnselectedItems = store.unSelectedItems.concat();
!find(
thisBatch, items.forEach(item => {
const idx = findIndex(
oldItems,
a => a[primaryField || 'id'] == item[primaryField || 'id'] a => a[primaryField || 'id'] == item[primaryField || 'id']
); );
newItems = store.selectedItems.filter(notInThisBatch); if (~idx) {
newUnSelectedItems = store.unSelectedItems.filter(notInThisBatch); oldItems[idx] = item;
} else {
oldItems.push(item);
}
});
newItems.push(...items); unSelectedItems.forEach(item => {
newUnSelectedItems.push(...unSelectedItems); const idx = findIndex(
oldUnselectedItems,
a => a[primaryField || 'id'] == item[primaryField || 'id']
);
const idx2 = findIndex(
oldItems,
a => a[primaryField || 'id'] == item[primaryField || 'id']
);
if (~idx) {
oldUnselectedItems[idx] = item;
} else {
oldUnselectedItems.push(item);
}
~idx2 && oldItems.splice(idx2, 1);
});
newItems = oldItems;
newUnSelectedItems = oldUnselectedItems;
// const thisBatch = items.concat(unSelectedItems);
// let notInThisBatch = (item: any) =>
// !find(
// thisBatch,
// a => a[primaryField || 'id'] == item[primaryField || 'id']
// );
// newItems = store.selectedItems.filter(notInThisBatch);
// newUnSelectedItems = store.unSelectedItems.filter(notInThisBatch);
// newItems.push(...items);
// newUnSelectedItems.push(...unSelectedItems);
} }
if (pickerMode && !multiple && newItems.length > 1) { if (pickerMode && !multiple && newItems.length > 1) {