使用AngularJS 添加行修改、删除表格数据

https://blog.csdn.net/xin_x1n/article/details/53070144

 


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
<title></title>
</head>
<body>

 

<div ng-app="myApp1" ng-controller="myCtrl1" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">

<p>循环对象:</p>
<table ng-repeat="y in names">
<tr>
<td>
<input value="{{ y.name }}" />
</td>
<td>{{ y.country }}</td>
<td>
<input type="button" ng-click="count = count + 1" value="{{ y.name + ', ' + y.country }}" /></td>
</tr>
</table>
<table>
<tr>
<th>id</th>
<th>productname</th>
<th>productid</th>
<th>
<input ng-click="add()" value="add" /></th>
</tr>
<tbody ng-repeat="z in arrs">
<tr>
<td>{{$index}}</td>
<td>
<input ng-model="z.productid" value="{{z.productid}}"></td>
<td>
<input ng-model="z.productname" value="{{z.productname}}"></td>
<td>
<input ng-click="updata($index)" value="update" /></td>
</tr>
</tbody>
</table>

 

 

<button ng-click="count = count + 1">点我!</button>

<p>{{ count }}</p>

</div>

 


<%--<div ng-app="myApp2" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">

<p>循环对象:</p>
<table ng-repeat="y in names">
<tr>
<td>
<input value="{{ y.name }}" />
</td>
<td>{{ y.country }}</td>
<td>
<input type="button" /></td>
</tr>
</table>


<ul>
<li ng-repeat="x in names">{{ x.name + ', ' + x.country }}</li>
</ul>

</div>--%>

 

 

 

<script>
var app = angular.module('myApp1', []);
app.controller('myCtrl1', function ($scope) {
//$scope表示作用域对象,每个控制器都有自己的作用域对象
$scope.arrs = [
{ "productid": "FI-SW-01", "productname": "Koi" },
{ "productid": "K9-DL-01", "productname": "Dalmation" },
{ "productid": "RP-SN-01", "productname": "Rattlesnake" },
{ "productid": "RP-LI-02", "productname": "Iguana" },
{ "productid": "FL-DSH-01", "productname": "Manx" },
{ "productid": "FL-DLH-02", "productname": "Persian" },
{ "productid": "AV-CB-01", "productname": "Amazon Parrot" }
];


//定义一个空对象 , 用于更新和保存数据时临时存储
$scope.prod = {};

//定义一个全局变量idx , 用于存储选中行的索引,方便执行保存操作时保存数据
var idx = -1;

 

//定义一个单击修改按钮时触发的事件,用于单击后弹出模块窗口用于修改数据
$scope.updata = function ($index) {

显示bootstrap中的模块窗口
//$('#modal-1').modal('show');

//将选中行的数据绑定到临时对象prod中,在下面的模态窗口中展示出来
$scope.prod.productid = $scope.arrs[$index].productid;
$scope.prod.productname = $scope.arrs[$index].productname;
//选中行的索引赋值给全局变量idx
idx = $index;
alert($scope.prod.productid)
};

//定义一个单机保存按钮时触发的事件,
$scope.save = function () {
//将修改后的值赋给数组
$scope.arrs[idx].productid = $scope.prod.productid;
$scope.arrs[idx].productname = $scope.prod.productname;
//关闭模块窗口
//$('#modal-1').modal('hide');
}
$scope.add = function () {
$scope.arrs.push({ "productid": "2222", "productname": "33333t" })
}

$scope.count = 0;
});
</script>

 

</body>
</html>

 

转载于:https://www.cnblogs.com/yyzyou/p/9623033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值