UNPKG

2.56 kBJavaScriptView Raw
1/**
2 * ngTable: Table + Angular JS
3 *
4 * @author Vitalii Savchuk <esvit666@gmail.com>
5 * @url https://github.com/esvit/ng-table/
6 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
7 */
8import * as ng1 from 'angular';
9ngTableDynamic.$inject = [];
10/**
11 * A dynamic version of the {@link ngTable ngTable} directive that accepts a dynamic list of columns
12 * definitions to render
13 * @ngdoc directive
14 *
15 * @example
16 * ```html
17 * <table ng-table-dynamic="$ctrl.tableParams with $ctrl.cols" class="table">
18 * <tr ng-repeat="row in $data">
19 * <td ng-repeat="col in $columns">{{row[col.field]}}</td>
20 * </tr>
21 * </table>
22 * ```
23 */
24export function ngTableDynamic() {
25 return {
26 restrict: 'A',
27 priority: 1001,
28 scope: true,
29 controller: 'ngTableController',
30 compile: function (tElement) {
31 var row;
32 // IE 8 fix :not(.ng-table-group) selector
33 ng1.forEach(tElement.find('tr'), function (tr) {
34 tr = ng1.element(tr);
35 if (!tr.hasClass('ng-table-group') && !row) {
36 row = tr;
37 }
38 });
39 if (!row) {
40 return undefined;
41 }
42 ng1.forEach(row.find('td'), function (item) {
43 var el = ng1.element(item);
44 var getAttrValue = function (attr) {
45 return el.attr('x-data-' + attr) || el.attr('data-' + attr) || el.attr(attr);
46 };
47 // this used in responsive table
48 var titleExpr = getAttrValue('title');
49 if (!titleExpr) {
50 el.attr('data-title-text', '{{$columns[$index].titleAlt(this) || $columns[$index].title(this)}}');
51 }
52 var showExpr = el.attr('ng-if');
53 if (!showExpr) {
54 el.attr('ng-if', '$columns[$index].show(this)');
55 }
56 });
57 return function (scope, element, attrs, controller) {
58 var expr = controller.parseNgTableDynamicExpr(attrs.ngTableDynamic);
59 controller.setupBindingsToInternalScope(expr.tableParams);
60 controller.compileDirectiveTemplates();
61 scope.$watchCollection(expr.columns, function (newCols /*, oldCols*/) {
62 scope.$columns = controller.buildColumns(newCols);
63 controller.loadFilterData(scope.$columns);
64 });
65 };
66 }
67 };
68}
69//# sourceMappingURL=ngTableDynamic.directive.js.map
\No newline at end of file