blob: 3a754641cd23756db6e0bd7db6b4203e2d54e930 [file] [log] [blame]
Matteo Scandolo66351432016-03-24 15:29:52 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 3/24/16.
7 */
8
9(function () {
10 'use strict';
11
Matteo Scandolo5614b902016-04-14 15:34:12 -070012 /**
13 * @ngdoc overview
14 * @name xos.uiComponents.table
15 * @description A table component
16 **/
Matteo Scandolo1dab3e02016-04-14 11:46:30 -070017
Matteo Scandolo66351432016-03-24 15:29:52 -070018 angular.module('xos.uiComponents.table', [])
Matteo Scandolo5614b902016-04-14 15:34:12 -070019
20 /**
21 * @ngdoc directive
22 * @name xos.uiComponents.table.directive:xosTable
23 * @restrict E
24 * @description The xos-table directive
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070025 * @param {Object} config The configuration for the component.
26 * ```
27 * {
28 * columns: [
29 * {
30 * label: 'Human readable name',
31 * prop: 'Property to read in the model object'
32 * }
33 * ],
34 * classes: 'table table-striped table-bordered',
35 * actions: [ // if defined add an action column
36 {
37 label: 'delete',
38 icon: 'remove', // refers to bootstraps glyphicon
39 cb: (user) => { // receive the model
40 console.log(user);
41 },
42 color: 'red'
43 }
Matteo Scandolo92ca4552016-04-14 17:21:45 -070044 ],
45 filter: 'field', // can be by `field` or `fulltext`
46 order: true // whether to show ordering arrows
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070047 * }
48 * ```
49 * @param {Array} data The data that should be rendered
Matteo Scandolo5614b902016-04-14 15:34:12 -070050 * @element ANY
51 * @scope
52 * @example
53
54 <example module="sampleModule1">
55 <file name="index.html">
56 <div ng-controller="SampleCtrl1 as vm">
57 <xos-table data="vm.data" config="vm.config"></xos-table>
Matteo Scandolo5614b902016-04-14 15:34:12 -070058 </div>
59 </file>
60 <file name="script.js">
61 angular.module('sampleModule1', ['xos.uiComponents.table'])
62 .controller('SampleCtrl1', function(){
63 this.config = {
64 columns: [
65 {
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070066 label: 'First Name', // column title
67 prop: 'name' // property to read in the data array
Matteo Scandolo5614b902016-04-14 15:34:12 -070068 },
69 {
70 label: 'Last Name',
71 prop: 'lastname'
72 }
73 ]
74 };
75
76 this.data = [
77 {
78 name: 'John',
79 lastname: 'Doe'
80 },
81 {
82 name: 'Gili',
83 lastname: 'Fereydoun'
84 }
85 ]
86 });
87 </file>
88 </example>
89
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070090 <example module="sampleModule2">
Matteo Scandolo5614b902016-04-14 15:34:12 -070091 <file name="index.html">
92 <div ng-controller="SampleCtrl2 as vm">
93 <xos-table data="vm.data" config="vm.config"></xos-table>
Matteo Scandolo5614b902016-04-14 15:34:12 -070094 </div>
95 </file>
96 <file name="script.js">
97 angular.module('sampleModule2', ['xos.uiComponents.table'])
98 .controller('SampleCtrl2', function(){
99 this.config = {
100 columns: [
101 {
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700102 label: 'First Name', // column title
103 prop: 'name' // property to read in the data array
Matteo Scandolo5614b902016-04-14 15:34:12 -0700104 },
105 {
106 label: 'Last Name',
107 prop: 'lastname'
108 }
109 ],
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700110 classes: 'table table-striped table-condensed', // table classes, default to `table table-striped table-bordered`
111 actions: [ // if defined add an action column
Matteo Scandolo5614b902016-04-14 15:34:12 -0700112 {
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700113 label: 'delete', // label
114 icon: 'remove', // icons, refers to bootstraps glyphicon
115 cb: (user) => { // callback, get feeded with the full object
Matteo Scandolo5614b902016-04-14 15:34:12 -0700116 console.log(user);
117 },
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700118 color: 'red' // icon color
Matteo Scandolo5614b902016-04-14 15:34:12 -0700119 }
Matteo Scandolo92ca4552016-04-14 17:21:45 -0700120 ],
121 filter: 'field', // can be by `field` or `fulltext`
122 order: true
Matteo Scandolo5614b902016-04-14 15:34:12 -0700123 };
124
125 this.data = [
126 {
127 name: 'John',
128 lastname: 'Doe'
129 },
130 {
131 name: 'Gili',
132 lastname: 'Fereydoun'
133 }
134 ]
135 });
136 </file>
137 </example>
138
139 **/
140
Matteo Scandolo66351432016-03-24 15:29:52 -0700141 .directive('xosTable', function(){
142 return {
143 restrict: 'E',
144 scope: {
145 data: '=',
Matteo Scandolodcc65242016-04-14 12:06:50 -0700146 config: '='
Matteo Scandolo66351432016-03-24 15:29:52 -0700147 },
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700148 template: `
Matteo Scandolodcc65242016-04-14 12:06:50 -0700149 <!-- <pre>{{vm.data | json}}</pre> -->
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700150 <div class="row" ng-if="vm.config.filter == 'fulltext'">
151 <div class="col-xs-12">
152 <input
153 class="form-control"
154 placeholder="Type to search.."
155 type="text"
156 ng-model="vm.query"/>
157 </div>
158 </div>
Matteo Scandolodcc65242016-04-14 12:06:50 -0700159 <table ng-class="vm.classes" ng-show="vm.data.length > 0">
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700160 <thead>
161 <tr>
Matteo Scandolof32a2e92016-04-14 17:19:08 -0700162 <th ng-repeat="col in vm.columns">
163 {{col.label}}
164 <span ng-if="vm.config.order">
165 <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = false">
166 <i class="glyphicon glyphicon-chevron-up"></i>
167 </a>
168 <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = true">
169 <i class="glyphicon glyphicon-chevron-down"></i>
170 </a>
171 </span>
172 </th>
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700173 <th ng-if="vm.config.actions">Actions</th>
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700174 </tr>
175 </thead>
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700176 <tbody ng-if="vm.config.filter == 'field'">
177 <tr>
178 <td ng-repeat="col in vm.columns">
179 <input
180 class="form-control"
181 placeholder="Type to search by {{col.label}}"
182 type="text"
183 ng-model="vm.query[col.prop]"/>
184 </td>
185 <td ng-if="vm.config.actions"></td>
186 </tr>
187 </tbody>
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700188 <tbody>
Matteo Scandolof32a2e92016-04-14 17:19:08 -0700189 <tr ng-repeat="item in vm.data | filter:vm.query | orderBy:vm.orderBy:vm.reverse">
Matteo Scandolodcc65242016-04-14 12:06:50 -0700190 <td ng-repeat="col in vm.columns">{{item[col.prop]}}</td>
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700191 <td ng-if="vm.config.actions">
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700192 <a href=""
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700193 ng-repeat="action in vm.config.actions"
194 ng-click="action.cb(item)"
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700195 title="{{action.label}}">
196 <i
197 class="glyphicon glyphicon-{{action.icon}}"
198 style="color: {{action.color}};"></i>
199 </a>
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700200 </td>
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700201 </tr>
202 </tbody>
203 </table>
204 `,
Matteo Scandolo66351432016-03-24 15:29:52 -0700205 bindToController: true,
206 controllerAs: 'vm',
207 controller: function(){
Matteo Scandolodcc65242016-04-14 12:06:50 -0700208
209 if(!this.config){
210 throw new Error('[xosTable] Please provide a configuration via the "config" attribute');
211 }
212
213 if(!this.config.columns){
214 throw new Error('[xosTable] Please provide a columns list in the configuration');
215 }
216
217 this.columns = this.config.columns;
218 this.classes = this.config.classes || 'table table-striped table-bordered';
219
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700220 if(this.config.actions){
221
222 }
223
Matteo Scandolo66351432016-03-24 15:29:52 -0700224 }
225 }
226 })
227})();