crud 中的快速编辑 rowsDiff 保留 id 字段

This commit is contained in:
2betop 2020-03-25 11:43:33 +08:00
parent bfa03d2b96
commit 95aa42d0f8
5 changed files with 18 additions and 22 deletions

View File

@ -826,15 +826,7 @@ export default class CRUD extends React.Component<CRUDProps, any> {
const data: any = createObject(store.data, {
rows,
rowsDiff: (diff as Array<any>).map((item: any, index: number) => {
if (rows[index] && rows[index].hasOwnProperty(primaryField || 'id')) {
item = {
...item,
[primaryField || 'id']: (rows[index] as any)[primaryField || 'id']
};
}
return item;
}),
rowsDiff: diff,
indexes: indexes,
rowsOrigin
});

View File

@ -342,7 +342,7 @@ export default class Cards extends React.Component<GridProps, object> {
return;
}
const {onSave} = this.props;
const {onSave, primaryField} = this.props;
if (!onSave) {
return;
@ -350,7 +350,7 @@ export default class Cards extends React.Component<GridProps, object> {
onSave(
item.data,
difference(item.data, item.pristine),
difference(item.data, item.pristine, ['id', primaryField]),
item.index,
undefined,
item.pristine
@ -358,7 +358,7 @@ export default class Cards extends React.Component<GridProps, object> {
}
handleSave() {
const {store, onSave} = this.props;
const {store, onSave, primaryField} = this.props;
if (!onSave || !store.modifiedItems.length) {
return;
@ -367,7 +367,7 @@ export default class Cards extends React.Component<GridProps, object> {
const items = store.modifiedItems.map(item => item.data);
const itemIndexes = store.modifiedItems.map(item => item.index);
const diff = store.modifiedItems.map(item =>
difference(item.data, item.pristine)
difference(item.data, item.pristine, ['id', primaryField])
);
const unModifiedItems = store.items
.filter(item => !item.modified)

View File

@ -315,7 +315,7 @@ export default class List extends React.Component<ListProps, object> {
return;
}
const {onSave} = this.props;
const {onSave, primaryField} = this.props;
if (!onSave) {
return;
@ -323,7 +323,7 @@ export default class List extends React.Component<ListProps, object> {
onSave(
item.data,
difference(item.data, item.pristine),
difference(item.data, item.pristine, ['id', primaryField]),
item.index,
undefined,
item.pristine
@ -331,7 +331,7 @@ export default class List extends React.Component<ListProps, object> {
}
handleSave() {
const {store, onSave} = this.props;
const {store, onSave, primaryField} = this.props;
if (!onSave || !store.modifiedItems.length) {
return;
@ -340,7 +340,7 @@ export default class List extends React.Component<ListProps, object> {
const items = store.modifiedItems.map(item => item.data);
const itemIndexes = store.modifiedItems.map(item => item.index);
const diff = store.modifiedItems.map(item =>
difference(item.data, item.pristine)
difference(item.data, item.pristine, ['id', primaryField])
);
const unModifiedItems = store.items
.filter(item => !item.modified)

View File

@ -393,7 +393,7 @@ export default class Table extends React.Component<TableProps, object> {
saveImmediately?: boolean | any,
savePristine?: boolean
) {
const {onSave, saveImmediately: propsSaveImmediately} = this.props;
const {onSave, saveImmediately: propsSaveImmediately, primaryField} = this.props;
item.change(values, savePristine);
@ -422,7 +422,7 @@ export default class Table extends React.Component<TableProps, object> {
onSave(
item.data,
difference(item.data, item.pristine),
difference(item.data, item.pristine, ['id', primaryField]),
item.index,
undefined,
item.pristine
@ -430,7 +430,7 @@ export default class Table extends React.Component<TableProps, object> {
}
async handleSave() {
const {store, onSave} = this.props;
const {store, onSave, primaryField} = this.props;
if (!onSave || !store.modifiedRows.length) {
return;
@ -451,7 +451,7 @@ export default class Table extends React.Component<TableProps, object> {
const rows = store.modifiedRows.map(item => item.data);
const rowIndexes = store.modifiedRows.map(item => item.index);
const diff = store.modifiedRows.map(item =>
difference(item.data, item.pristine)
difference(item.data, item.pristine, ['id', primaryField])
);
const unModifiedRows = store.rows
.filter(item => !item.modified)

View File

@ -501,7 +501,7 @@ export function getScrollParent(node: HTMLElement): HTMLElement | null {
export function difference<
T extends {[propName: string]: any},
U extends {[propName: string]: any}
>(object: T, base: U): {[propName: string]: any} {
>(object: T, base: U, keepProps?: Array<string>): {[propName: string]: any} {
function changes(object: T, base: U) {
const keys: Array<keyof T & keyof U> = uniq(
Object.keys(object).concat(Object.keys(base))
@ -512,6 +512,10 @@ export function difference<
const a: any = object[key as keyof T];
const b: any = base[key as keyof U];
if (keepProps && ~keepProps.indexOf(key as string)) {
result[key] = a;
}
if (isEqual(a, b)) {
return;
}