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

如何用angularjs制作一个完整的表格,第1张

首先,表格采用的是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 tbid-->

<tr ng-repeat="tb in saveDate"><!-- 只用angularjs实现点击一行就选中暂时无法实现 -->

<td style="width: 20px;"><input type="checkbox" ng-checked="selectAll"></td>

<td style="text-align:center;">{{tbid}}</td>

<td style="text-align:center;">{{tbzbname}}</td>

<td style="text-align:center;">{{tbzbtime}}</td>

<td style="text-align:center;">{{tbzbrul1}}</td>

<td style="text-align:center;">{{tbzbrul2}}</td>

<td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tbpor"></div></td>

<td style="text-align:center;">{{tbzbrul2}}</td>

<td style="text-align:center;">{{tbzbrul1}}</td>

<td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tbpor"></div></td><!-- 2016119通过可编译的div来代替输入框 -->

<td style="text-align:center;">{{tbscore}}</td>

<td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tbpor"></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">{{tbtype}}</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>

angularmodule("myModule",[])controller('myCtrl', function($scope) {

$scopekaohzbTitle = "考核指标维护";

$scopesearch = new Object();

$scopeisdisabled=false;

$scopeisInfo=false;

$scopesaveDate="";//用于保存得到的原始数据

// $>

// 模仿请求得到的数据

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:"双方相同"}];

$scopefun=function(){

var e=windowevent||arguments[0];

var src=esrcElement||etarget;

if(srcnodeName=="TD"){

var par=srcparentNode;

var sd=pargetElementsByTagName("td")[0];

if(sdfirstChildchecked==true){

sdfirstChildchecked=false;

}else{

$("tr td")attr("checked",false);

sdfirstChildchecked=true;

}

}

}

$scoperefresh=function(){//点击刷新按钮显示表格

$scopesaveDate=datalist;

// consolelog("结束赋予数据");

$scope$watch("saveDate",function(){//2016120监听列表生成数据,当发生变化时执行刷新列表

table_page();

$scopeisshow=true;

});

}

$scopesave=function(){//页面提交按钮

consolelog("准备保存");

consolelog($scopesaveDate);//只要数据改变,自动保存到原始数据列表中

}

//表格分页功能

function table_page(){

var show_page=5;//每页显示的条数

var page_all=$("#page")children()size();//总条数

var current_page=1;//当前页

//  consolelog(page_all);

var page_num=Mathceil(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() {

elementhtml(ngModel$viewValue || '');

};

// Listen for change events to enable binding

elementon('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 = elementhtml();

// When we clear the content editable the browser leaves a <br> behind

// If strip-br attribute is provided then we strip this out

if (attrsstripBr && html === '<br>') {

html = '';

}

ngModel$setViewValue(html);

}

}

};

})

</script>

1 既然你使用了 ui-router,说明你的应用是 单页程序,既然是单页程序,首先要考虑动态路由是否有必要?只要根据用户角色 显示该角色可以访问的菜单即可,在每次路由切换的时候判断下是否有访问此路由的权限,没有就跳转到指定页面即可;

2 至于你说的动态加载 是想根据用户角色动态返回指定角色的路由、模板、controller js吗?一般项目把所有的js和模板都打包压缩成一个js,反而效果更好;

3 如果你非要想根据角色动态生成路由,可以在angular还没有启动的时候获取该用户角色的所有路由(也可以说菜单),然后循环菜单 通过 ui-router 动态加入即可;

4 这样只有路由是动态载入了,关于每个路由对应的js,如果你也想实现 动态载入的话,估计就需要使用类似 requirejs 的东西,比如 marcoslin/angularAMD · GitHub 或者 atian25/angular-lazyload · GitHub 或者自己写一个也可以,原理就是在 路由的 resolve 中加载对应的js

5 模板感觉就不需要动态加载了吧,因为使用ng,模板都是前端模板,如果要根据角色动态生成模板岂不是变成服务端渲染了。

在AngularJS中显示模型中的数据有两种方式:

一种是使用花括号插值的方式:

另一种是使用基于属性的指令,叫做ng-bind

这两种方式的效果是一样的,主要的区别在于:

使用花括号语法的时候,在AngularJS使用数据模板中的花括号时,第一个加载的页面,通常是应用中的indexhtml, 其未被渲染的模板可能会被用户看到

使用ng-bind的方法时就不会遇到这样的情况

遇到这种情况的原因是:

浏览器需要首先加载indexhtml页面,渲染他,然后AngularJS才能把它解析成你期望的内容。所以,对于Indexhtml页面的数据绑定 *** 作,建议采用ng-bind。那么在数据加载完成之前用户就不会看到任何内容。

以上就是关于如何用angularjs制作一个完整的表格全部的内容,包括:如何用angularjs制作一个完整的表格、如何使用angularjs处理动态菜单、AngularJS展示数据的ng-bind指令和{{}}的区别等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9509775.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存