AngularJS中的Directive自定義一個表格
先給大家說下表格的需求:
● 表格結(jié)構(gòu)
<table> <thead> <tr> <th>Name</th> <th>Street</th> <th>Age</th> </tr> </thead> <tbody> <tr> <td>></td> <td>></td> <td>></td> </tr> </tbody> </table> <div>4行</div>
● 點擊某個th,就對該列進(jìn)行排序
● 可以給表頭取別名
● 可以設(shè)置某個列是否顯示
● 表格下方有一行顯示總行數(shù)
我們希望表格按如下方式展示:
<table-helper datasource="customers" clumnmap="[{name: 'Name'}, {street: 'Street'}, {age: 'Age'}, {url: 'URL', hidden: true}]"></table-helper>
以上,datasource的數(shù)據(jù)源來自controller中$scope.customers,大致是{name: 'David',street: '1234 Anywhere St.',age: 25,url: 'index.html'}這樣的格式,具體略去。
columnmap負(fù)責(zé)給列取別名,并且決定是否顯示某個列。
如何實現(xiàn)呢?
Directive大致是這樣的:
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);
具體來說,
首先要監(jiān)視datasource的變化,一旦有變化,就重新加載表格。
scope.$watchCollection('datasource', render); //初始化表格 function render(){ if(scope.datasource && scope.datasource.length){ table += tableStart; table += renderHeader(); table += renderRows() + tableEnd; //加載統(tǒng)計行 renderTable(); } }
加載表格大致分成了三個步驟,加載表頭,加載表格體,加載統(tǒng)計行。
//加載頭部 function renderHeader(){ var tr = '<tr>'; for(var prop in scope.datasource[0]){ //{name: 'David',street: '1234 Anywhere St.',age: 25,url: 'index.html'} //根據(jù)原始列名獲取別名,并考慮了是否顯示列的情況 var val = getColumnName(prop); if(val){ //visibleProps存儲的是原始列名 visibleProps.push(prop); tr += '<th>' + val + '</th>'; } } tr += '</tr>'; tr = '<thead>' + tr '</thead>'; return tr; } //加載行 function renderRows(){ var rows = ''; for(var i = 0, len = scope.datasource.length; i < len; i++){ rows += '<tr>'; var row = scope.datasource[i]; for(var prop in row){ //當(dāng)前遍歷的原始列名是否在visibleProps集合中,該集合存儲的是原始列名 if(visibleProps.indexOf(prop) > -1){ rows += '<td>' + row[prop] + '</td>'; } } rows += '</tr>'; } rows = '<tbody>' + rows + '</tbody>'; return rows; } //加載統(tǒng)計行 function renderTable(){ table += '<br /><div class="rowCount">' + scope.datasource.length + '行</div>'; element.html(table); table = ''; }
加載表頭的時候,用到了一個根據(jù)原始列名獲取別名的方法。
//根據(jù)原始列名獲取列的別名,并考慮是否隱藏列的情況 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方法中,用到了一個根據(jù)原始列名
//比如根據(jù)name屬性,這里得到[{name: 'Name'}] //[{name: 'Name'}, {street: 'Street'}, {age: 'Age'}, {url: 'URL', hidden: true}] function filterColumnMap(prop) { var val = scope.columnmap.filter(function(map) { if (map[prop]) { return true; } return false; }); return val; }
具體代碼如下:
(function(){ var tableHelper = fucntion(){ var template = '<div class="tableHelper"></div>', link = function(scope, element, attrs){ var headerCols = [], //表頭列們 tableStart = '<table>', tableEnd = '</table>', table = '', visibleProps = [],//可見列 sortCol = null,//排序列 sortDir =1; //監(jiān)視集合 sscope.$watchCollection('datasource', render); //給表頭th綁定事件 wireEvents(); //初始化表格 function render(){ if(scope.datasource && scope.datasource.length){ table += tableStart; table += renderHeader(); table += renderRows() + tableEnd; //加載統(tǒng)計行 renderTable(); } } //給th添加click事件 function wireEvents() { element.on('click', function(event){ if(event.srcElement.nodeName === 'TH'){ //獲取列的名稱 var val = event.srcElement.innerHTML; //根據(jù)列的別名獲取原始列名 var col = (scope.columnmap) ? getRawColumnName(val) : val; if(col){ //對該列進(jìn)行排序 sort(col); } } }); } //給某列排序 function sort(col){ if(sortCol === col){ sortDir = sortDir * -1; } sortCol = col; scope.datasource.sort(function(a,b){ if(a[col] > b[col]) return 1 * sortDir; if(a[col] < b[col]) return -1 * sortDir; return 0; }); //重新加載表格 render(); } //加載頭部 function renderHeader(){ var tr = '<tr>'; for(var prop in scope.datasource[0]){ //{name: 'David',street: '1234 Anywhere St.',age: 25,url: 'index.html'} //根據(jù)原始列名獲取別名,并考慮了是否顯示列的情況 var val = getColumnName(prop); if(val){ //visibleProps存儲的是原始列名 visibleProps.push(prop); tr += '<th>' + val + '</th>'; } } tr += '</tr>'; tr = '<thead>' + tr '</thead>'; return tr; } //加載行 function renderRows(){ var rows = ''; for(var i = 0, len = scope.datasource.length; i < len; i++){ rows += '<tr>'; var row = scope.datasource[i]; for(var prop in row){ //當(dāng)前遍歷的原始列名是否在visibleProps集合中,該集合存儲的是原始列名 if(visibleProps.indexOf(prop) > -1){ rows += '<td>' + row[prop] + '</td>'; } } rows += '</tr>'; } rows = '<tbody>' + rows + '</tbody>'; return rows; } //加載統(tǒng)計行 function renderTable(){ table += '<br /><div class="rowCount">' + scope.datasource.length + '行</div>'; element.html(table); table = ''; } //根據(jù)列的別名獲取原始列名 function getRawColumnName(friendlyCol) { var rawCol; //columnmap =[{name: 'Name'}, {street: 'Street'}, {age: 'Age'}, {url: 'URL', 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); } //比如根據(jù)name屬性,這里得到[{name: 'Name'}] //[{name: 'Name'}, {street: 'Street'}, {age: 'Age'}, {url: 'URL', hidden: true}] function filterColumnMap(prop) { var val = scope.columnmap.filter(function(map) { if (map[prop]) { return true; } return false; }); return val; } //根據(jù)原始列名獲取列的別名,并考慮是否隱藏列的情況 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', scope: { columnmap: '=', datasource: '=' }, link:link, template:template }; }; angular.module('directiveModule') .directive('tableHelper', tableHelper); }());
以上所述是小編給大家分享的AngularJS中的Directive自定義一個表格的相關(guān)知識,希望對大家有所幫助。
- AngularJS模糊查詢功能實現(xiàn)代碼(過濾內(nèi)容下拉菜單排序過濾敏感字符驗證判斷后添加表格信息)
- Angular實現(xiàn)的table表格排序功能完整示例
- AngularJS中table表格基本操作示例
- Angular將填入表單的數(shù)據(jù)渲染到表格的方法
- AngularJS實現(xiàn)表格的增刪改查(僅限前端)
- Angular.js與Bootstrap相結(jié)合實現(xiàn)表格分頁代碼
- 使用angularjs創(chuàng)建簡單表格
- AngularJS表格添加序號的方法
- Angular表格神器ui-grid應(yīng)用詳解
- AngularJS表格樣式簡單設(shè)置方法示例
- Angular實現(xiàn)較為復(fù)雜的表格過濾,刪除功能示例
相關(guān)文章
Angular2從搭建環(huán)境到開發(fā)步驟詳解
本文的內(nèi)容主要是想幫助那些想學(xué)習(xí)Angular2的朋友們,因為我自己在玩Angular2時碰到了不少坑,而且Angular2語法一直處于變化中,讓人很頭疼。不過也怪不了Anguar2,因為它現(xiàn)在是處于并長期處于alpha階段,下面就通過本文來學(xué)習(xí)Angular2的搭建環(huán)境和開發(fā)吧。2016-10-10Angular5中提取公共組件之radio list的實例代碼
這篇文章主要介紹了Angular5中提取公共組件之radio list的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07Angular 4 依賴注入學(xué)習(xí)教程之FactoryProvider的使用(四)
這篇文章主要給大家介紹了關(guān)于Angular 4 依賴注入之FactoryProvider使用的相關(guān)資料,文中介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Angular4具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06Angular中$state.go頁面跳轉(zhuǎn)并傳遞參數(shù)的方法
這篇文章主要介紹了angular中$state.go頁面跳轉(zhuǎn)并傳遞參數(shù)的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05Angular 2父子組件之間共享服務(wù)通信的實現(xiàn)
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件之間共享服務(wù)通信的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-07-07