AngularJS中的Directive自定义一个表格

先给大家说下表格的需求:

● 表格结构

rush:js;"> Name Street Age 4行

● 点击某个th,就对该列进行排序 ● 可以给表头取别名 ● 可以设置某个列是否显示 ● 表格下方有一行显示总行数

我们希望表格按如下方式展示:

rush:js;">

以上,datasource的数据源来自controller中$scope.customers,大致是{name: 'David',street: '1234 Anywhere St.',age: 25,url: 'index.html'}这样的格式,具体略去。

columnmap负责给列取别名,并且决定是否显示某个列。

如何实现呢?

Directive大致是这样的:

rush:js;"> var tableHelper = function(){ var template = '',link = function(scope,element,attrs){ } return { restrict: 'E',scope: { columnmap: '=',datasource: '=' },link:link,template:template }; } angular.module('directiveModule') .directive('tableHelper',tableHelper);

具体来说,

首先要监视datasource的变化,一旦有变化,就重新加载表格。

rush:js;"> scope.$watchCollection('datasource',render); //初始化表格 function render(){ if(scope.datasource && scope.datasource.length){ table += tableStart; table += renderHeader(); table += renderRows() + tableEnd; //加载统计行 renderTable(); } }

加载表格大致分成了三个步骤,加载表头,加载表格体,加载统计行。

rush:js;"> //加载头部 function renderHeader(){ var tr = 'cope.datasource[0]){ //{name: 'David',url: 'index.html'} //根据原始列名获取别名,并考虑了是否显示列的情况 var val = getColumnName(prop); if(val){ //visibleProps存储的是原始列名 visibleProps.push(prop); tr += '' + val + ''; } } tr += ''; return tr; } //加载行 function renderRows(){ var rows = ''; for(var i = 0,len = scope.datasource.length; i < len; i++){ rows += ' -1){ rows += ''; return rows; } //加载统计行 function renderTable(){ table += '
cope.datasource.length + '行
'; element.html(table); table = ''; }

加载表头的时候,用到了一个根据原始列名获取别名的方法

rush:js;"> //根据原始列名获取列的别名,并考虑是否隐藏列的情况 function getColumnName(prop){ if(!scope.columnmap) return prop; //得到[{name: 'Name'}] var val = filterColumnMap(prop); if(val && val.length && !val[0].hidden) return val[0][prop]; else return null; }

在getColumnName方法中,用到了一个根据原始列名

rush:js;"> //比如根据name属性,这里得到[{name: 'Name'}] //[{name: 'Name'},hidden: true}] function filterColumnMap(prop) { var val = scope.columnmap.filter(function(map) { if (map[prop]) { return true; } return false; }); return val; }

具体代码如下:

rush:js;"> (function(){ var tableHelper = fucntion(){ var template = '
',attrs){ var headerCols = [],//表头列们 tableStart = ' b[col]) return 1 * sortDir; if(a[col] < b[col]) return -1 * sortDir; return 0; }); //重新加载表格 render(); } //加载头部 function renderHeader(){ var tr = 'cope.datasource[0]){ //{name: 'David',len = scope.datasource.length; i < len; i++){ rows += ' -1){ rows += ''; return rows; } //加载统计行 function renderTable(){ table += '
cope.datasource.length + '行
'; element.html(table); table = ''; } //根据列的别名获取原始列名 function getRawColumnName(friendlyCol) { var rawCol; //columnmap =[{name: 'Name'},hidden: true}] scope.columnmap.forEach(function(colMap) { //{name: 'Name'} for (var prop in colMap) { if (colMap[prop] === friendlyCol) { rawCol = prop; break; } } return null; }); return rawCol; } function pushColumns(rawCol,renamedCol) { visibleProps.push(rawCol); scope.columns.push(renamedCol); } //比如根据name属性,这里得到[{name: 'Name'}] //[{name: 'Name'},hidden: true}] function filterColumnMap(prop) { var val = scope.columnmap.filter(function(map) { if (map[prop]) { return true; } return false; }); return val; } //根据原始列名获取列的别名,并考虑是否隐藏列的情况 function getColumnName(prop){ if(!scope.columnmap) return prop; //得到[{name: 'Name'}] var val = filterColumnMap(prop); if(val && val.length && !val[0].hidden) return val[0][prop]; else return null; } }; return { restrict: 'E',template:template }; }; angular.module('directiveModule') .directive('tableHelper',tableHelper); }());

以上所述是小编给大家分享的AngularJS中的Directive自定义一个表格的相关知识,希望对大家有所帮助。

相关文章

什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据...
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:...
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面