AngularJS实现表格的增删改查(仅限前端)

前端之家收集整理的这篇文章主要介绍了AngularJS实现表格的增删改查(仅限前端)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用AngularJS实现对表格的增删改查(仅限前端),具体代码

Meta charset="UTF-8"> 实现表格的增删改查

<Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<Meta http-equiv="description" content="this is my page">
<Meta http-equiv="content-type" content="text/html; charset=UTF-8">

nofollow" type="text/css"> nofollow" type="text/css"> nofollow" type="text/css">

<script type="text/javascript" src="js/jquery-1.11.1.js">
<script type="text/javascript" src="js/bootstrap.js">
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"&gt;

管理信息:

搜索

修改

<div class="modal" id="modal-1">

<div class="modal-dialog"&gt;

 <div class="modal-content"&gt;

  <div class="modal-header"&gt;
   <button class="close" data-dismiss="modal"&gt;
    <span class="glyphicon glyphicon-remove"&gt;</span>
   </button>
   <h3 class="modal-title"&gt;<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>信息</h3>
  </div>

  <div class="modal-body"&gt;
   <div>姓名:</div>
   <input ng-model="newName" type="text"&gt;
   <div>年龄:</div>
   <input ng-model="newAge" type="text"&gt;
   <div>城市:</div>
   <input ng-model="newCity" type="text"&gt;
  </div>

  <div class="modal-footer"&gt;
   <button class="btn btn-default" data-dismiss="modal"&gt;<a href="/tag/guanbi/" target="_blank" class="keywords">关闭</a></button>
   <button class="btn btn-success" ng-click="save()"&gt;保存</button>
  </div>

 </div>

</div>

<div class="modal" id="modal-2">

<div class="modal-dialog"&gt;

 <div class="modal-content"&gt;

  <div class="modal-header"&gt;
   <button class="close" data-dismiss="modal"&gt;
    <span class="glyphicon glyphicon-remove"&gt;</span>
   </button>
   <h3 class="modal-title"&gt;<a href="/tag/xiugai/" target="_blank" class="keywords">修改</a>信息</h3>
  </div>

  <div class="modal-body"&gt;
   <div>姓名:</div>
   <input ng-model="prod.name" value="{{prod.name}}" type="text"&gt;
   <div>年龄:</div>
   <input ng-model="prod.age" value="{{prod.age}}" type="text"&gt;
   <div>城市:</div>
   <input ng-model="prod.city" value="{{prod.city}}" type="text"&gt;
  </div>

  <div class="modal-footer"&gt;
   <button class="btn btn-default" data-dismiss="modal"&gt;<a href="/tag/guanbi/" target="_blank" class="keywords">关闭</a></button>
   <button class="btn btn-success" ng-click="ensure()"&gt;确定</button>
  </div>

 </div>

</div>

<script type="text/javascript">
var app = angular.module('myapp',[]);
app.controller('myCtrl',function($scope){
//定义表格内容
$scope.texts = [
{name:"张三",age:"23",city:"海南"},{name:"李四",age:"25",city:"香港"},{name:"王五",city:"济南"},{name:"刘六",age:"22",{name:"李七",age:"35",city:"烟台"},{name:"张八",age:"32",city:"聊城"},{name:"吕九",age:"30",city:"盘锦"}
];
//定义一个空对象,用于保存和修改数据时临时存储
$scope.prod = {};
//定义一个单击删除按钮时触发的事件,用于删除选中行
$scope.del = function ($index) {
if($index>=0){
if(confirm("是否删除"+$scope.texts[$index].name) ){
$scope.texts.splice($index,1);
}
}
};

//定义一个全局变量idx,用于存储选中行的索引,方便执行保存操作。idx取值为0、1、、、、都有用,所以暂取值为-1;
var idx = -1;
//定义一个点击添加按钮时触发的事件,用于新增数据
$scope.add = function(){
//显示bootstrap中的模块窗口
$('#modal-1').modal('show');

};
//定义一个点击保存按钮时触发的事件
$scope.save = function(){
//将添加的值赋给数组
$scope.texts.name = $scope.newName;
$scope.texts.age = $scope.newAge;
$scope.texts.city = $scope.newCity;
$scope.texts.push({name:$scope.newName,age:$scope.newAge,city:$scope.newCity});
//关闭模块窗口
$('#modal-1').modal('hide');
};

//定义一个点击修改按钮时出发的事件,用于修改数据
$scope.update = function($index){
//显示bootstrap中的模块窗口
$('#modal-2').modal('show');

//将选中行的数据绑定到临时对象prod中,在下面的模态窗口中展示出来
$scope.prod.name = $scope.texts[$index].name;
$scope.prod.age = $scope.texts[$index].age;
$scope.prod.city = $scope.texts[$index].city;
//选中行的索引赋值给<a href="/tag/quanjubianliang/" target="_blank" class="keywords">全局变量</a>idx
idx = $index;

};

//定义一个点击确定按钮时触发的事件,$scope.ensure = function () {
//将修改后的值赋给数组
$scope.texts[idx].name = $scope.prod.name;
$scope.texts[idx].age = $scope.prod.age;
$scope.texts[idx].city = $scope.prod.city;
//关闭模块窗口
$('#modal-2').modal('hide');
};

});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

猜你在找的JavaScript相关文章