登录  /  注册
首页 > web前端 > js教程 > 正文

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

黄舟
发布: 2017-03-27 14:36:20
原创
2039人浏览过

这篇文章主要介绍了angularjs bootstrap table多选(全选)支持单击行选中实现编辑、删除功能,需要的朋友可以参考下

最终实现效果:

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">
  <p 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>
 <p>已选数量:{{count}}</p>
 <p>已选对象:{{selectData}}</p>
</p>
 </body>
</html>
登录后复制

script.js

// Code goes here
var routerApp = angular.module(&#39;routerApp&#39;, [ &#39;ngAnimate&#39;, &#39;ngSanitize&#39;, 
                   &#39;ui.bootstrap&#39;]);
routerApp.controller(&#39;zdTable&#39;, [
    &#39;$scope&#39;,
    function(scope) {
      console.log(&#39;controller&#39;);
     //初始化数据
      scope.datas = [
        {name:&#39;admin1&#39;, rem:&#39;备注&#39;}, 
        { name:&#39;admin2&#39;, rem:&#39;备注&#39;}, 
        { name:&#39;admin3&#39;, rem:&#39;备注&#39;}
        ];
      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(&#39;filterTable&#39;, function() {
  return function(obj) {
    var newObj = {};
    for ( var i in obj) {
      var property = obj[i];
      if(i != &#39;checked&#39;){
        newObj[i] = property;
      }
    }
    //console.log(newObj);
    return newObj;
  };
});
登录后复制

以上就是Angularjs+bootstrap+table多选(全选)支持单击行选中实现编辑、删除功能的代码案例的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号