Angularjs+bootstrap+table多選(全選)支持單擊行選中實(shí)現(xiàn)編輯、刪除功能
最終實(shí)現(xiàn)效果:
index.html
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script> <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.3.1.js"></script> <link rel="external nofollow" rel="stylesheet"> <link rel="stylesheet" href="style.css" rel="external nofollow" > <script src="script.js"></script> </head> <body ng-app="routerApp"> <div ng-controller="zdTable"> <table class="table table-bordered" > <thead> <tr> <th> <input type="checkbox" ng-model="selectAll" ng-change="changeAll()" /> 選擇</th> <th>序號(hào)</th> <th>用戶</th> <th>備注</th> <th>操作</th> </tr> </thead> <tbody> <tr ng-repeat="row in datas" ng-init="outerIndex = $index" ng-click="changeCurrents(row, $event)"> <td><input type="checkbox" ng-model="row.checked" ng-click="changeCurrent(row, $event)" /></td> <td ng-bind="outerIndex+1"></td> <td ng-repeat="tddata in row | filterTable"> {{tddata}} </td> <td> <button type="button" class="btn btn-info" ng-click="zdTableEdit(row, $event)">編輯</button> <button type="button" class="btn btn-danger" ng-click="zdTableRemove(row, $event)">刪除</button> </td> </tr> </tbody> </table> <div>已選數(shù)量:{{count}}</div> <div>已選對(duì)象:{{selectData}}</div> </div> </body> </html>
script.js
// Code goes here var routerApp = angular.module('routerApp', [ 'ngAnimate', 'ngSanitize', 'ui.bootstrap']); routerApp.controller('zdTable', [ '$scope', function(scope) { console.log('controller'); //初始化數(shù)據(jù) scope.datas = [ {name:'admin1', rem:'備注'}, { name:'admin2', rem:'備注'}, { name:'admin3', rem:'備注'} ]; scope.count = 0;//已選擇數(shù)量 scope.selectData = [];//已選對(duì)象 //選擇單個(gè)(取消選擇單個(gè) scope.changeCurrent = function(current, $event) { //計(jì)算已選數(shù)量 true加, false減 scope.count += current.checked ? 1 : -1; //判斷是否全選,選數(shù)量等于數(shù)據(jù)長(zhǎng)度為true scope.selectAll = scope.count === scope.datas.length; //統(tǒng)計(jì)已選對(duì)象 scope.selectData = []; angular.forEach(scope.datas, function(item) { if(item.checked){ scope.selectData[scope.selectData.length] = item; } }); $event.stopPropagation();//阻止冒泡 }; //單擊行選中 scope.changeCurrents = function(current, $event) { if(current.checked == undefined){ current.checked = true; }else{ current.checked = !current.checked; } scope.changeCurrent(current, $event); }; //全選(取消全選 scope.changeAll = function() { //console.log(scope.selectAll); angular.forEach(scope.datas, function(item) { item.checked = scope.selectAll; }); scope.count = scope.selectAll ? scope.datas.length : 0; if (scope.selectAll) { scope.selectData = scope.datas; } else { scope.selectData = []; } }; //編輯事件 scope.zdTableEdit = function(item, $event){ console.log(item); $event.stopPropagation();//阻止冒泡 }; //刪除事件 scope.zdTableRemove = function(item, $event){ console.log(item); $event.stopPropagation();//阻止冒泡 }; } ]); //去掉不需要顯示的字段 routerApp.filter('filterTable', function() { return function(obj) { var newObj = {}; for ( var i in obj) { var property = obj[i]; if(i != 'checked'){ newObj[i] = property; } } //console.log(newObj); return newObj; }; });
以上所述是小編給大家介紹的Angularjs bootstrap table多選(全選)支持單擊行選中實(shí)現(xiàn)編輯、刪除功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
indexedDB bootstrap angularjs之 MVC DOMO (應(yīng)用示例)
這篇文章主要介紹了indexedDB bootstrap angularjs之 MVC DOMO (應(yīng)用示例)的相關(guān)資料,需要的朋友可以參考下2016-06-06AngularJS實(shí)現(xiàn)頁面定時(shí)刷新
本篇文章主要介紹了AngularJS實(shí)現(xiàn)頁面定時(shí)刷新,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03AngularJs返回前一頁面時(shí)刷新一次前面頁面的方法
今天小編就為大家分享一篇AngularJs返回前一頁面時(shí)刷新一次前面頁面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10Angular.js與node.js項(xiàng)目里用cookie校驗(yàn)賬戶登錄詳解
這篇文章主要介紹了Angular.js與node.js項(xiàng)目里用cookie校驗(yàn)賬戶登錄的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02Angular使用ng-messages與PHP進(jìn)行表單數(shù)據(jù)驗(yàn)證
這篇文章主要介紹了Angular使用ng-messages與PHP進(jìn)行表單數(shù)據(jù)驗(yàn)證,ng-messages提供了更方便的表單數(shù)據(jù)驗(yàn)證服務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Angular.js中用ng-repeat-start實(shí)現(xiàn)自定義顯示
大家都知道Angular.js可以用ng-repeat來顯示列表數(shù)據(jù),可是如果想要自定義顯示數(shù)據(jù)列表的話ng-repeat就實(shí)現(xiàn)不了了,這個(gè)時(shí)候可以利用ng-repeat-start 和 ng-repeat-end來實(shí)現(xiàn),下面通過本文來詳細(xì)看看實(shí)現(xiàn)的方法吧。2016-10-10Angular通過angular-cli來搭建web前端項(xiàng)目的方法
這篇文章主要介紹了Angular通過angular-cli來搭建web前端項(xiàng)目的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Angular2學(xué)習(xí)教程之TemplateRef和ViewContainerRef詳解
這篇文章主要給大家介紹了Angular2中TemplateRef和ViewContainerRef的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案
這篇文章主要介紹了詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07