如何用angularjs制作一个完整的表格
由于本人也是边学边写,因此整理的比较乱,下面放出我例子的完整代码,方便大家交流测试,如有问题欢迎评论
首先,表格采用的是BootStrap样式编辑的,主要使用的是angularjs,为了方便也有jQuery的方法,在测试时需自行引入bootstrap,angularjs和jq的文件。
整体代码预览:
HTML:
<!DOCTYPEhtml> <htmllang="en"ng-app="myModule"> <head> //需要自行引入BOOTStrap,angularjs和jQuery的js,css文件 <style> .pagination.num{ font-size:22px;color:red; } .text{ margin:0auto; border:1pxsolid#ccc; width:100%; max-width:200px; } </style> <title>欢迎</title> </head> <bodyng-controller="myCtrl"> <divclass="block"> <divclass="navbarnavbar-innerblock-header"> <divclass="mutedpull-left">{{kaohzbTitle}}</div> </div> <divclass="span12"style="float:left;"> <divclass="table-toolbar"> <buttonstyle="margin-left:5px;"id="refresh"ng-click="refresh()" class="btnbtn-success"> <iclass="icon-refreshicon-white"></i>刷新 </button> <buttonng-disabled="isdisabled"class="btn"ng-class="{'btn-info':isInfo}"id="savekaohzb" ng-click="save()"> <iclass="icon-editicon-white"></i>保存 </button> </div> </div> <divclass="row-fluid"> <divclass="span6"></div> <tableclass="tabletable-stripedtable-borderedtable-hover" id="example"style="margin-top:10px;"> <thead> <tr> <thstyle="width:20px;"rowspan="2">全选<br><inputtype="checkbox"ng-model="selectAll"></th> <thstyle="text-align:center;width:50px;vertical-align:middle"rowspan="2">序号</th> <thstyle="text-align:center;width:150px;vertical-align:middle"rowspan="2">名称</th> <thstyle="text-align:center;width:150px;vertical-align:middle"rowspan="2">日期</th> <thstyle="text-align:center;width:150px;"colspan="3">比赛队伍(红)</th> <thstyle="text-align:center;width:150px;"colspan="3">比赛队伍(蓝)</th> <thstyle="text-align:center;width:150px;vertical-align:middle"rowspan="2">比分</th> <thstyle="text-align:center;width:150px;vertical-align:middle"rowspan="2">说明</th> <thstyle="text-align:center;width:150px;vertical-align:middle"rowspan="2">玩家支持队伍</th> </tr> <tr> <thstyle="text-align:center;width:80px;">第一场</th> <thstyle="text-align:center;width:80px;">第二场</th> <thstyle="text-align:center;width:80px;">说明</th> <thstyle="text-align:center;width:80px;">第一场</th> <thstyle="text-align:center;width:80px;">第二场</th> <thstyle="text-align:center;width:80px;">说明</th> </tr> </thead> <tbodyng-click="fun()"id="page"ng-show="isshow"> <!--trackbytb.id--> <trng-repeat="tbinsaveDate"><!--只用angularjs实现点击一行就选中暂时无法实现--> <tdstyle="width:20px;"><inputtype="checkbox"ng-checked="selectAll"></td> <tdstyle="text-align:center;">{{tb.id}}</td> <tdstyle="text-align:center;">{{tb.zbname}}</td> <tdstyle="text-align:center;">{{tb.zbtime}}</td> <tdstyle="text-align:center;">{{tb.zbrul1}}</td> <tdstyle="text-align:center;">{{tb.zbrul2}}</td> <tdstyle="text-align:center;"><divclass="text"contenteditable="true"ng-model="tb.por"></div></td> <tdstyle="text-align:center;">{{tb.zbrul2}}</td> <tdstyle="text-align:center;">{{tb.zbrul1}}</td> <tdstyle="text-align:center;"><divclass="text"contenteditable="true"ng-model="tb.por"></div></td><!--2016.1.19通过可编译的div来代替输入框--> <tdstyle="text-align:center;">{{tb.score}}</td> <tdstyle="text-align:center;"><divclass="text"contenteditable="true"ng-model="tb.por"></div></td> <td> <selectname=""id=""ng-change="changetype(adds)"ng-model="adds"style="text-align:center;width:100%;min-width:80px;margin-bottom:0"> <optionvalue=""ng-show="isShow">{{tb.type}}</option> <optionvalue="支持红方">支持红方</option> <optionvalue="支持蓝方">支持蓝方</option> <optionvalue="双方相同">双方相同</option> </select> </td> </tr> </tbody> </table> </div> <divclass="pagination"> <ulstyle="float:right"> <liid="previous"><ahref="">上一页</a></li> <li><!--用于页标的显示--> <ulid="page_num_all"> </ul> </li> <liid="next"><ahref=""style="border:1pxsolid#ddd;float:right">下一页</a></li> </ul> <span> 当前为第<spanclass="num"id="current_page"></span>页,总共<spanclass="num"id="page_all"></span>页 </span> <span>您当前对select的操作值为:</span>{{typename}} </div> <!--ENDFORM--> </div> </body>
js代码:
<script> angular.module("myModule",[]).controller('myCtrl',function($scope){ $scope.kaohzbTitle="考核指标维护"; $scope.search=newObject(); $scope.isdisabled=false; $scope.isInfo=false; $scope.saveDate="";//用于保存得到的原始数据 //$http.post请求表格数据 //模仿请求得到的数据 vardatalist=[{ 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(){ vare=window.event||arguments[0]; varsrc=e.srcElement||e.target; if(src.nodeName=="TD"){ varpar=src.parentNode; varsd=par.getElementsByTagName("td")[0]; if(sd.firstChild.checked==true){ sd.firstChild.checked=false; }else{ $("trtd").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);//只要数据改变,自动保存到原始数据列表中 } //表格分页功能 functiontable_page(){ varshow_page=5;//每页显示的条数 varpage_all=$("#page").children().size();//总条数 varcurrent_page=1;//当前页 //console.log(page_all); varpage_num=Math.ceil(page_all/show_page);//总页数 varcurrent_num=0;//用于生成页标的计数器 varli="";//页标元素 while(page_num>current_num){//循环生成页标元素 li+='<liclass="page_num"><ahref="javasctip:(0)">'+(current_num+1)+'</a></li>'; current_num++; } $("#page_num_all").html(li);//添加页标到页面 $('#pagetr').css('display','none');//设置隐藏 $('#pagetr').slice(0,show_page).css('display','');//设置显示 $("#current_page").html(" "+current_page+" ");//显示当前页 $("#page_all").html(" "+page_num+" ");//显示总页数 $("#previous").click(function(){//上一页 varnew_page=parseInt($("#current_page").text())-1; if(new_page>0){ $("#current_page").html(" "+new_page+" "); tab_page(new_page); } }); $("#next").click(function(){//下一页 varnew_page=parseInt($("#current_page").text())+1;//当前页标 if(new_page<=page_num){//判断是否为最后或第一页 $("#current_page").html(" "+new_page+" "); tab_page(new_page); } }); $(".page_num").click(function(){//页标跳转 varnew_page=parseInt($(this).text()); tab_page(new_page); }); functiontab_page(index){//切换对应页标的页面 varstart=(index-1)*show_page;//开始截取的页标 varend=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; }//donothingifnong-model //SpecifyhowUIshouldbeupdated ngModel.$render=function(){ element.html(ngModel.$viewValue||''); }; //Listenforchangeeventstoenablebinding element.on('blurkeyupchange',function(){ scope.$apply(readViewText); }); //Noneedtoinitialize,AngularJSwillinitializethetextbasedonng-modelattribute //Writedatatothemodel functionreadViewText(){ varhtml=element.html(); //Whenweclearthecontenteditablethebrowserleavesa<br>behind //Ifstrip-brattributeisprovidedthenwestripthisout if(attrs.stripBr&&html==='<br>'){ html=''; } ngModel.$setViewValue(html); } } }; }) </script>