国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

AngularJS實(shí)現(xiàn)表格的增刪改查(僅限前端)

2019-11-19 16:10:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

用AngularJS實(shí)現(xiàn)對(duì)表格的增刪改查(僅限前端),具體代碼:

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>實(shí)現(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"  rel="external nofollow" > <link rel="stylesheet" href="css/font-awesome.css" rel="external nofollow" type="text/css"></link> <link rel="stylesheet" href="css/ui.css" rel="external nofollow" type="text/css"></link> <link rel="stylesheet" href="css/form.css" rel="external nofollow" 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="請(qǐng)輸入關(guān)鍵字" 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">關(guān)閉</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">關(guān)閉</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:"濟(jì)南"},    {name:"劉六",age:"22",city:"濟(jì)南"},    {name:"李七",age:"35",city:"煙臺(tái)"},    {name:"張八",age:"32",city:"聊城"},    {name:"呂九",age:"30",city:"盤(pán)錦"}   ];   //定義一個(gè)空對(duì)象,用于保存和修改數(shù)據(jù)時(shí)臨時(shí)存儲(chǔ)   $scope.prod = {};   //定義一個(gè)單擊刪除按鈕時(shí)觸發(fā)的事件,用于刪除選中行   $scope.del = function ($index) {    if($index>=0){     if(confirm("是否刪除"+$scope.texts[$index].name) ){      $scope.texts.splice($index,1);     }    }   };      //定義一個(gè)全局變量idx,用于存儲(chǔ)選中行的索引,方便執(zhí)行保存操作。idx取值為0、1、、、、都有用,所以暫取值為-1;   var idx = -1;   //定義一個(gè)點(diǎn)擊添加按鈕時(shí)觸發(fā)的事件,用于新增數(shù)據(jù)   $scope.add = function(){    //顯示bootstrap中的模塊窗口    $('#modal-1').modal('show');       };   //定義一個(gè)點(diǎn)擊保存按鈕時(shí)觸發(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});    //關(guān)閉模塊窗口    $('#modal-1').modal('hide');   };         //定義一個(gè)點(diǎn)擊修改按鈕時(shí)出發(fā)的事件,用于修改數(shù)據(jù)   $scope.update = function($index){    //顯示bootstrap中的模塊窗口    $('#modal-2').modal('show');    //將選中行的數(shù)據(jù)綁定到臨時(shí)對(duì)象prod中,在下面的模態(tài)窗口中展示出來(lá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;   };   //定義一個(gè)點(diǎn)擊確定按鈕時(shí)觸發(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;    //關(guān)閉模塊窗口    $('#modal-2').modal('hide');   };           }); </script> </body></html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 卢湾区| 湘潭市| 宜春市| 莱州市| 东莞市| 阿合奇县| 桂平市| 姚安县| 固安县| 南召县| 荔波县| 和平县| 合山市| 获嘉县| 罗平县| 张北县| 宜君县| 密山市| 天水市| 启东市| 加查县| 房产| 绥宁县| 赤峰市| 南木林县| 玛纳斯县| 乌兰察布市| 巴中市| 疏附县| 利辛县| 石门县| 宜阳县| 眉山市| 新泰市| 西华县| 庆阳市| 梁山县| 云梦县| 桂林市| 平山县| 建平县|