我创建了表数据和编辑按钮,但在尝试将编辑数据添加到特定行时遇到问题。
这是我的笨蛋:- http://plnkr.co/edit/QKz7nu458i8Il4OLmAyw?p=preview
这里是 HTML 页面
<table class="table table-bordered">
<thead>
<tr>
<th><input type='checkbox' ng-modal='isall' ng-click='selectallrows()'></th>
<th>ID</th>
<th>Name</th>
<th>EDIT</th>
</tr>
</thead>
<tbody>
<tr ng-repeat='p in person' ng-class="{'success':tableselection[$index]}">
<td><input type='checkbox' ng-model='tableselection[$index]'></td>
<td>{{p.id}}</td>
<td>{{p.name}}</td>
<td><button class='btn btn-primary' ng-click='edit(p.id)'><span class="glyphicon glyphicon-pencil"></span>Edit</button></td>
</tr>
</tbody>
</table>
<div ng-show='editabledata'>
<form role='form' class='form-horizontal'>
<div class='form-group'>
<label>Id :-</label>
<input type='text' ng-model='pid' class='from-group col-sm-2'></div>
<div class='form-group'>
<label>Name:-</label>
<input type='text' ng-model='pname' class='from-group col-sm-2'>
</div>
<button class='btn btn-primary' ng-click='saveeditdata(pid)'>Save</button>
<button clas='btn btn-primary' ng-click='canceleditdata()'>Cancel</button>
</form>
</div>
这是我的 .js
$scope.person=[
{id:1,name:'devendher'},
{id:2,name:'Macha'},
{id:3,name:'Macha devendher'}
];
$scope.editabledata=false;
$scope.edit=function(id){
$scope.editabledata=true;
for(i=0;i<$scope.person.length;i++){
if($scope.person[i].id==id)
{
$scope.pid=$scope.person[i].id;
$scope.pname=$scope.person[i].name;
}
}
}
$scope.saveeditdata=function(id){
for(i=0;i<$scope.person.length;i++){
if($scope.person[i].id==id){
$scope.person[i].push({'id':$scope.pid,'name':$scope.pname})
}
}
}
我得到错误“$scope.person[i].push”不是一个函数 有没有其他方法可以将数据添加到数组的特定索引?
最佳答案
我想你的意思是:
$scope.person.push({...})
或者
$scope.person[i] = {...}
关于javascript - 如何使用angularjs将数据添加到json数组的特定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244138/