blob: 891e96dee4e58c8b9894d7dea4060e1be5909a0e [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 }
44 ]
45 * }
46 * ```
47 * @param {Array} data The data that should be rendered
Matteo Scandolo5614b902016-04-14 15:34:12 -070048 * @element ANY
49 * @scope
50 * @example
51
52 <example module="sampleModule1">
53 <file name="index.html">
54 <div ng-controller="SampleCtrl1 as vm">
55 <xos-table data="vm.data" config="vm.config"></xos-table>
Matteo Scandolo5614b902016-04-14 15:34:12 -070056 </div>
57 </file>
58 <file name="script.js">
59 angular.module('sampleModule1', ['xos.uiComponents.table'])
60 .controller('SampleCtrl1', function(){
61 this.config = {
62 columns: [
63 {
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070064 label: 'First Name', // column title
65 prop: 'name' // property to read in the data array
Matteo Scandolo5614b902016-04-14 15:34:12 -070066 },
67 {
68 label: 'Last Name',
69 prop: 'lastname'
70 }
71 ]
72 };
73
74 this.data = [
75 {
76 name: 'John',
77 lastname: 'Doe'
78 },
79 {
80 name: 'Gili',
81 lastname: 'Fereydoun'
82 }
83 ]
84 });
85 </file>
86 </example>
87
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070088 <example module="sampleModule2">
Matteo Scandolo5614b902016-04-14 15:34:12 -070089 <file name="index.html">
90 <div ng-controller="SampleCtrl2 as vm">
91 <xos-table data="vm.data" config="vm.config"></xos-table>
Matteo Scandolo5614b902016-04-14 15:34:12 -070092 </div>
93 </file>
94 <file name="script.js">
95 angular.module('sampleModule2', ['xos.uiComponents.table'])
96 .controller('SampleCtrl2', function(){
97 this.config = {
98 columns: [
99 {
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700100 label: 'First Name', // column title
101 prop: 'name' // property to read in the data array
Matteo Scandolo5614b902016-04-14 15:34:12 -0700102 },
103 {
104 label: 'Last Name',
105 prop: 'lastname'
106 }
107 ],
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700108 classes: 'table table-striped table-condensed', // table classes, default to `table table-striped table-bordered`
109 actions: [ // if defined add an action column
Matteo Scandolo5614b902016-04-14 15:34:12 -0700110 {
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700111 label: 'delete', // label
112 icon: 'remove', // icons, refers to bootstraps glyphicon
113 cb: (user) => { // callback, get feeded with the full object
Matteo Scandolo5614b902016-04-14 15:34:12 -0700114 console.log(user);
115 },
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700116 color: 'red' // icon color
Matteo Scandolo5614b902016-04-14 15:34:12 -0700117 }
118 ]
119 };
120
121 this.data = [
122 {
123 name: 'John',
124 lastname: 'Doe'
125 },
126 {
127 name: 'Gili',
128 lastname: 'Fereydoun'
129 }
130 ]
131 });
132 </file>
133 </example>
134
135 **/
136
Matteo Scandolo66351432016-03-24 15:29:52 -0700137 .directive('xosTable', function(){
138 return {
139 restrict: 'E',
140 scope: {
141 data: '=',
Matteo Scandolodcc65242016-04-14 12:06:50 -0700142 config: '='
Matteo Scandolo66351432016-03-24 15:29:52 -0700143 },
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700144 template: `
Matteo Scandolodcc65242016-04-14 12:06:50 -0700145 <!-- <pre>{{vm.data | json}}</pre> -->
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700146 <div class="row" ng-if="vm.config.filter == 'fulltext'">
147 <div class="col-xs-12">
148 <input
149 class="form-control"
150 placeholder="Type to search.."
151 type="text"
152 ng-model="vm.query"/>
153 </div>
154 </div>
Matteo Scandolodcc65242016-04-14 12:06:50 -0700155 <table ng-class="vm.classes" ng-show="vm.data.length > 0">
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700156 <thead>
157 <tr>
Matteo Scandolof32a2e92016-04-14 17:19:08 -0700158 <th ng-repeat="col in vm.columns">
159 {{col.label}}
160 <span ng-if="vm.config.order">
161 <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = false">
162 <i class="glyphicon glyphicon-chevron-up"></i>
163 </a>
164 <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = true">
165 <i class="glyphicon glyphicon-chevron-down"></i>
166 </a>
167 </span>
168 </th>
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700169 <th ng-if="vm.config.actions">Actions</th>
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700170 </tr>
171 </thead>
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700172 <tbody ng-if="vm.config.filter == 'field'">
173 <tr>
174 <td ng-repeat="col in vm.columns">
175 <input
176 class="form-control"
177 placeholder="Type to search by {{col.label}}"
178 type="text"
179 ng-model="vm.query[col.prop]"/>
180 </td>
181 <td ng-if="vm.config.actions"></td>
182 </tr>
183 </tbody>
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700184 <tbody>
Matteo Scandolof32a2e92016-04-14 17:19:08 -0700185 <tr ng-repeat="item in vm.data | filter:vm.query | orderBy:vm.orderBy:vm.reverse">
Matteo Scandolodcc65242016-04-14 12:06:50 -0700186 <td ng-repeat="col in vm.columns">{{item[col.prop]}}</td>
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700187 <td ng-if="vm.config.actions">
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700188 <a href=""
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700189 ng-repeat="action in vm.config.actions"
190 ng-click="action.cb(item)"
Matteo Scandolo6a6586e2016-04-14 16:52:13 -0700191 title="{{action.label}}">
192 <i
193 class="glyphicon glyphicon-{{action.icon}}"
194 style="color: {{action.color}};"></i>
195 </a>
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700196 </td>
Matteo Scandolo1dab3e02016-04-14 11:46:30 -0700197 </tr>
198 </tbody>
199 </table>
200 `,
Matteo Scandolo66351432016-03-24 15:29:52 -0700201 bindToController: true,
202 controllerAs: 'vm',
203 controller: function(){
Matteo Scandolodcc65242016-04-14 12:06:50 -0700204
205 if(!this.config){
206 throw new Error('[xosTable] Please provide a configuration via the "config" attribute');
207 }
208
209 if(!this.config.columns){
210 throw new Error('[xosTable] Please provide a columns list in the configuration');
211 }
212
213 this.columns = this.config.columns;
214 this.classes = this.config.classes || 'table table-striped table-bordered';
215
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700216 if(this.config.actions){
217
218 }
219
Matteo Scandolo66351432016-03-24 15:29:52 -0700220 }
221 }
222 })
223})();