Javascript 在angular js应用程序中需要根据索引对对象列表重新排序
在我的angularjs应用程序中,我有以下对象列表Javascript 在angular js应用程序中需要根据索引对对象列表重新排序,javascript,angularjs,json,Javascript,Angularjs,Json,在我的angularjs应用程序中,我有以下对象列表 $scope.itemsList = [ { "setupId": "T_2893", "name" : "abc" }, { "setupId": "LBT826", "name" : "xyz" }, { "setupId": "LBT1252", "name" : "pqr" }, { "setupId": "G12
$scope.itemsList = [
{
"setupId": "T_2893",
"name" : "abc"
},
{
"setupId": "LBT826",
"name" : "xyz"
},
{
"setupId": "LBT1252",
"name" : "pqr"
},
{
"setupId": "G1252",
"name" : "dwr"
}
]
现在,当我调用$scope.ChangeOreOrder(1,3)函数时,它应该根据上一个索引和下一个索引对对象重新排序。因此,清单应如下所示
$scope.itemsList = [
{
"setupId": "T_2893",
"name" : "abc"
},
{
"setupId": "LBT1252",
"name" : "pqr"
},
{
"setupId": "G1252",
"name" : "dwr"
},
{
"setupId": "LBT826",
"name" : "xyz"
}
]
现在,如果我调用,$scope.changeorder(2,0)
,新列表应该是
$scope.itemsList = [
{
"setupId": "G1252",
"name" : "dwr"
},
{
"setupId": "T_2893",
"name" : "abc"
},
{
"setupId": "LBT1252",
"name" : "pqr"
},
{
"setupId": "LBT826",
"name" : "xyz"
}
]
在我的$scope.changeOrder函数中,我尝试了不同的方法,比如在
prevIndex
处备份对象,然后在prevIndex
处删除obj以在newIndex
处插入备份的obj,但是因为我已经删除了对象,newIndex
在当前列表中不再有效!!!。像这样,我尝试了不同的方法,但最终的列表并没有按照我期望的方式排序。有人能帮我修复吗。将项目移动到指定索引:
var items = [
{
"setupId": "G1252",
"name" : "dwr"
},
{
"setupId": "T_2893",
"name" : "abc"
},
{
"setupId": "LBT1252",
"name" : "pqr"
},
{
"setupId": "LBT826",
"name" : "xyz"
}
];
function moveItem(posA, posB) {
/*
* If an item is moved from a higher index to a lower index, then we need to
* remove the current item first, then add it.
* When moving a item from a low index to a higer index, then we need to add
* the item first, then delete it.
*/
var upToDown = posA > posB;
var itemToMove = items[posA];
//Make copy first
var tmpList = items.slice(0);
if (!upToDown) {
//Add item to specified index
tmpList.splice(posB+1, 0, itemToMove);
//Remove the old item
tmpList.splice(posA, 1);
} else {
//Remove the old item
tmpList.splice(posA, 1);
//Add item to specified index
tmpList.splice(posB, 0, itemToMove);
}
return tmpList;
}
var result = moveItem(0, 3);
console.log(result);
Plunk:将项目移动到指定索引:
var items = [
{
"setupId": "G1252",
"name" : "dwr"
},
{
"setupId": "T_2893",
"name" : "abc"
},
{
"setupId": "LBT1252",
"name" : "pqr"
},
{
"setupId": "LBT826",
"name" : "xyz"
}
];
function moveItem(posA, posB) {
/*
* If an item is moved from a higher index to a lower index, then we need to
* remove the current item first, then add it.
* When moving a item from a low index to a higer index, then we need to add
* the item first, then delete it.
*/
var upToDown = posA > posB;
var itemToMove = items[posA];
//Make copy first
var tmpList = items.slice(0);
if (!upToDown) {
//Add item to specified index
tmpList.splice(posB+1, 0, itemToMove);
//Remove the old item
tmpList.splice(posA, 1);
} else {
//Remove the old item
tmpList.splice(posA, 1);
//Add item to specified index
tmpList.splice(posB, 0, itemToMove);
}
return tmpList;
}
var result = moveItem(0, 3);
console.log(result);
Plunk:为什么不使用重新排序的对象创建一个新数组,然后在完成后使旧数组与新数组相等?为什么不使用重新排序的对象创建一个新数组,然后在完成后使旧数组与新数组相等?如果要通过添加/删除元素来修改现有数组,然后我建议使用
angular.copy()
这是交换项目的一个非常简单的解决方案。发布的问题中的问题是项目已被删除,如果只是内容更改,这是不必要的。com2ghz,您的解决方案中存在错误,它会交换两个位置对象,但这不是我想要的。我希望将一个对象向左或向右拖动,并相应地调整其余对象的位置。我已修改了答案,请检查这是否是您想要的。如果您希望通过添加/删除元素来修改现有数组,则建议使用angular.copy()
这是交换项目的一个非常简单的解决方案。发布的问题中的问题是项目已被删除,如果只是内容更改,这是不必要的。com2ghz,您的解决方案中存在错误,它会交换两个位置对象,但这不是我想要的。我希望将一个对象拖动到左侧或右侧,并相应地调整其余对象的位置。我已修改了我的答案,请检查这是否是您想要的。