Angularjs+bootstrap+table多选(全选)支持单击行选中实现编辑、删除功能

 更新时间:2017年03月27日 11:20:03   作者:小思思smile  
这篇文章主要介绍了Angularjs bootstrap table多选(全选)支持单击行选中实现编辑、删除功能,需要的朋友可以参考下

最终实现效果:

最终效果

index.html

<!DOCTYPE html>
<html>
 <head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.3.1.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 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>序号</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>已选数量:{{count}}</div>
 <div>已选对象:{{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');
     //初始化数据
      scope.datas = [
        {name:'admin1', rem:'备注'}, 
        { name:'admin2', rem:'备注'}, 
        { name:'admin3', rem:'备注'}
        ];
      scope.count = 0;//已选择数量 
      scope.selectData = [];//已选对象
      //选择单个(取消选择单个
      scope.changeCurrent = function(current, $event) {
       //计算已选数量 true加, false减
        scope.count += current.checked ? 1 : -1;
        //判断是否全选,选数量等于数据长度为true
    scope.selectAll = scope.count === scope.datas.length;
        //统计已选对象
        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多选(全选)支持单击行选中实现编辑、删除功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • 详解AngularJS之$window窗口对象

    详解AngularJS之$window窗口对象

    本篇文章主要介绍了AngularJS之$window窗口对象,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Angular 数据请求的实现方法

    Angular 数据请求的实现方法

    本篇文章主要介绍了Angular 数据请求的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • angularJS实现表格部分列展开缩起示例代码

    angularJS实现表格部分列展开缩起示例代码

    这篇文章主要介绍了angularJS实现表格部分列展开缩起示例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • angular将html代码输出为内容的实例

    angular将html代码输出为内容的实例

    今天小编就为大家分享一篇angular将html代码输出为内容的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • 详解Angular如何正确的操作DOM

    详解Angular如何正确的操作DOM

    这篇文章主要介绍了详解Angular如何正确的操作DOM,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • AngularJS实现元素显示和隐藏的几个案例

    AngularJS实现元素显示和隐藏的几个案例

    这篇文章主要介绍了AngularJS实现元素显示和隐藏的几个案例,需要的朋友可以参考下
    2015-12-12
  • Angular整合zTree的示例代码

    Angular整合zTree的示例代码

    本篇文章主要介绍了Angular整合zTree的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • 老生常谈angularjs中的$state.go

    老生常谈angularjs中的$state.go

    下面小编就为大家带来一篇老生常谈angularjs中的$state.go。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • angular6.0使用教程之父组件通过url传递id给子组件的方法

    angular6.0使用教程之父组件通过url传递id给子组件的方法

    这篇文章主要介绍了angular6.0使用教程之父组件通过url传递id给子组件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 详解Angular中通过$location获取地址栏的参数

    详解Angular中通过$location获取地址栏的参数

    这篇文章主要介绍了详解 Angular中通过$location获取地址栏的参数,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08

最新评论