Javascript 如何将记录传递到另一个表中?

Javascript 如何将记录传递到另一个表中?,javascript,html,angularjs,Javascript,Html,Angularjs,当我选中第一个表中的复选框时,整个行应该显示在第二个表中。在这里,我是用硬编码的方法来实现的,但我需要以一种动态的方式来实现 var-app=angular.module('myApp',[]); 应用程序控制器(“homeCtrl”,函数($scope){ $scope.items=[{ itemID:'BR063', itemValue:'145154' }, { itemID:'BR053', itemValue:'145154' }]; $scope.selectedItems=[];

当我选中第一个表中的复选框时,整个行应该显示在第二个表中。在这里,我是用硬编码的方法来实现的,但我需要以一种动态的方式来实现

var-app=angular.module('myApp',[]);
应用程序控制器(“homeCtrl”,函数($scope){
$scope.items=[{
itemID:'BR063',
itemValue:'145154'
}, {
itemID:'BR053',
itemValue:'145154'
}];
$scope.selectedItems=[];
$scope.addRec=函数(结果,i){
如果(结果==真){
$scope.selectedItems.push($scope.items[i]);
}
否则{
$scope.selectedItems.pop();
}
}
});

选择行
指数
项目ID
项目值
挑选
{{$index+1}}
{{x.itemID}
{{x.itemValue}}
选定行
指数
项目ID
项目值
挑选
{{$index+1}}
{{x.itemID}
{{x.itemValue}}

将addRec函数更改为

$scope.addRec = function(result, i){
      if(result == true){

         $scope.selectedItems.push($scope.items[i]);
      }
      else{ 
          $scope.selectedItems.pop();
      }
}

我相信这就是你需要的

var-app=angular.module('myApp',[]);
应用程序控制器(“homeCtrl”,函数($scope){
$scope.items=[{
itemID:'BR064',
itemValue:'145154'
}, {
itemID:'BR065',
itemValue:'145155'
},
{
itemID:'BR066',
itemValue:'145156'
},
{
itemID:'BR067',
itemValue:'145157'
},
{
itemID:'BR068',
itemValue:'145158'
}];
$scope.selectedItems=[];
$scope.addRec=函数(结果,x){
如果(结果==真){
$scope.selectedItems.push(x);
}否则{
if($scope.selectedItems.length){
设idx=0;
$scope.selectedItems.find(函数(元素,i){
if(element.itemValue==x.itemValue){
idx=i;
}
});
$scope.selectedItems.splice(idx,1);
}
}
}
});

选择行
指数
项目ID
项目值
挑选
{{$index+1}}
{{x.itemID}
{{x.itemValue}}
选定行
指数
项目ID
项目值
挑选
{{$index+1}}
{{x.itemID}
{{x.itemValue}}

1-将对象发送到函数

2-检查所选数组中是否存在

3-删除或向阵列添加项目

答案是这样的

var-app=angular.module('myApp',[]);
应用程序控制器(“homeCtrl”,函数($scope){
$scope.items=[{
itemID:'BR063',
itemValue:'145154'
}, {
itemID:'BR053',
itemValue:'145154'
}];
$scope.selectedItems=[];
$scope.addRec=函数(itm){
let index=$scope.selectedItems.findIndex((iitt)=>{
返回itm.itemID==iitt.itemID;
});
如果(索引==-1){
$scope.selectedItems.push(itm);
}否则{
$scope.selectedItems.splice(索引,1);
}
}
});

选择行
指数
项目ID
项目值
挑选
{{$index+1}}
{{x.itemID}
{{x.itemValue}}
选定行
指数
项目ID
项目值
挑选
{{$index+1}}
{{x.itemID}
{{x.itemValue}}

抓取您单击的行的索引,并更新与网格2关联的模型。我希望下面的回答能有所帮助。刚才我用您的代码更新了,但是如果我选择了第二条记录,那么它将推动emty行$scope.selectedItems.push($scope.items[I-1]);将行更改为此代码。将索引减少1并推送代码这是错误的-此处提示
$scope.selectedItems.push($scope.items[i-1])
您不能只删除最后添加的,请尝试选择1,然后选择2,然后取消选择1,然后再次选择它。我如何删除SelectedIn而不是
.pop()
方法,获取id,即
i
,并使用JavaScript的
.splice()
方法删除特定元素。如何使用splice来删除特定项目更新了代码,请注意,我现在传递的是model
x
的值,而不是index。