如何用angularjs制作一个完整的表格


Posted in Javascript onJanuary 21, 2016

由于本人也是边学边写,因此整理的比较乱,下面放出我例子的完整代码,方便大家交流测试,如有问题欢迎评论

首先,表格采用的是BootStrap样式编辑的,主要使用的是angularjs,为了方便也有jQuery的方法,在测试时需自行引入bootstrap,angularjs和jq的文件。

 整体代码预览:

HTML:

<!DOCTYPE html>
<html lang="en" ng-app="myModule">
<head>
 //需要自行引入BOOTStrap,angularjs和jQuery的js,css文件
 <style>
  .pagination .num{
   font-size:22px;color:red;
  }
  .text{
   margin:0 auto;
   border:1px solid #ccc;
   width:100%;
   max-width:200px;
  }
 </style>
 <title>欢迎</title>
</head>
<body ng-controller="myCtrl">
 <div class="block">
  <div class="navbar navbar-inner block-header">
   <div class="muted pull-left">{{kaohzbTitle}}</div>
  </div>

   <div class="span12" style="float:left;">
    <div class="table-toolbar">
     <button style="margin-left: 5px;" id="refresh" ng-click="refresh()"
       class="btn btn-success">
      <i class=" icon-refresh icon-white"></i> 刷新
     </button>
     <button ng-disabled="isdisabled" class="btn" ng-class="{'btn-info':isInfo}" id="savekaohzb"
       ng-click="save()">
      <i class="icon-edit icon-white"></i> 保存
     </button>
    </div>
   </div>
   <div class="row-fluid">
    <div class="span6"></div>
    <table class="table table-striped table-bordered table-hover"
      id="example" style="margin-top:10px;">
     <thead>
     <tr>
      <th style="width: 20px;" rowspan="2">全选 <br><input type="checkbox" ng-model="selectAll"></th>
      <th style="text-align: center; width: 50px;vertical-align: middle" rowspan="2">序号</th>
      <th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">名称</th>
      <th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">日期</th>
      <th style="text-align: center; width: 150px;" colspan="3">比赛队伍(红)</th>
      <th style="text-align: center; width: 150px;" colspan="3">比赛队伍(蓝)</th>
      <th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">比分</th>
      <th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">说明</th>
      <th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">玩家支持队伍</th>
     </tr>
     <tr>
      <th style="text-align: center; width: 80px;">第一场</th>
      <th style="text-align: center; width: 80px;">第二场</th>
      <th style="text-align: center; width: 80px;">说明</th>
      <th style="text-align: center; width: 80px;">第一场</th>
      <th style="text-align: center; width: 80px;">第二场</th>
      <th style="text-align: center; width: 80px;">说明</th>
     </tr>
     </thead>
     <tbody ng-click="fun()" id="page" ng-show="isshow">
     <!--track by tb.id-->
      <tr ng-repeat="tb in saveDate"><!-- 只用angularjs实现点击一行就选中暂时无法实现 -->
       <td style="width: 20px;"><input type="checkbox" ng-checked="selectAll"></td>
       <td style="text-align:center;">{{tb.id}}</td>
       <td style="text-align:center;">{{tb.zbname}}</td>
       <td style="text-align:center;">{{tb.zbtime}}</td>
       <td style="text-align:center;">{{tb.zbrul1}}</td>
       <td style="text-align:center;">{{tb.zbrul2}}</td>
       <td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tb.por"></div></td>
       <td style="text-align:center;">{{tb.zbrul2}}</td>
       <td style="text-align:center;">{{tb.zbrul1}}</td>
       <td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tb.por"></div></td><!-- 2016.1.19通过可编译的div来代替输入框 -->
       <td style="text-align:center;">{{tb.score}}</td>
       <td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tb.por"></div></td>
       <td>
        <select name="" id="" ng-change="changetype(adds)" ng-model="adds" style="text-align:center;width:100%;min-width:80px;margin-bottom:0">
         <option value="" ng-show="isShow">{{tb.type}}</option>
         <option value="支持红方">支持红方</option>
         <option value="支持蓝方">支持蓝方</option>
         <option value="双方相同">双方相同</option>
        </select>
       </td>
      </tr>
     </tbody>
    </table>
   </div>
  <div class="pagination">
   <ul style="float:right">
    <li id="previous"><a href="">上一页</a></li>
    <li><!--用于页标的显示 -->
     <ul id="page_num_all">
     </ul>
    </li>
    <li id="next"><a href="" style="border:1px solid #ddd;float:right">下一页</a></li>
   </ul>
   <span>
    当前为第<span class="num" id="current_page"></span>页,总共<span class="num" id="page_all"></span>页
   </span>
   <span>您当前对select的操作值为:</span>{{typename}}
  </div>
  <!-- END FORM-->
 </div>
</body>

js代码:

<script>
 angular.module("myModule",[]).controller('myCtrl', function($scope) {
  $scope.kaohzbTitle = "考核指标维护";
  $scope.search = new Object();
  $scope.isdisabled=false;
  $scope.isInfo=false;
  $scope.saveDate="";//用于保存得到的原始数据
// $http.post请求表格数据
// 模仿请求得到的数据
  var datalist=[{
   id:1,zbname:"中亚赛区比赛",zbtime:"2015-12-03",zbrul1:"胜利",zbrul2:"失败",por:"请输入说明内容",score:"2:1",type:"支持红方"},{
   id:2,zbname:"日韩赛区比赛",zbtime:"2015-11-11",zbrul1:"胜利",zbrul2:"胜利",por:"请输入说明内容",score:"2:1",type:"支持蓝方"},{
   id:3,zbname:"欧美赛区比赛",zbtime:"2015-3-03",zbrul1:"失败",zbrul2:"胜利",por:"请输入说明内容",score:"2:1",type:"双方相同"},{
   id:4,zbname:"中东赛区比赛",zbtime:"2016-1-05",zbrul1:"胜利",zbrul2:"失败",por:"请输入说明内容",score:"2:1",type:"支持蓝方"},{
   id:5,zbname:"北京赛区比赛",zbtime:"2014-12-23",zbrul1:"失败",zbrul2:"胜利",por:"请输入说明内容",score:"2:1",type:"双方相同"},{
   id:6,zbname:"韩国赛区比赛",zbtime:"2015-11-01",zbrul1:"失败",zbrul2:"胜利",por:"请输入说明内容",score:"2:1",type:"双方相同"},{
   id:7,zbname:"日本赛区比赛",zbtime:"2011-1-23",zbrul1:"胜利",zbrul2:"失败",por:"请输入说明内容",score:"2:1",type:"支持红方"},{
   id:8,zbname:"中亚赛区比赛",zbtime:"2013-12-15",zbrul1:"失败",zbrul2:"胜利",por:"请输入说明内容",score:"2:1",type:"支持蓝方"},{
   id:9,zbname:"中亚赛区比赛",zbtime:"2015-10-17",zbrul1:"失败",zbrul2:"胜利",por:"请输入说明内容",score:"2:1",type:"支持红方"},{
   id:10,zbname:"中亚赛区比赛",zbtime:"2015-11-21",zbrul1:"胜利",zbrul2:"胜利",por:"请输入说明内容",score:"2:1",type:"支持蓝方"},{
   id:11,zbname:"中亚赛区比赛",zbtime:"2015-2-02",zbrul1:"失败",zbrul2:"失败",por:"请输入说明内容",score:"2:1",type:"支持红方"},{
   id:12,zbname:"中亚赛区比赛",zbtime:"2015-2-05",zbrul1:"胜利",zbrul2:"失败",por:"请输入说明内容",score:"2:1",type:"双方相同"}];
$scope.fun=function(){
   var e=window.event||arguments[0];
   var src=e.srcElement||e.target;
   if(src.nodeName=="TD"){
    var par=src.parentNode;
    var sd=par.getElementsByTagName("td")[0];
    if(sd.firstChild.checked==true){
     sd.firstChild.checked=false;
    }else{
     $("tr td").attr("checked",false);
     sd.firstChild.checked=true;
    }
   }
  }
$scope.refresh=function(){//点击刷新按钮显示表格
   $scope.saveDate=datalist;
   // console.log("结束赋予数据");
   $scope.$watch("saveDate",function(){//2016.1.20监听列表生成数据,当发生变化时执行刷新列表
    table_page();
    $scope.isshow=true;
   });
  }

  $scope.save=function(){//页面提交按钮
   console.log("准备保存");
   console.log($scope.saveDate);//只要数据改变,自动保存到原始数据列表中

  }

  //表格分页功能
  function table_page(){
   var show_page=5;//每页显示的条数
   var page_all=$("#page").children().size();//总条数
   var current_page=1;//当前页
//  console.log(page_all);
   var page_num=Math.ceil(page_all/show_page);//总页数
   var current_num=0;//用于生成页标的计数器
   var li="";//页标元素
   while(page_num>current_num){//循环生成页标元素
    li+='<li class="page_num"><a href="javasctip:(0)">'+(current_num+1)+'</a></li>';
    current_num++;
   }
   $("#page_num_all").html(li);//添加页标到页面
   $('#page tr').css('display', 'none');//设置隐藏
   $('#page tr').slice(0, show_page).css('display', '');//设置显示
   $("#current_page").html(" "+current_page+" ");//显示当前页
   $("#page_all").html(" "+page_num+" ");//显示总页数
   $("#previous").click(function(){//上一页
    var new_page=parseInt($("#current_page").text())-1;
    if(new_page>0){
     $("#current_page").html(" "+new_page+" ");
     tab_page(new_page);
    }
   });
   $("#next").click(function(){//下一页
    var new_page=parseInt($("#current_page").text())+1;//当前页标
    if(new_page<=page_num){//判断是否为最后或第一页
     $("#current_page").html(" "+new_page+" ");
     tab_page(new_page);
    }
   });
   $(".page_num").click(function(){//页标跳转
    var new_page=parseInt($(this).text());
    tab_page(new_page);
   });
   function tab_page(index){//切换对应页标的页面
    var start=(index-1)*show_page;//开始截取的页标
    var end=start+show_page;//截取个数
    $('#page').children().css('display', 'none').slice(start, end).css('display', '');
    current_page=index;
    $("#current_page").html(" "+current_page+" ");
   }
  }

 }).directive('contenteditable', function() {//自定义ngModel的属性可以用在div等其他元素中
  return {
   restrict: 'A', // 作为属性使用
   require: '?ngModel', // 此指令所代替的函数
   link: function(scope, element, attrs, ngModel) {
    if (!ngModel) {
     return;
    } // do nothing if no ng-model
    // Specify how UI should be updated
    ngModel.$render = function() {
     element.html(ngModel.$viewValue || '');
    };
    // Listen for change events to enable binding
    element.on('blur keyup change', function() {
     scope.$apply(readViewText);
    });
    // No need to initialize, AngularJS will initialize the text based on ng-model attribute
    // Write data to the model
    function readViewText() {
     var html = element.html();
     // When we clear the content editable the browser leaves a <br> behind
     // If strip-br attribute is provided then we strip this out
     if (attrs.stripBr && html === '<br>') {
      html = '';
     }
     ngModel.$setViewValue(html);
    }
   }
  };
 })
</script>
Javascript 相关文章推荐
js 页面传参数时 参数值含特殊字符的问题
Dec 13 Javascript
Jquery 数组操作大全个人总结
Nov 13 Javascript
JS中的异常处理方法分享
Dec 22 Javascript
简单的jQuery入门指引
Jul 28 Javascript
详解从angular-cli:1.0.0-beta.28.3升级到@angular/cli:1.0.0
May 22 Javascript
js图片轮播插件的封装
Jul 21 Javascript
轻松搞定jQuery+JSONP跨域请求的解决方案
Mar 06 jQuery
vue 自定义右键样式的实例代码
Nov 06 Javascript
Jquery ajax书写方法代码实例解析
Jun 12 jQuery
Vue项目前后端联调(使用proxyTable实现跨域方式)
Jul 18 Javascript
vue解决跨域问题(推荐)
Nov 10 Javascript
WebWorker 封装 JavaScript 沙箱详情
Nov 02 Javascript
angularjs自定义ng-model标签的属性
Jan 21 #Javascript
angularjs在ng-repeat中使用ng-model遇到的问题
Jan 21 #Javascript
js实现的二分查找算法实例
Jan 21 #Javascript
jQuery模拟物体自由落体运动(附演示与demo源码下载)
Jan 21 #Javascript
angularjs表格分页功能详解
Jan 21 #Javascript
使用angularjs创建简单表格
Jan 21 #Javascript
Jquery中巧用Ajax的beforeSend方法
Jan 20 #Javascript
You might like
第十二节--类的自动加载
2006/11/16 PHP
php实现天干地支计算器示例
2014/03/14 PHP
ThinkPHP采用GET方式获取中文参数查询无结果的解决方法
2014/06/26 PHP
php单文件版在线代码编辑器
2015/03/12 PHP
fckeditor上传文件按日期存放及重命名方法
2015/05/22 PHP
PHP查询附近的人及其距离的实现方法
2016/05/11 PHP
JavaScript 数组循环引起的思考
2010/01/01 Javascript
jQuery对表单的操作代码集合
2011/04/06 Javascript
NodeJS 模块开发及发布详解分享
2012/03/07 NodeJs
学习从实践开始之jQuery插件开发 菜单插件开发
2012/05/03 Javascript
HTML长文本截取含有HTML代码同样适用的两种方法
2013/07/31 Javascript
Jquery中给animation加更多的运作效果实例
2013/09/05 Javascript
深入剖析JavaScript中的枚举功能
2014/03/06 Javascript
JS实现至少包含字母、大小写数字、字符的密码等级的两种方法
2015/02/03 Javascript
jQuery实现自定义checkbox和radio样式
2015/07/13 Javascript
jQuery取消特定的click事件
2016/02/29 Javascript
深入浅析Vue组件开发
2016/11/25 Javascript
Bootstrap进度条与AJAX后端数据传递结合使用实例详解
2017/04/23 Javascript
jquery ajaxfileuplod 上传文件 essyui laoding 效果【防止重复上传文件】
2018/05/26 jQuery
javascript异步处理与Jquery deferred对象用法总结
2019/06/04 jQuery
layui 动态设置checbox 选中状态的例子
2019/09/02 Javascript
js实现简单点赞操作
2020/03/17 Javascript
微信小程序将页面按钮悬浮固定在底部的实现代码
2020/10/29 Javascript
Webpack3+React16代码分割的实现
2021/03/03 Javascript
对python中使用requests模块参数编码的不同处理方法
2018/05/18 Python
python随机数分布random测试
2018/08/27 Python
python导入模块交叉引用的方法
2019/01/19 Python
win10安装tensorflow-gpu1.8.0详细完整步骤
2020/01/20 Python
Python内置函数及功能简介汇总
2020/10/13 Python
HTML5 Canvas+JS控制电脑或手机上的摄像头实例
2014/05/03 HTML / CSS
协议书怎么写
2014/04/21 职场文书
获奖感言一句话
2015/07/31 职场文书
公司规章制度范本
2015/08/03 职场文书
学习习近平主席讲话心得体会
2016/01/20 职场文书
2016年小学教师政治学习心得体会
2016/01/23 职场文书
jdbc使用PreparedStatement批量插入数据的方法
2021/04/27 MySQL