Fix issue 4280 by initializing sortField when create new item (#4294)

* add more ignore pattern for git

* revert the change for api/.gitignore

* fix #4288 initialize sortField when create new item

* init the maxSort counter as -1

* use more reliable way to check if sortField exists or not

* reverted unexpected file changes

* Use local previewValues for start sort index

Co-authored-by: Jian Liao <jianliao@adobe.com>
Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
Jian Liao
2021-02-26 13:04:04 -08:00
committed by GitHub
parent 2b53532bd7
commit d1a6a61a16

View File

@@ -303,9 +303,11 @@ export default defineComponent({
if (o2mRelation.value?.sort_field) {
return [
...values
.filter((val) => val[o2mRelation.value.sort_field!])
.filter((val) => val.hasOwnProperty(o2mRelation.value.sort_field!))
.sort((a, b) => a[o2mRelation.value.sort_field!] - b[o2mRelation.value.sort_field!]), // sort by sort field if it exists
...values.filter((val) => !val[o2mRelation.value.sort_field!]).sort((a, b) => a.$index - b.$index), // sort the rest with $index
...values
.filter((val) => !val.hasOwnProperty(o2mRelation.value.sort_field!))
.sort((a, b) => a.$index - b.$index), // sort the rest with $index
];
} else {
return [...values.sort((a, b) => a.$index - b.$index)];
@@ -560,6 +562,11 @@ export default defineComponent({
[anyRelation.value.many_field]: {},
};
if (previewValues.value && o2mRelation.value?.sort_field) {
const maxSort = Math.max(-1, ...previewValues.value.map((val) => val[o2mRelation.value.sort_field!]));
newItem[o2mRelation.value.sort_field!] = maxSort + 1;
}
editsAtStart.value = newItem;
relatedPrimaryKey.value = '+';
currentlyEditing.value = '+';