欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS中的Directive自定義一個表格

 更新時間:2016年01月25日 11:52:20   作者:Darren Ji  
本篇文章給大家介紹在angularjs中自定義一個有關(guān)表格的directive,涉及到angularjs directive相關(guān)知識,對本文感興趣的朋友一起學(xué)習(xí)吧

先給大家說下表格的需求:

● 表格結(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)知識,希望對大家有所幫助。

相關(guān)文章

  • Angular2從搭建環(huán)境到開發(fā)步驟詳解

    Angular2從搭建環(huán)境到開發(fā)步驟詳解

    本文的內(nèi)容主要是想幫助那些想學(xué)習(xí)Angular2的朋友們,因為我自己在玩Angular2時碰到了不少坑,而且Angular2語法一直處于變化中,讓人很頭疼。不過也怪不了Anguar2,因為它現(xiàn)在是處于并長期處于alpha階段,下面就通過本文來學(xué)習(xí)Angular2的搭建環(huán)境和開發(fā)吧。
    2016-10-10
  • angular2 NgModel模塊的具體使用方法

    angular2 NgModel模塊的具體使用方法

    這篇文章主要介紹了angular2 NgModel模塊的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Angular5中提取公共組件之radio list的實例代碼

    Angular5中提取公共組件之radio list的實例代碼

    這篇文章主要介紹了Angular5中提取公共組件之radio list的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • Angular2下使用pdf插件的方法詳解

    Angular2下使用pdf插件的方法詳解

    這篇文章主要給大家介紹了在Angular2下使用pdf插件的方法,使用這個插件是要實現(xiàn)一個pdf顯示的功能,文中介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • angular動態(tài)表單制作

    angular動態(tài)表單制作

    通過實例代碼給大家詳細(xì)講述了angular動態(tài)表單的制作方法,對此有需要的朋友參考下。
    2018-02-02
  • Angular 4 依賴注入學(xué)習(xí)教程之FactoryProvider的使用(四)

    Angular 4 依賴注入學(xué)習(xí)教程之FactoryProvider的使用(四)

    這篇文章主要給大家介紹了關(guān)于Angular 4 依賴注入之FactoryProvider使用的相關(guān)資料,文中介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Angular4具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-06-06
  • AngularJS實現(xiàn)多級下拉框

    AngularJS實現(xiàn)多級下拉框

    這篇文章主要為大家詳細(xì)介紹了AngularJS實現(xiàn)多級下拉框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Angular2的管道Pipe的使用方法

    Angular2的管道Pipe的使用方法

    本篇文章主要介紹了Angular 2的管道Pipe的使用方法,詳細(xì)的介紹了管道的定義和使用方法,具有一定的參考價值,有興趣的可以了解一下
    2017-11-11
  • Angular中$state.go頁面跳轉(zhuǎn)并傳遞參數(shù)的方法

    Angular中$state.go頁面跳轉(zhuǎn)并傳遞參數(shù)的方法

    這篇文章主要介紹了angular中$state.go頁面跳轉(zhuǎn)并傳遞參數(shù)的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-05-05
  • Angular 2父子組件之間共享服務(wù)通信的實現(xiàn)

    Angular 2父子組件之間共享服務(wù)通信的實現(xiàn)

    這篇文章主要給大家介紹了關(guān)于Angular 2父子組件之間共享服務(wù)通信的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-07-07

最新評論