Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
# GTreeTable 2
2
 
3
GTreeTable is extension of [Tweeter Bootstrap 3](http://getbootstrap.com) framework, which allows to use tree structure inside HTML table.
4
 
5
Thanks to the script it`s possible to create and manage tree with unlimited nesting.
6
 
7
Version 2 of code was completly rewrite and now it's possible to use such functions as: advanced cache, moving nodes by Drag & Drop method, or sorting directly from JavaScript level.
8
 
9
Test available on [demo project](http://gtreetable2.gilek.net).
10
 
11
![](http://gilek.net/images/gtt2-demo.png)
12
 
13
## Enviroment
14
 
15
Till now aplication was test on following browsers:
16
 
17
+ Mozilla Firefox 30,
18
+ Chrome 37,
19
+ Internet Explorer 11.
20
 
21
## Minimal installation
22
 
23
1. Firstly add required files:
24
 
25
    ```html
26
    <link rel="stylesheet" type="text/css" href="gtreetable.min.css" />
27
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
28
    <script type="text/javascript" src="bootstrap-gtreetable.js"></script>
29
    ```
30
 
31
2. Next create empty HTML table:
32
 
33
    ```html
34
    <table class="table gtreetable" id="gtreetable"><thead><tr><th>Category</th></tr></thead></table>
35
    ```
36
 
37
3. Inside of method `jQuery.ready` define basic configuration:
38
 
39
    ```javascript
40
    jQuery('#gtreetable').gtreetable({
41
      'source': function (id) {
42
	      return {
43
	        type: 'GET',
44
	        url: 'nodeChildren',
45
	        data: { 'id': id },
46
	        dataType: 'json',
47
	        error: function(XMLHttpRequest) {
48
	          alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
49
	        }
50
	      }
51
	    }
52
    });
53
    ```
54
 
55
More about `source` parameter and returned data format find in [configuration](#configuration) section.
56
 
57
## Functionality
58
 
59
### Actions
60
 
61
Actions defined in `defaultAction` and `actions` parameters are available after indicate or choosing the node.
62
`defualtActions` includes defualt actions, needed to CUD operations. It may be disabled by set null value.
63
`actions` defined the actions which may be add after last defined position in `defaultActions`.
64
 
65
More about action format find in [configuration](#configuration) section.
66
 
67
### CUD
68
 
69
Moment of saving and deleting node can goes together witch server communication by AJAX. Responsible actions: `onSave` and `onDelete`. It's should be functions that return [jQuery.ajax](http://api.jquery.com/jquery.ajax/) settings.
70
 
71
Example configuration:
72
 
73
```javascript
74
jQuery('#gtreetable').gtreetable({
75
  'source': function (id) {
76
    return {
77
      type: 'GET',
78
      url: 'nodeChildren',
79
      data: { 'id': id },
80
      dataType: 'json',
81
      error: function(XMLHttpRequest) {
82
        alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
83
      }
84
    }
85
  },
86
  'onSave':function (oNode) {
87
    return {
88
      type: 'POST',
89
      url: !oNode.isSaved() ? 'nodeCreate' : 'nodeUpdate?id=' + oNode.getId(),
90
      data: {
91
        parent: oNode.getParent(),
92
        name: oNode.getName(),
93
        position: oNode.getInsertPosition(),
94
        related: oNode.getRelatedNodeId()
95
      },
96
      dataType: 'json',
97
      error: function(XMLHttpRequest) {
98
        alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
99
      }
100
  	};
101
  },
102
  'onDelete':function (oNode) {
103
    return {
104
      type: 'POST',
105
      url: 'nodeDelete?id=' + oNode.getId(),
106
      dataType: 'json',
107
      error: function(XMLHttpRequest) {
108
        alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
109
      }
110
    };
111
  }
112
});
113
```
114
 
115
The new node may be added in various locations:
116
+ before chosen node (`before`),
117
+ after chosen node (`after`),
118
+ as a first child (`firstChild`),
119
+ as a last child (`lastChild`).
120
 
121
### Moving
122
 
123
Moving nodes may be realized by using Drag and Drop method.
124
By default mechanism is disabled, to activate it `draggable` parameter need to be set on true value. Also you need to define `onMove` event.
125
 
126
At the moment of node dragging, its new locations is marked by pointer with may be located:
127
+ before the node (`before`),
128
+ as a last child (`lastChild`),
129
+ after the node (`after`).
130
 
131
![](http://gilek.net/images/gtt2-pointer.png)
132
 
133
In this case, application is using additional libraries such as: [jQueryUI](http://jqueryui.com/) and [jQuery Browser Plugin](https://github.com/gabceb/jquery-browser-plugin) so it's necessary to include required files in the code:
134
 
135
```html
136
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
137
<script type="text/javascript" src="jquery.browser.js"></script>
138
```
139
Example configuration:
140
 
141
```javascript
142
jQuery('#gtreetable').gtreetable({
143
  'source': function (id) {
144
    return {
145
      type: 'GET',
146
      url: 'nodeChildren',
147
      data: { 'id': id },
148
      dataType: 'json',
149
      error: function(XMLHttpRequest) {
150
        alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
151
      }
152
    }
153
  },
154
  'draggable': true,
155
  'onMove': function (oSource, oDestination, position) {
156
    return {
157
      type: 'POST',
158
      url: 'nodeMove?id=' + oNode.getId(),
159
      data: {
160
        related: oDestination.getId(),
161
        position: position
162
      },
163
      dataType: 'json',
164
      error: function(XMLHttpRequest) {
165
        alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
166
      }
167
    };
168
  }
169
});
170
```
171
 
172
### Choosing
173
 
174
To choose the node it's needed to click on its name. Depending on `selectLimit` parameter it's possible to indicate one or more nodes.
175
 
176
In this case it's worth to pay attention on a few events triggered in the moment of:
177
+ selecting node (`onSelect`),
178
+ unselecting node (`onUnselect`),
179
+ when limit of selection is overflowed (`onSelectOverflow`).
180
 
181
More information about [configuration](#configuration):
182
 
183
### Sorting
184
 
185
Node sorting function inside of tree may be realized directly from JavaScript level. Only needed to define sorting method as `sort` parameter function.
186
 
187
Sorting operation is triggered during: expanding tree branches, adding new node and existing node name edition.
188
 
189
Working of sorting function is the same as in case of [table sorting](https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array/sort), so example of configuration may look as follow:
190
 
191
```javascript
192
jQuery('#gtreetable').gtreetable({
193
  'source': function (id) {
194
    return {
195
      type: 'GET',
196
      url: 'nodeChildren',
197
      data: { 'id': id },
198
      dataType: 'json',
199
      error: function(XMLHttpRequest) {
200
        alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
201
      }
202
    }
203
  },
204
  'sort': function (a, b) {
205
    var aName = a.name.toLowerCase();
206
    var bName = b.name.toLowerCase();
207
    return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
208
  }
209
});
210
```
211
 
212
### Nodes types
213
 
214
Depending on node type is possible to display additional icon next to its name. Nodes type definition is based on `types` parameter.
215
 
216
Example configuration:
217
 
218
```javascript
219
jQuery('#gtreetable').gtreetable({
220
  'source': function (id) {
221
    return {
222
      type: 'GET',
223
      url: 'nodeChildren',
224
      data: { 'id': id },
225
      dataType: 'json',
226
      error: function(XMLHttpRequest) {
227
        alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
228
      }
229
    }
230
  },
231
  'types': { default: 'glyphicon glyphicon-folder-open'}
232
});
233
```
234
 
235
Adding various node type is realized by `GTreeTableNode.add(String position, String type)` method.
236
 
237
![](http://gilek.net/images/gtt2-type.png)
238
 
239
### Translations
240
 
241
User interface elements by default are displayed in English. There is a possibility to change language by change `language` parameter and attaching appropriate files:
242
 
243
Example configuration:
244
 
245
```html
246
<script type="text/javascript" src="languages/bootstrap-gtreetable.pl.js"></script>
247
```
248
 
249
```javascript
250
jQuery('#gtreetable').gtreetable({
251
  'source': function (id) {
252
    return {
253
      type: 'GET',
254
      url: 'nodeChildren',
255
      data: { 'id': id },
256
      dataType: 'json',
257
      error: function(XMLHttpRequest) {
258
        alert(XMLHttpRequest.status+': '+XMLHttpRequest.responseText);
259
      }
260
    }
261
  },
262
  'language': 'pl'
263
});
264
```
265
 
266
In the moment when some position from translations can't be found then its values stays unchanged.
267
 
268
### Cache
269
 
270
In relation to 1.x version, cache mechanism was improved. It's possible to work in 3 levels:
271
+ 0 - mechanism off,
272
+ 1 - information about child nodes are stored in memory. After moving or CUD operation, redownload information from data base is required,
273
+ 2 - as in 1 level with the difference that all operation on nodes are mapping in cache.
274
 
275
There is possibility to force refesh data by pushing <kbd>Alt</kbd> in the moment of node expanding.
276
 
277
## <a name="configuration"></a>Configuration
278
 
279
### Parameters
280
 
281
+ `actions` (Array) - set of actions, which should be added after the last position defined in `defaultActions` parameter. More info about required data format is located in description of  `defaultActions` parameter.
282
 
283
+ `cache (Integer)` - define whether actual node state should be stored in cache. It's possible to work in 3 levels:
284
  + 0 - mechanism off,
285
  + 1 - partial mapping. In the moment of moving or CUD operation the state of depending nodes is resetting,
286
  + 2 - fully mapping.
287
 
288
+ `classes` (Object) - parameter consists set of CSS class using to build user interface.
289
 
290
+ `defaultActions` (Array) - set of default CUD actions. Parameter needs to be table consists of objects in following format:
291
 
292
    ```javascript
293
    {
294
      name: 'Action label',
295
      event: function (oNode, oManager) { } // code to execute
296
    }
297
    ```
298
 
299
    Separator (horizontal line) defined by following construction (object):
300
 
301
    ```javascript
302
    { divider: true }
303
    ```
304
 
305
    When action label is surrounded by bracket and preceded by $ i.e. `${actionEdit}` then its value is translated on `language` parameter language.
306
 
307
+ `dragCanExpand` (boolean) - define whether during node moving is possible to expand other nodes after choosing appropriate icon.
308
 
309
+ `draggable` (boolean) - define whether nodes can be moved. Parameter value changing on true is related with necessity of adding required [jQueryUI](http://jqueryui.com/) library:
310
  + core,
311
  + widget,
312
  + position,
313
  + mouse,
314
  + draggable,
315
  + droppable.
316
 
317
+ `inputWidth` (String) - width of field of adding / edition  node name.
318
 
319
+ `language` (String) - user interface language. Default English, change of language is related with necessity of adding translation file. In the moment when some translated position won't be found, then its value stays in English.
320
 
321
+ `manyroots` (boolean) - define whether it's possible to create multiple nodes roots.
322
 
323
+ `selectLimit` (Integer) - define nodes selection behavours:
324
  + > 1 - indicate exactly the same number of nodes,
325
  + 0 - selection disabled,
326
  + -1 - unlimited selection.
327
 
328
+ `nodeIndent` (Integer) - Distance between node and its container. The value is multiplied, depending on node level.
329
 
330
+ `nodeLevel` (Integer)
331
 
332
+ `nodesWrapper` (String) - define nodes wrapper property. Default `nodes`. [More info](https://github.com/gilek/bootstrap-gtreetable/issues/9).
333
 
334
+ `readonly` (boolean) - determines whether executing action on node is possible or not.
335
 
336
+ `showExpandIconOnEmpty` (boolean) - parameter set on true value means that expanding node icon stays visible all the time, even if there is no node child.
337
 
338
+ `sort` (callback (GTreeTableNode oNodeA, GTreeTableNode oNodeB)) - sorting function triggered in the moment of: displaying nodes, adding new one or its changing. Working of sorting function is the same as in case of [table sorting](https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array/sort).
339
 
340
	  Example of sorting by node name in the ascending order.
341
 
342
    ```javascript
343
    function (a, b) {
344
      var aName = a.name.toLowerCase();
345
      var bName = b.name.toLowerCase();
346
      return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
347
    }
348
    ```
349
 
350
 
351
+ `source` (callback (Integer id))<a name="source"></a> - function must return `jQuery.ajax` settings, responsible for getting nodes from data base.
352
 
353
    If ID = 0, then tree roots should be returned.
354
 
355
	  Information about nodes should be included in the object table in JSON format:
356
 
357
    ```JSON
358
    {
359
      "id": "node ID",
360
      "name": "node name",
361
      "level": "node level",
362
      "type": "node type"
363
    }
364
    ```
365
+ `template` (String)
366
 
367
+ `templateParts` (Object)
368
 
369
+ `types` (Object) - object consists relations between node types and its icon class i.e.
370
 
371
    ```javascript
372
    { default: "glyphicon glyphicon-folder-open" }
373
    ```
374
 
375
### Events
376
 
377
+ `onDelete(GTreeTableNode node)` - event triggering at the node deleting moment, must return `jQuery.ajax` settings.
378
 
379
+ `onMove(GTreeTableNode node, GTreeTableNode destination, string position)` - event triggering at the node moving moment, must return `jQuery.ajax` settings.
380
 
381
+ `onSave(GTreeTableNode node)` - event triggering at the node adding / edition moment. It must return `jQuery.ajax` settings.
382
 
383
+ `onSelect(GTreeTableNode node)` - event triggering at the node selecting moment.
384
 
385
+ `onSelectOverflow(GTreeTableNode node)` - event triggering when `selectLimit` parameter is positive number and selecting another node would be related with overflow of defined quantity.
386
 
387
+ `onUnselect(GTreeTableNode node)` - event triggering at the moment when node is unselected.
388
 
389
### Methods (chosen)
390
 
391
+ `GTreeTableManager.getSelectedNodes()` - returns table of selected nodes.
392
 
393
+ `GTreeTableNode.getPath(GTreeTableNode oNode)` -  returns table consists the node path i.e.:
394
 
395
    ```javascript
396
    ["Node name", "Parent node", "Main node"]
397
    ```
398
 
399
## Server side
400
 
401
GTreeTable offer supports only in JavaScript level. Special extensions of Yii framework [yii2-gtreetable](https://github.com/gilek/yii2-gtreetable) or [yii-gtreetable](https://github.com/gilek/yii-gtreetable) can be use as server side application. Even if you don't use this software every day, don't worry in the near future will be prepared special library (written in native PHP), designed to realize this task.