Javascript 带索引分配的表排序
我有一个对象的Javascript 带索引分配的表排序,javascript,vuejs2,sortables,Javascript,Vuejs2,Sortables,我有一个对象的数组: ruta: [ { 'order': 1, 'id': 121 }, { 'order': 2, 'id': 123 } ] 我将其用作表的模型,同时,我使用扩展来手动排序表行: const createSortable = (el, options, vnode) => { return Sortable.create(el, { ...options, onEnd: function (evt) {
数组
:
ruta: [
{ 'order': 1, 'id': 121 },
{ 'order': 2, 'id': 123 }
]
我将其用作表的模型,同时,我使用扩展来手动排序表行:
const createSortable = (el, options, vnode) => {
return Sortable.create(el, {
...options,
onEnd: function (evt) {
const data = vnode.context.ruta
const item = data[evt.oldIndex]
if (evt.newIndex > evt.oldIndex) {
for (let i = evt.oldIndex; i < evt.newIndex; i++) {
data[i] = data[i + 1]
}
} else {
for (let i = evt.oldIndex; i > evt.newIndex; i--) {
data[i] = data[i - 1]
}
}
data[evt.newIndex] = item
//here
for (let i = 0; i < data.length; i++) {
data[i].order = i + 1;
}
}
})
}
因为我想从1开始计算订单的值。我还尝试将更改放入if/else
块:
if
data[i].order = i + 1;
else
data[i].order = i - 1;
但它也不起作用。行的顺序发生了错误的更改。您已经问过这个问题,我在那里给了您一个解决方案。我知道你已经解决了这个问题,但我将发布你问题的另一个解决方案,因为它将来可能对其他用户有用
首先,我已经向您解释了出现此问题的原因:如果更改模型的顺序,更改其索引的值,Vue
,则需要以另一种方式修改数组,例如,制作拼接
。在您的代码中,Vue
仅在您更改order
参数时检测到更改,此时列表手动排序,并且数组的每个索引的值都已更改,因此,视图将错误更新:
┌───────────────┬───────────────┬──────────────────┬───────────────┐
│ Initial state │ -> Model │ Manually sorting │ -> Model │
├───────────────┼───────────────┼──────────────────┼───────────────┤
│ 1 - item 1 │ Array index 0 │ 1 - item 4 │ Array index 3 │
│ 2 - item 2 │ Array index 1 │ 2 - item 1 │ Array index 0 │
│ 3 - item 3 │ Array index 2 │ 3 - item 2 │ Array index 1 │
│ 4 - item 4 │ Array index 3 │ 4 - item 3 │ Array index 2 │
└───────────────┴───────────────┴──────────────────┴───────────────┘
我之前给你的解决方案:
const createSortable = (el, options, vnode) => {
// Copy the order property
vnode.context.data.forEach( (obj) => {obj.norder = obj.order} );
// Create an array of orders
const orders = vnode.context.data.map((obj) => obj.order);
return Sortable.create(el, {
...options,
onEnd: function (evt) {
const data = vnode.context.data;
// Update the position of the objects
orders.splice(evt.newIndex, 0, ...orders.splice(evt.oldIndex, 1));
// Change the order parameter
data.forEach((obj) => {
obj.order = orders.findIndex((n) => n === obj.norder) + 1;
});
}
});
};
工作示例:
另一种解决方案:
const createSortable = (el, options, vnode) => {
// Copy the order property
vnode.context.data.forEach( (obj) => {obj.norder = obj.order} );
// Create an array of orders
const orders = vnode.context.data.map((obj) => obj.order);
return Sortable.create(el, {
...options,
onEnd: function (evt) {
const data = vnode.context.data;
// Update the position of the objects
orders.splice(evt.newIndex, 0, ...orders.splice(evt.oldIndex, 1));
// Change the order parameter
data.forEach((obj) => {
obj.order = orders.findIndex((n) => n === obj.norder) + 1;
});
}
});
};
另一种解决方案是在每次移动后重置手动排序,并使用拼接
更改阵列
顺序。看一看:
const createSortable = (el, options, vnode) => {
let order = [];
return Sortable.create(el, {
...options,
onStart: function (evt) {
// when the sort starts, store the initial order of the array
order = this.toArray();
},
onEnd: function (evt) {
// when the sort ends, set the order to the initial state
this.sort(order);
// change the order using splice
const data = vnode.context.data;
data.splice(evt.newIndex, 0, ...data.splice(evt.oldIndex, 1));
// now it is safe, you can update the order parameter
data.forEach((o, i) => {
o.order = i + 1;
});
}
});
};
这里有一个有效的例子:你已经问过了,我在那里给了你一个解决方案。我知道你已经解决了这个问题,但我将发布你问题的另一个解决方案,因为它将来可能对其他用户有用
首先,我已经向您解释了出现此问题的原因:如果更改模型的顺序,更改其索引的值,Vue
,则需要以另一种方式修改数组,例如,制作拼接
。在您的代码中,Vue
仅在您更改order
参数时检测到更改,此时列表手动排序,并且数组的每个索引的值都已更改,因此,视图将错误更新:
┌───────────────┬───────────────┬──────────────────┬───────────────┐
│ Initial state │ -> Model │ Manually sorting │ -> Model │
├───────────────┼───────────────┼──────────────────┼───────────────┤
│ 1 - item 1 │ Array index 0 │ 1 - item 4 │ Array index 3 │
│ 2 - item 2 │ Array index 1 │ 2 - item 1 │ Array index 0 │
│ 3 - item 3 │ Array index 2 │ 3 - item 2 │ Array index 1 │
│ 4 - item 4 │ Array index 3 │ 4 - item 3 │ Array index 2 │
└───────────────┴───────────────┴──────────────────┴───────────────┘
我之前给你的解决方案:
const createSortable = (el, options, vnode) => {
// Copy the order property
vnode.context.data.forEach( (obj) => {obj.norder = obj.order} );
// Create an array of orders
const orders = vnode.context.data.map((obj) => obj.order);
return Sortable.create(el, {
...options,
onEnd: function (evt) {
const data = vnode.context.data;
// Update the position of the objects
orders.splice(evt.newIndex, 0, ...orders.splice(evt.oldIndex, 1));
// Change the order parameter
data.forEach((obj) => {
obj.order = orders.findIndex((n) => n === obj.norder) + 1;
});
}
});
};
工作示例:
另一种解决方案:
const createSortable = (el, options, vnode) => {
// Copy the order property
vnode.context.data.forEach( (obj) => {obj.norder = obj.order} );
// Create an array of orders
const orders = vnode.context.data.map((obj) => obj.order);
return Sortable.create(el, {
...options,
onEnd: function (evt) {
const data = vnode.context.data;
// Update the position of the objects
orders.splice(evt.newIndex, 0, ...orders.splice(evt.oldIndex, 1));
// Change the order parameter
data.forEach((obj) => {
obj.order = orders.findIndex((n) => n === obj.norder) + 1;
});
}
});
};
另一种解决方案是在每次移动后重置手动排序,并使用拼接
更改阵列
顺序。看一看:
const createSortable = (el, options, vnode) => {
let order = [];
return Sortable.create(el, {
...options,
onStart: function (evt) {
// when the sort starts, store the initial order of the array
order = this.toArray();
},
onEnd: function (evt) {
// when the sort ends, set the order to the initial state
this.sort(order);
// change the order using splice
const data = vnode.context.data;
data.splice(evt.newIndex, 0, ...data.splice(evt.oldIndex, 1));
// now it is safe, you can update the order parameter
data.forEach((o, i) => {
o.order = i + 1;
});
}
});
};
这里有一个工作示例: