-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
38 lines (36 loc) · 1.11 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var app = angular.module('cmTable', []);
app
.controller('mainController', ['$scope', function($scope) {
$scope.dogSpecies = [
{name: 'scottish terrier', pattern: 'solid', size: 'small', personality: 'energetic'},
{name: 'golden retriever', pattern: 'solid', size: 'large'},
{name: 'dalmation', pattern: 'spotted', size: 'medium'}
];
}])
.directive('cmTable', [function() {
return {
restrict: 'A',
scope: {
cmTableData: '='
},
link: function(scope, elem, attrs) {
var $th = elem.find('th');
var attrsArray = [];
$($th).each(function(th) {
attrsArray.push($(this).attr('cm-table-param'));
});
var tbody = elem.find('tbody');
scope.cmTableData.forEach(function(item) {
var tr = angular.element('<tr></tr>');
tbody.append(tr);
attrsArray.forEach(function(attribute) {
tr.append('<td>' + item[attribute] + '</td>');
});
});
scope.sortBy = function sortBy(key) {
if(scope.prop === key) return scope.reverse = !scope.reverse;
scope.prop = key;
};
}
};
}]);