AngularJS實現(xiàn)表格的增刪改查(僅限前端)
更新時間:2017年07月04日 16:08:15 作者:CREATE_17
這篇文章主要介紹了AngularJS實現(xiàn)表格的增刪改查,僅限前端,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
用AngularJS實現(xiàn)對表格的增刪改查(僅限前端),具體代碼:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>實現(xiàn)表格的增刪改查</title> <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"> <link rel="stylesheet" > <link rel="stylesheet" href="css/font-awesome.css" type="text/css"></link> <link rel="stylesheet" href="css/ui.css" type="text/css"></link> <link rel="stylesheet" href="css/form.css" type="text/css"></link> <script type="text/javascript" src="js/jquery-1.11.1.js"></script> <script type="text/javascript" src="js/bootstrap.js"></script> <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> <style> .add{ position:relative; top:-40px; left:1000px; } </style> </head> <body> <div ng-app="myapp" ng-controller="myCtrl"> <h2>管理信息:</h2><br> <p>搜索:<input type="text" placeholder="請輸入關鍵字" ng-model="test"></p> <button class="btn btn-primary add" ng-click="add()">添加</button> <table class="table table-bordered" style="text-align: center"> <thead> <tr> <td>姓名</td> <td>年齡</td> <td>城市</td> <td>操作</td> </tr> </thead> <tbody> <tr ng-repeat="x in texts | filter:test"> <td>{{x.name}}</td> <td>{{x.age}}</td> <td>{{x.city}}</td> <td> <button class="btn btn-warning"" ng-click="update($index)">修改</button> <button class="btn btn-danger" ng-click="del($index)">刪除</button> </td> </tr> </tbody> </table> <!-- 添加信息 --> <div class="modal" id="modal-1"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button class="close" data-dismiss="modal"> <span class="glyphicon glyphicon-remove"></span> </button> <h3 class="modal-title">添加信息</h3> </div> <div class="modal-body"> <div>姓名:</div> <input ng-model="newName" type="text"> <div>年齡:</div> <input ng-model="newAge" type="text"> <div>城市:</div> <input ng-model="newCity" type="text"> </div> <div class="modal-footer"> <button class="btn btn-default" data-dismiss="modal">關閉</button> <button class="btn btn-success" ng-click="save()">保存</button> </div> </div> </div> </div> <!-- 修改信息 --> <div class="modal" id="modal-2"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button class="close" data-dismiss="modal"> <span class="glyphicon glyphicon-remove"></span> </button> <h3 class="modal-title">修改信息</h3> </div> <div class="modal-body"> <div>姓名:</div> <input ng-model="prod.name" value="{{prod.name}}" type="text"> <div>年齡:</div> <input ng-model="prod.age" value="{{prod.age}}" type="text"> <div>城市:</div> <input ng-model="prod.city" value="{{prod.city}}" type="text"> </div> <div class="modal-footer"> <button class="btn btn-default" data-dismiss="modal">關閉</button> <button class="btn btn-success" ng-click="ensure()">確定</button> </div> </div> </div> </div> </div> <script type="text/javascript"> var app = angular.module('myapp',[]); app.controller('myCtrl',function($scope){ //定義表格內(nèi)容 $scope.texts = [ {name:"張三",age:"23",city:"海南"}, {name:"李四",age:"25",city:"香港"}, {name:"王五",age:"25",city:"濟南"}, {name:"劉六",age:"22",city:"濟南"}, {name:"李七",age:"35",city:"煙臺"}, {name:"張八",age:"32",city:"聊城"}, {name:"呂九",age:"30",city:"盤錦"} ]; //定義一個空對象,用于保存和修改數(shù)據(jù)時臨時存儲 $scope.prod = {}; //定義一個單擊刪除按鈕時觸發(fā)的事件,用于刪除選中行 $scope.del = function ($index) { if($index>=0){ if(confirm("是否刪除"+$scope.texts[$index].name) ){ $scope.texts.splice($index,1); } } }; //定義一個全局變量idx,用于存儲選中行的索引,方便執(zhí)行保存操作。idx取值為0、1、、、、都有用,所以暫取值為-1; var idx = -1; //定義一個點擊添加按鈕時觸發(fā)的事件,用于新增數(shù)據(jù) $scope.add = function(){ //顯示bootstrap中的模塊窗口 $('#modal-1').modal('show'); }; //定義一個點擊保存按鈕時觸發(fā)的事件 $scope.save = function(){ //將添加的值賦給數(shù)組 $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'); }; //定義一個點擊修改按鈕時出發(fā)的事件,用于修改數(shù)據(jù) $scope.update = function($index){ //顯示bootstrap中的模塊窗口 $('#modal-2').modal('show'); //將選中行的數(shù)據(jù)綁定到臨時對象prod中,在下面的模態(tài)窗口中展示出來 $scope.prod.name = $scope.texts[$index].name; $scope.prod.age = $scope.texts[$index].age; $scope.prod.city = $scope.texts[$index].city; //選中行的索引賦值給全局變量idx idx = $index; }; //定義一個點擊確定按鈕時觸發(fā)的事件, $scope.ensure = function () { //將修改后的值賦給數(shù)組 $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'); }; }); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
angular同一頁面跳轉重新執(zhí)行的實現(xiàn)方法
這篇文章主要介紹了angular同一頁面跳轉重新執(zhí)行的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11angular json對象push到數(shù)組中的方法
下面小編就為大家分享一篇angular json對象push到數(shù)組中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02ng-events類似ionic中Events的angular全局事件
這篇文章主要介紹了ng-events類似ionic中Events的angular全局事件,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09Angular 2父子組件數(shù)據(jù)傳遞之局部變量獲取子組件其他成員
這篇文章主要給大家介紹了關于Angular 2父子組件之間數(shù)據(jù)傳遞之局部變量獲取子組件其他成員的相關資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07Angular 2.0+ 的數(shù)據(jù)綁定的實現(xiàn)示例
本篇文章主要介紹了Angular 2.0+ 的數(shù)據(jù)綁定的實現(xiàn)實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08angularJS?實現(xiàn)長按不觸發(fā)點擊事件可以復制剪貼方法
這篇文章主要為大家介紹了angularJS實現(xiàn)長按不觸發(fā)點擊事件可以復制剪貼方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06