forked from hkalbertl/jquery.appendGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.appendGrid-1.2.0.js
969 lines (969 loc) · 45.2 KB
/
jquery.appendGrid-1.2.0.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*!
* jQuery appendGrid v1.2.0
* https://appendgrid.apphb.com/
*
* Copyright 2013 Albert L.
* Dual licensed under the GPL (http://www.gnu.org/licenses/gpl.html)
* and MIT (http://www.opensource.org/licenses/mit-license.php) licenses.
*
* Depends:
* jQuery v1.9.1+
* jquery UI v1.10.2+
*/
(function ($) {
// The default initial options.
var _defaultInitOptions = {
// The text as table caption, set null to disable caption generation.
caption: null,
// The total number of empty rows generated when init the grid. This will be ignored if `initData` is assigned.
initRows: 3,
// An array of data to be filled after initialized the grid.
initData: null,
// The width in pixel of first cell that displaying the row sequence number.
firstCellWidth: 32,
// The width in pixel of last cell that containing add/remove/move up/move down buttons.
lastCellWidth: 89,
// Array of column options.
columns: null,
// Labels or messages used in grid.
i18n: null,
// The ID prefix of controls generated inside the grid. Table ID will be used if not defined.
idPrefix: null,
// Enable row dragging by using jQuery UI sortable on grid rows.
rowDragging: false,
// Hide the move up/down buttons at the end of rows.
hideMoveUpDown: false,
// The callback function to be triggered after data loaded to grid.
dataLoaded: null,
// The callback function to be triggered after new row appended.
afterRowAppended: null,
// The callback function to be triggered after new row inserted.
afterRowInserted: null,
// The callback function to be triggered after grid row swapped.
afterRowSwapped: null,
// The callback function to be triggered before grid row remove.
beforeRowRemove: null,
// The callback function to be triggered after grid row removed.
afterRowRemoved: null,
// (Private) The UniqueIndex accumulate counter.
_uniqueIndex: 0,
// (Private) The row order array.
_rowOrder: null,
// (Private) Indicate data is loaded or not.
_isDataLoaded: false,
// (Private) Visible column count for internal calculation.
_visibleCount: 0
};
// Default column options.
var _defaultColumnOptions = {
// Type of column control.
type: 'text',
// Name of column.
name: null,
// Default value.
value: null,
// Display text on the header section.
display: null,
// Extra CSS setting to be added to display text.
displayCss: null,
// Extra CSS setting to be added to the control container table cell.
cellCss: null,
// Extra attributes to be added to the control.
ctrlAttr: null,
// Extra properties to be added to the control.
ctrlProp: null,
// Extra CSS to be added to the control.
ctrlCss: null,
// Extra name of class to be added to the control.
ctrlClass: null,
// The available option for building `select` type control.
ctrlOptions: null,
// Options for initalize jQuery UI widget.
uiOption: null,
// Options for initalize jQuery UI tooltip.
uiTooltip: null,
// Callback function to build custom type control.
customBuilder: null,
// Callback function to get control value.
customGetter: null,
// Callback function to set control value.
customSetter: null,
// The `OnClick` event callback of control.
onClick: null,
// The `OnChange` event callback of control.
onChange: null
};
var _textResources = { append: 'Append Row', removeLast: 'Remove Last Row', insert: 'Insert Row Above', remove: 'Remove Current Row', moveUp: 'Move Up', moveDown: 'Move Down', rowDrag: 'Sort Row' };
var _methods = {
init: function (options) {
var target = this;
var tbWhole, tbHead, tbBody, tbFoot, tbRow, tbCell;
if (target.length > 0) {
// Check mandatory paramters included
if (!$.isArray(options.columns) || options.columns.length == 0) {
alert('Cannot initial grid without column information!');
return target;
}
// Check target element is table or not
tbWhole = target[0];
if (isEmpty(tbWhole.tagName) || tbWhole.tagName != 'TABLE') {
alert('Cannot initial grid on element other than TABLE!');
return target;
}
// Generate settings
var settings = $.extend({}, _defaultInitOptions, options);
if ($.isPlainObject(options.i18n))
settings.textResources = $.extend({}, _textResources, options.i18n);
else
settings.textResources = $.extend({}, _textResources);
settings._rowOrder = [];
// Check `idPrefix` is defined
if (isEmpty(settings.idPrefix)) {
// Check table ID defined
if (isEmpty(tbWhole.id) || tbWhole.id == '') {
// Generate an ID using current time
settings.idPrefix = 'ag' + new Date().getTime();
}
else {
settings.idPrefix = tbWhole.id;
}
}
// Create thead and tbody
tbHead = document.createElement('thead');
tbHead.className = 'ui-widget-header';
tbBody = document.createElement('tbody');
tbBody.className = 'ui-widget-content';
tbFoot = document.createElement('tfoot');
tbFoot.className = 'ui-widget-header';
// Remove existing content and append new thead and tbody
$(tbWhole).empty().addClass('appendGrid').addClass('ui-widget').append(tbHead, tbBody, tbFoot);
// Handle header row
settings._visibleCount = 0;
tbRow = tbHead.insertRow(-1);
tbCell = tbRow.insertCell(-1);
tbCell.className = 'ui-widget-header';
$(tbCell).width(settings.firstCellWidth);
for (var z = 0; z < settings.columns.length; z++) {
// Assign default setting
var columnOpt = $.extend({}, _defaultColumnOptions, settings.columns[z]);
settings.columns[z] = columnOpt;
// Skip hidden
if (settings.columns[z].type != 'hidden') {
settings._visibleCount++;
tbCell = tbRow.insertCell(-1);
tbCell.className = 'ui-widget-header';
$(tbCell).text(settings.columns[z].display);
if (settings.columns[z].displayCss) $(tbCell).css(settings.columns[z].displayCss);
}
}
tbCell = tbRow.insertCell(-1);
tbCell.className = 'ui-widget-header';
$(tbCell).width(settings.lastCellWidth);
// Add caption when defined
if (settings.caption) {
tbRow = tbHead.insertRow(0);
tbCell = tbRow.insertCell(-1);
tbCell.className = 'ui-state-active caption';
tbCell.colSpan = 2 + settings._visibleCount;
$(tbCell).text(settings.caption);
}
// Handle footer row
tbRow = tbFoot.insertRow(-1);
tbCell = tbRow.insertCell(-1);
tbCell.colSpan = 2 + settings._visibleCount;
$(tbCell).append($('<input/>').attr({ type: 'hidden', id: settings.idPrefix + '_rowOrder', name: settings.idPrefix + '_rowOrder' }).addClass('row-order'));
$(tbCell).append($('<button/>').attr({ type: 'button', title: settings.textResources.append }).button({ icons: { primary: 'ui-icon-plusthick' }, text: false }).click(function () { insertRow(tbWhole, 1, null, null); }));
$(tbCell).append($('<button/>').attr({ type: 'button', title: settings.textResources.removeLast }).button({ icons: { primary: 'ui-icon-closethick' }, text: false }).click(function () { removeRow(tbWhole, null, this.value, false); }));
// Enable dragging
if (settings.rowDragging) {
$(tbBody).sortable({
axis: 'y',
containment: tbWhole,
handle: '.rowDrag',
helper: function (e, tr) {
var org = tr.children();
var helper = tr.clone();
helper.children().each(function (index) {
$(this).width(org.eq(index).width());
});
return helper;
},
update: function (event, ui) {
var uniqueIndex = ui.item[0].id.substring(ui.item[0].id.lastIndexOf('_') + 1);
var tbRowIndex = ui.item[0].rowIndex - $('tr', tbHead).length;
gridRowDragged(tbWhole, ui.originalPosition.top > ui.position.top, uniqueIndex, tbRowIndex);
}
});
}
// Save options
$(tbWhole).data('appendGrid', settings);
if ($.isArray(options.initData)) {
// Load data if initData is array
loadData(tbWhole, options.initData, true);
} else {
// Add empty rows
$(tbWhole).appendGrid('appendRow', settings.initRows);
}
}
return target;
},
isReady: function () {
var target = this, result = false;
if (target.length > 0) {
var settings = target.first().data('appendGrid');
if (settings) {
result = true;
}
}
return result;
},
isDataLoaded: function () {
var target = this, result = null;
if (this.length == 1) {
var settings = target.data('appendGrid');
if (settings) {
return settings._isDataLoaded;
}
else {
return false;
}
}
else {
return false;
}
},
load: function (records) {
var target = this;
if (target.length > 0) {
loadData(target[0], records, false);
}
return target;
},
appendRow: function (numOfRow) {
return this.appendGrid('insertRow', numOfRow);
},
insertRow: function (numOfRow, rowIndex, callerUniqueIndex) {
var target = this;
if (target.length > 0) {
var tbWhole = target[0], insertResult = null;
var settings = $(tbWhole).data('appendGrid');
if (!settings) {
alert('`appendGrid` does not initialized');
}
else {
// Define variables
insertResult = insertRow(tbWhole, numOfRow || 1, rowIndex, callerUniqueIndex);
// Reorder sequence as needed
if ($.isNumeric(rowIndex) || $.isNumeric(callerUniqueIndex)) {
// Sort sequence
sortSequence(tbWhole, insertResult.rowIndex);
// Move focus
var insertUniqueIndex = settings._rowOrder[insertResult.addedRows[0]];
$('#' + settings.idPrefix + '_Insert_' + insertUniqueIndex, tbWhole).focus();
}
}
}
return target;
},
removeRow: function (rowIndex, uniqueIndex) {
var target = this, tbodyIndex = -1;
if (target.length > 0) {
var tbWhole = target[0], tbHead, tbBody, tbRow;
var settings = $(tbWhole).data('appendGrid');
if (!settings) {
alert('`appendGrid` does not initialized');
}
else if (settings._rowOrder.length > 0) {
removeRow(tbWhole, rowIndex, uniqueIndex, true);
}
}
return target;
},
moveUpRow: function (rowIndex, uniqueIndex) {
var target = this, tbodyIndex = -1;
if (target.length > 0) {
var tbWhole = target[0], tbBody, trTarget, trSwap, swapSeq;
var settings = $(tbWhole).data('appendGrid');
if (!settings) {
alert('`appendGrid` does not initialized');
}
else {
var oldIndex = null;
tbBody = tbWhole.getElementsByTagName('tbody')[0];
if ($.isNumeric(rowIndex) && rowIndex > 0 && rowIndex < settings._rowOrder.length) {
oldIndex = rowIndex;
uniqueIndex = settings._rowOrder[rowIndex];
trTarget = document.getElementById(settings.idPrefix + '_Row_' + uniqueIndex, tbWhole);
} else if ($.isNumeric(uniqueIndex)) {
oldIndex = findRowIndex(uniqueIndex, settings);
trTarget = document.getElementById(settings.idPrefix + '_Row_' + uniqueIndex, tbWhole);
}
if (oldIndex != null && oldIndex > 0) {
// Get row to swap
trSwap = document.getElementById(settings.idPrefix + '_Row_' + settings._rowOrder[oldIndex - 1], tbWhole);
// Remove current row
tbBody.removeChild(trTarget);
// Insert before the above row
tbBody.insertBefore(trTarget, trSwap);
// Update rowOrder
settings._rowOrder[oldIndex] = settings._rowOrder[oldIndex - 1];
settings._rowOrder[oldIndex - 1] = uniqueIndex;
// Update row label
swapSeq = $('td.first', trSwap).html();
$('td.first', trSwap).html($('td.first', trTarget).html());
$('td.first', trTarget).html(swapSeq)
// Save setting
saveSetting(tbWhole, settings);
// Change focus
$('td.last button.moveUp', trTarget).removeClass('ui-state-hover').blur();
$('td.last button.moveUp', trSwap).focus();
// Trigger event
if (settings.afterRowSwapped) {
settings.afterRowSwapped(tbWhole, oldIndex, oldIndex - 1);
}
}
}
}
return target;
},
moveDownRow: function (rowIndex, uniqueIndex) {
var target = this, tbodyIndex = -1;
if (target.length > 0) {
var tbWhole = target[0], tbBody, trTarget, trSwap, swapSeq;
var settings = $(tbWhole).data('appendGrid');
if (!settings) {
alert('`appendGrid` does not initialized');
}
else {
var oldIndex = null;
tbBody = tbWhole.getElementsByTagName('tbody')[0];
if ($.isNumeric(rowIndex) && rowIndex >= 0 && rowIndex < settings._rowOrder.length - 1) {
oldIndex = rowIndex;
uniqueIndex = settings._rowOrder[rowIndex];
trTarget = document.getElementById(settings.idPrefix + '_Row_' + uniqueIndex, tbWhole);
} else if ($.isNumeric(uniqueIndex)) {
oldIndex = findRowIndex(uniqueIndex, settings);
trTarget = document.getElementById(settings.idPrefix + '_Row_' + uniqueIndex, tbWhole);
}
if (oldIndex != null && oldIndex != settings._rowOrder.length - 1) {
// Get row to swap
trSwap = document.getElementById(settings.idPrefix + '_Row_' + settings._rowOrder[oldIndex + 1], tbWhole);
// Remove current row
tbBody.removeChild(trSwap);
// Insert before the above row
tbBody.insertBefore(trSwap, trTarget);
// Update rowOrder
settings._rowOrder[oldIndex] = settings._rowOrder[oldIndex + 1];
settings._rowOrder[oldIndex + 1] = uniqueIndex;
// Update row label
swapSeq = $('td.first', trSwap).html();
$('td.first', trSwap).html($('td.first', trTarget).html());
$('td.first', trTarget).html(swapSeq)
// Save setting
saveSetting(tbWhole, settings);
// Change focus
$('td.last button.moveDown', trTarget).removeClass('ui-state-hover').blur();
$('td.last button.moveDown', trSwap).focus();
// Trigger event
if (settings.afterRowSwapped) {
settings.afterRowSwapped(tbWhole, oldIndex, oldIndex + 1);
}
}
}
}
return target;
},
getRowCount: function () {
var target = this;
if (target.length > 0) {
var settings = target.data('appendGrid');
if (settings) {
return settings._rowOrder.length;
}
else {
alert('`appendGrid` does not initialized');
}
}
else {
alert('Cannot get values on multiple grid');
}
return null;
},
getUniqueIndex: function (rowIndex) {
var target = this;
if (target.length > 0 && rowIndex >= 0) {
var settings = target.data('appendGrid');
if (settings) {
if (rowIndex < settings._rowOrder.length) {
return settings._rowOrder[rowIndex];
}
}
else {
alert('`appendGrid` does not initialized');
}
}
return null;
},
getRowIndex: function (uniqueIndex) {
var target = this;
if (target.length > 0) {
var settings = target.data('appendGrid');
if (settings) {
for (var z = 0; z < settings._rowOrder.length; z++) {
if (settings._rowOrder[z] == uniqueIndex) {
return z;
}
}
return null;
}
else {
alert('`appendGrid` does not initialized');
}
}
return null;
},
getRowValue: function (rowIndex, uniqueIndex, loopIndex) {
var target = this, result = null;
if (target.length > 0) {
var settings = target.data('appendGrid');
if (settings) {
if ($.isNumeric(rowIndex) && rowIndex >= 0 && rowIndex < settings._rowOrder.length) {
uniqueIndex = settings._rowOrder[rowIndex];
}
if (!isEmpty(uniqueIndex)) {
result = getRowValue(settings, uniqueIndex, loopIndex);
}
}
else {
alert('`appendGrid` does not initialized');
}
}
return result;
},
getAllValue: function (objectMode) {
var target = this, result = null, rowValue;
if (target.length > 0) {
var settings = $(target).data('appendGrid');
if (settings) {
// Prepare result based on objectMode setting
result = objectMode ? {} : [];
// Process on each rows
for (var z = 0; z < settings._rowOrder.length; z++) {
if (objectMode) {
rowValue = getRowValue(settings, settings._rowOrder[z], z);
$.extend(result, rowValue)
} else {
rowValue = getRowValue(settings, settings._rowOrder[z]);
result.push(rowValue);
}
}
if (objectMode) {
result['_RowCount'] = settings._rowOrder.length;
}
}
}
return result;
},
getCtrlValue: function (name, rowIndex) {
var target = this;
if (target.length > 0) {
settings = target.data('appendGrid');
if (settings && rowIndex >= 0 && rowIndex < settings._rowOrder.length) {
for (var z = 0; z < settings.columns.length; z++) {
if (settings.columns[z].name === name) {
return getCtrlValue(settings, z, settings._rowOrder[rowIndex]);
}
}
}
}
return null;
},
setCtrlValue: function (name, rowIndex, value) {
var target = this;
if (target.length > 0) {
var tbWhole = this, settings = $(this).data('appendGrid');
if (settings && rowIndex >= 0 && rowIndex < settings._rowOrder.length) {
for (var z = 0; z < settings.columns.length; z++) {
if (settings.columns[z].name == name) {
setCtrlValue(settings, z, settings._rowOrder[rowIndex], value);
break;
}
}
}
}
return target;
},
getCellCtrl: function (name, uniqueIndex) {
var target = this, result = null;
if (target.length == 1) {
settings = target.data('appendGrid');
if (!settings) {
alert('`appendGrid` does not initialized');
}
else {
for (var z = 0; z < settings.columns.length; z++) {
if (settings.columns[z].name === name) {
return getCellCtrl(settings.columns[z].type, settings.idPrefix, name, uniqueIndex);
break;
}
}
}
}
return null;
},
getCellCtrlByRowIndex: function (name, rowIndex) {
var target = this, result = null;
if (target.length == 1) {
settings = target.data('appendGrid');
if (!settings) {
alert('`appendGrid` does not initialized');
}
else {
if (rowIndex >= 0 && rowIndex < settings._rowOrder.length) {
var uniqueIndex = settings._rowOrder[rowIndex];
for (var z = 0; z < settings.columns.length; z++) {
if (settings.columns[z].name === name) {
return getCellCtrl(settings.columns[z].type, settings.idPrefix, name, uniqueIndex);
}
}
}
}
}
return null;
},
setCellCtrlByRowIndex: function (name, rowIndex, value) {
var target = this, result = null;
if (target.length == 1) {
settings = target.data('appendGrid');
if (!settings) {
alert('`appendGrid` does not initialized');
}
else {
if (rowIndex >= 0 && rowIndex < settings._rowOrder.length) {
var uniqueIndex = settings._rowOrder[rowIndex];
for (var z = 0; z < settings.columns.length; z++) {
if (settings.columns[z].name === name) {
return setCellCtrl(settings.columns[z].type, settings.idPrefix, name, uniqueIndex, value);
}
}
}
}
}
return null;
},
getRowOrder: function () {
var target = this, result = null;
if (this.length == 1) {
var settings = target.data('appendGrid');
if (settings) {
result = settings._rowOrder.slice();
}
else {
alert('`appendGrid` does not initialized');
}
}
else {
alert('Cannot get values on multiple grid');
}
return result;
}
};
function insertRow(tbWhole, numOfRow, rowIndex, callerUniqueIndex) {
// Define variables
var settings = $(tbWhole).data('appendGrid');
var addedRows = [], parentIndex = null, uniqueIndex, ctrl, hidden = [];
var tbHead = tbWhole.getElementsByTagName('thead')[0];
var tbBody = tbWhole.getElementsByTagName('tbody')[0];
// Check parent row
if ($.isNumeric(callerUniqueIndex)) {
for (var z = 0; z < settings._rowOrder.length; z++) {
if (settings._rowOrder[z] == callerUniqueIndex) {
rowIndex = z;
if (z != 0) parentIndex = z - 1;
break;
}
}
}
else if ($.isNumeric(rowIndex)) {
if (rowIndex >= settings._rowOrder.length) {
rowIndex = null;
} else {
parentIndex = rowIndex - 1;
}
}
else if (settings._rowOrder.length != 0) {
rowIndex = null;
parentIndex = settings._rowOrder.length - 1;
}
// Add total number of row
for (var z = 0; z < numOfRow; z++) {
// Update variables
settings._uniqueIndex++;
uniqueIndex = settings._uniqueIndex;
hidden.length = 0;
// Check row insert index
if ($.isNumeric(rowIndex)) {
settings._rowOrder.splice(rowIndex, 0, uniqueIndex);
tbRow = tbBody.insertRow(rowIndex);
addedRows.push(rowIndex);
}
else {
settings._rowOrder.push(uniqueIndex);
tbRow = tbBody.insertRow(-1);
addedRows.push(settings._rowOrder.length - 1);
}
tbRow.id = settings.idPrefix + '_Row_' + uniqueIndex;
// Add row number
tbCell = tbRow.insertCell(-1);
$(tbCell).addClass('ui-widget-content first').text(settings._rowOrder.length).data('appendGrid', uniqueIndex);
// Process on each columns
for (var y = 0; y < settings.columns.length; y++) {
// Skip hidden
if (settings.columns[y].type == 'hidden') {
hidden.push(y);
continue;
}
// Insert cell
tbCell = tbRow.insertCell(-1);
tbCell.className = 'ui-widget-content';
if (settings.columns[y].cellCss != null) $(tbCell).css(settings.columns[y].cellCss);
// Check control type
ctrl = null;
if (settings.columns[y].type == 'custom') {
if (typeof (settings.columns[y].customBuilder) == 'function') {
ctrl = settings.columns[y].customBuilder(tbCell, settings.idPrefix, settings.columns[y].name, uniqueIndex);
}
}
else if (settings.columns[y].type == 'select') {
ctrl = document.createElement('select');
ctrl.id = settings.idPrefix + '_' + settings.columns[y].name + '_' + uniqueIndex;
ctrl.name = ctrl.id;
// Build option list
if ($.isArray(settings.columns[y].ctrlOptions)) {
// For array type option list
if (settings.columns[y].ctrlOptions.length > 0) {
if ($.isPlainObject(settings.columns[y].ctrlOptions[0])) {
for (var x = 0; x < settings.columns[y].ctrlOptions.length; x++) {
ctrl.options[ctrl.options.length] = new Option(settings.columns[y].ctrlOptions[x].label, settings.columns[y].ctrlOptions[x].value);
}
}
else {
for (var x = 0; x < settings.columns[y].ctrlOptions.length; x++) {
ctrl.options[ctrl.options.length] = new Option(settings.columns[y].ctrlOptions[x], settings.columns[y].ctrlOptions[x]);
}
}
}
}
else if ($.isPlainObject(settings.columns[y].ctrlOptions)) {
// For plain object type option list
for (var x in settings.columns[y].ctrlOptions) {
ctrl.options[ctrl.options.length] = new Option(settings.columns[y].ctrlOptions[x], x);
}
}
else if (typeof (settings.columns[y].ctrlOptions) == 'string') {
// For string type option list
var arrayOpt = settings.columns[y].ctrlOptions.split(';');
for (var x = 0; x < arrayOpt.length; x++) {
var eqIndex = arrayOpt[x].indexOf(':');
if (-1 == eqIndex) {
ctrl.options[ctrl.options.length] = new Option(arrayOpt[x], arrayOpt[x]);
} else {
ctrl.options[ctrl.options.length] = new Option(arrayOpt[x].substring(eqIndex + 1, arrayOpt[x].length), arrayOpt[x].substring(0, eqIndex));
}
}
}
tbCell.appendChild(ctrl);
}
else if (settings.columns[y].type == 'checkbox') {
ctrl = document.createElement('input');
ctrl.type = 'checkbox';
ctrl.id = settings.idPrefix + '_' + settings.columns[y].name + '_' + uniqueIndex;
ctrl.name = ctrl.id;
ctrl.value = 1;
tbCell.appendChild(ctrl);
tbCell.style.textAlign = 'center';
}
else if (-1 != settings.columns[y].type.search(/^(color|date|datetime|datetime\-local|email|month|number|range|search|tel|time|url|week)$/)) {
ctrl = document.createElement('input');
try {
ctrl.type = settings.columns[y].type;
}
catch (err) { }
ctrl.id = settings.idPrefix + '_' + settings.columns[y].name + '_' + uniqueIndex;
ctrl.name = ctrl.id;
tbCell.appendChild(ctrl);
}
else {
// Generate text input
ctrl = document.createElement('input');
ctrl.type = 'text';
ctrl.id = settings.idPrefix + '_' + settings.columns[y].name + '_' + uniqueIndex;
ctrl.name = ctrl.id;
tbCell.appendChild(ctrl);
// Handle UI widget
if (settings.columns[y].type == 'ui-datepicker') {
$(ctrl).datepicker(settings.columns[y].uiOption);
} else if (settings.columns[y].type == 'ui-spinner') {
$(ctrl).spinner(settings.columns[y].uiOption);
} else if (settings.columns[y].type == 'ui-autocomplete') {
$(ctrl).autocomplete(settings.columns[y].uiOption);
}
}
// Add extra control properties
if (settings.columns[y].type != 'custom') {
// Add control attributes as needed
if (settings.columns[y].ctrlAttr != null) $(ctrl).attr(settings.columns[y].ctrlAttr);
// Add control properties as needed
if (settings.columns[y].ctrlProp != null) $(ctrl).prop(settings.columns[y].ctrlProp);
// Add control CSS as needed
if (settings.columns[y].ctrlCss != null) $(ctrl).css(settings.columns[y].ctrlCss);
// Add control class as needed
if (settings.columns[y].ctrlClass != null) $(ctrl).addClass(settings.columns[y].ctrlClass);
// Add jQuery UI tooltip as needed
if (settings.columns[y].uiTooltip) $(ctrl).tooltip(settings.columns[y].uiTooltip);
// Add control events as needed
if (typeof (settings.columns[y].onClick) == 'function') $(ctrl).click({ caller: tbWhole, callback: settings.columns[y].onClick, uniqueIndex: uniqueIndex }, function (evt) { evt.data.callback(evt, $(evt.data.caller).appendGrid('getRowIndex', evt.data.uniqueIndex)); });
if (typeof (settings.columns[y].onChange) == 'function') $(ctrl).change({ caller: tbWhole, callback: settings.columns[y].onChange, uniqueIndex: uniqueIndex }, function (evt) { evt.data.callback(evt, $(evt.data.caller).appendGrid('getRowIndex', evt.data.uniqueIndex)); });
}
// Set default value
if (!isEmpty(settings.columns[y].value)) {
setCtrlValue(settings, y, uniqueIndex, settings.columns[y].value);
}
}
// Add button cell
tbCell = tbRow.insertCell(-1);
tbCell.className = 'ui-widget-content last';
$(tbCell).append($('<button/>').addClass('insert').val(uniqueIndex).attr({ id: settings.idPrefix + '_Insert_' + uniqueIndex, type: 'button', title: settings.textResources.insert, tabindex: -1 }).button({ icons: { primary: 'ui-icon-arrowreturnthick-1-w' }, text: false }).click(function () {
$(tbWhole).appendGrid('insertRow', 1, null, this.value);
}));
$(tbCell).append($('<button/>').addClass('delete').val(uniqueIndex).attr({ id: settings.idPrefix + '_Delete_' + uniqueIndex, type: 'button', title: settings.textResources.remove, tabindex: -1 }).button({ icons: { primary: 'ui-icon-trash' }, text: false }).click(function () {
removeRow(tbWhole, null, this.value, false);
}));
if (!settings.hideMoveUpDown) {
$(tbCell).append($('<button/>').addClass('moveUp').val(uniqueIndex).attr({ id: settings.idPrefix + '_MoveUp_' + uniqueIndex, type: 'button', title: settings.textResources.moveUp, tabindex: -1 }).button({ icons: { primary: 'ui-icon-arrowthick-1-n' }, text: false }).click(function () {
$(tbWhole).appendGrid('moveUpRow', null, this.value);
}));
$(tbCell).append($('<button/>').addClass('moveDown').val(uniqueIndex).attr({ id: settings.idPrefix + '_MoveDown_' + uniqueIndex, type: 'button', title: settings.textResources.moveDown, tabindex: -1 }).button({ icons: { primary: 'ui-icon-arrowthick-1-s' }, text: false }).click(function () {
$(tbWhole).appendGrid('moveDownRow', null, this.value);
}));
}
// Handle row dragging
if (settings.rowDragging) {
$(tbCell).append($('<div/>').attr('title', settings.textResources.rowDrag).addClass('rowDrag ui-state-default ui-corner-all').append($('<div/>').addClass('ui-icon ui-icon-carat-2-n-s')));
}
// Add hidden
for (var y = 0; y < hidden.length; y++) {
ctrl = document.createElement('input');
ctrl.id = settings.idPrefix + '_' + settings.columns[hidden[y]].name + '_' + uniqueIndex;
ctrl.type = 'hidden';
tbCell.appendChild(ctrl);
}
}
// Save setting
saveSetting(tbWhole, settings);
// Trigger events
if ($.isNumeric(rowIndex)) {
if (typeof (settings.afterRowInserted) == 'function') {
settings.afterRowInserted(tbWhole, parentIndex, addedRows);
}
}
else {
if (typeof (settings.afterRowAppended) == 'function') {
settings.afterRowAppended(tbWhole, parentIndex, addedRows);
}
}
// Return added rows' uniqueIndex
return { addedRows: addedRows, parentIndex: parentIndex, rowIndex: rowIndex };
}
function removeRow(tbWhole, rowIndex, uniqueIndex, force) {
var settings = $(tbWhole).data('appendGrid');
var tbBody = tbWhole.getElementsByTagName('tbody')[0];
if ($.isNumeric(uniqueIndex)) {
for (var z = 0; z < settings._rowOrder.length; z++) {
if (settings._rowOrder[z] == uniqueIndex) {
rowIndex = z;
break;
}
}
}
if ($.isNumeric(rowIndex)) {
// Remove middle row
if (force || typeof (settings.beforeRowRemove) != 'function' || settings.beforeRowRemove(tbWhole, rowIndex)) {
settings._rowOrder.splice(rowIndex, 1);
tbBody.deleteRow(rowIndex);
// Save setting
saveSetting(tbWhole, settings);
// Sort sequence
sortSequence(tbWhole, rowIndex);
// Trigger event
if (typeof (settings.afterRowRemoved) == 'function') {
settings.afterRowRemoved(tbWhole, rowIndex);
}
}
}
else {
// Remove last row
if (force || typeof (settings.beforeRowRemove) != 'function' || settings.beforeRowRemove(tbWhole, settings._rowOrder.length - 1)) {
uniqueIndex = settings._rowOrder.pop();
tbBody.deleteRow(-1);
// Save setting
saveSetting(tbWhole, settings);
// Trigger event
if (typeof (settings.afterRowRemoved) == 'function') {
settings.afterRowRemoved(tbWhole, null);
}
}
}
}
function sortSequence(tbWhole, startIndex) {
var settings = $(tbWhole).data('appendGrid');
for (var z = startIndex; z < settings._rowOrder.length; z++) {
$('#' + settings.idPrefix + '_Row_' + settings._rowOrder[z] + ' td.first', tbWhole).text(z + 1);
}
}
function loadData(tbWhole, records, isInit) {
var tbBody, tbRow, tbCell, uniqueIndex, insertResult;
var settings = $(tbWhole).data('appendGrid');
if (settings) {
// Clear existing content
tbBody = tbWhole.getElementsByTagName('tbody')[0];
$(tbBody).empty();
settings._rowOrder.length = 0;
settings._uniqueIndex = 0;
// Check any records
if (records != null && records.length) {
// Add rows
insertResult = insertRow(tbWhole, records.length, null, null);
// Set data
for (var r = 0; r < insertResult.addedRows.length; r++) {
for (var c = 0; c < settings.columns.length; c++) {
setCtrlValue(settings, c, settings._rowOrder[r], records[r][settings.columns[c].name]);
}
}
}
// Save setting
settings._isDataLoaded = true;
if (isInit) settings.initData = null;
$(tbWhole).data('appendGrid', settings);
// Trigger data loaded event
if (typeof (settings.dataLoaded) == 'function') {
settings.dataLoaded(tbWhole);
}
}
}
function findRowIndex(uniqueIndex, settings) {
for (var z = 0; z < settings._rowOrder.length; z++) {
if (settings._rowOrder[z] == uniqueIndex) {
return z;
}
}
return null;
}
function isEmpty(value) {
return typeof (value) == 'undefined' || value == null;
}
function getObjValue(obj, key) {
if (!isEmpty(obj) && $.isPlainObject(obj) && !isEmpty(obj[key])) {
return obj[key];
}
return null;
}
function saveSetting(tbWhole, settings) {
$(tbWhole).data('appendGrid', settings);
$('#' + settings.idPrefix + '_rowOrder', tbWhole).val(settings._rowOrder.join());
}
function getRowIndex(settings, uniqueIndex) {
var rowIndex = null;
for (var z = 0; z < settings._rowOrder.length; z++) {
if (settings._rowOrder[z] == uniqueIndex) {
return z;
}
}
return rowIndex;
}
function getRowValue(settings, uniqueIndex, loopIndex) {
var result = {}, keyName = null;
for (var z = 0; z < settings.columns.length; z++) {
keyName = settings.columns[z].name + (isEmpty(loopIndex) ? '' : '_' + loopIndex);
result[keyName] = getCtrlValue(settings, z, uniqueIndex);
}
return result;
}
function getCtrlValue(settings, colIndex, uniqueIndex) { // type, idPrefix, columnName, uniqueIndex
var ctrl = null, type = settings.columns[colIndex].type, columnName = settings.columns[colIndex].name;
if (type == 'checkbox') {
ctrl = getCellCtrl(type, settings.idPrefix, columnName, uniqueIndex);
if (ctrl == null)
return null;
else
return ctrl.checked ? 1 : 0;
}
else if (type == 'custom') {
if (typeof (settings.columns[colIndex].customGetter) == 'function')
return settings.columns[colIndex].customGetter(settings.idPrefix, columnName, uniqueIndex);
else
return null;
}
else {
ctrl = getCellCtrl(type, settings.idPrefix, columnName, uniqueIndex);
if (ctrl == null)
return null;
else
return ctrl.value;
}
}
function getCellCtrl(type, idPrefix, columnName, uniqueIndex) {
return document.getElementById(idPrefix + '_' + columnName + '_' + uniqueIndex);
}
function setCtrlValue(settings, colIndex, uniqueIndex, data) { // type, idPrefix, columnName, uniqueIndex, data
var type = settings.columns[colIndex].type;
var columnName = settings.columns[colIndex].name;
if (type == 'checkbox') {
getCellCtrl(type, settings.idPrefix, columnName, uniqueIndex).checked = (data != null && data != 0);
}
else if (type == 'custom') {
if (typeof (settings.columns[colIndex].customSetter) == 'function') {
settings.columns[colIndex].customSetter(settings.idPrefix, columnName, uniqueIndex, data);
}
}
else {
getCellCtrl(type, settings.idPrefix, columnName, uniqueIndex).value = (data == null ? '' : data);
}
}
function gridRowDragged(tbWhole, isMoveUp, uniqueIndex, tbRowIndex) {
// Get setting
var settings = $(tbWhole).data('appendGrid');
// Find the start sorting index
var startIndex = -1;
for (var z = 0; z < settings._rowOrder.length; z++) {
if (settings._rowOrder[z] == uniqueIndex) {
if (isMoveUp) {
startIndex = tbRowIndex;
settings._rowOrder.splice(z, 1);
settings._rowOrder.splice(tbRowIndex, 0, uniqueIndex);
} else {
startIndex = z;
settings._rowOrder.splice(tbRowIndex + 1, 0, uniqueIndex);
settings._rowOrder.splice(z, 1);
}
break;
}
}
// Do re-order
sortSequence(tbWhole, startIndex);
// Save setting
saveSetting(tbWhole, settings);
}
/// <summary>
/// Initialize append grid or calling its methods.
/// </summary>
$.fn.appendGrid = function (params) {
if (_methods[params]) {
return _methods[params].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof (params) === 'object' || !params) {
return _methods.init.apply(this, arguments);
} else {
$.error('`' + params + '` method does not supported by appendGrid...');
}
};
})(jQuery);