-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathjquery.bs_grid.js
1369 lines (1152 loc) · 61.1 KB
/
jquery.bs_grid.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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileOverview bs_grid is a jQuery datagrid plugin based on Twitter Bootstrap.
* <p>License MIT
* <br />Copyright Christos Pontikis <a href="http://www.pontikis.net">http://www.pontikis.net</a>
* <br />Project page <a href="http://www.pontikis.net/labs/bs_grid/">http://www.pontikis.net/labs/bs_grid/</a>
* @version 0.9.2 (28 May 2014)
* @author Christos Pontikis http://www.pontikis.net
* @requires jquery >= 1.8, twitter bootstrap >= 2, bs_pagination plugin, jQuery UI sortable (optional), jui_filter_rules plugin >= 1.0.4 (optional)
*/
/**
* See <a href="http://jquery.com">http://jquery.com</a>.
* @name $
* @class
* See the jQuery Library (<a href="http://jquery.com">http://jquery.com</a>) for full details. This just
* documents the function and classes that are added to jQuery by this plug-in.
*/
/**
* See <a href="http://jquery.com">http://jquery.com</a>
* @name fn
* @class
* See the jQuery Library (<a href="http://jquery.com">http://jquery.com</a>) for full details. This just
* documents the function and classes that are added to jQuery by this plug-in.
* @memberOf $
*/
/**
* Pseudo-Namespace containing bs_grid private methods (for documentation purposes)
* @name _private_methods
* @namespace
*/
"use strict";
(function($) {
var pluginName = "bs_grid",
pluginGivenOptions = "bs_grid_given_options",
pluginStatus = "bs_grid_status";
// public methods
var methods = {
/**
* @lends $.fn.bs_grid
*/
init: function(options) {
var elem = this;
return this.each(function() {
/**
* store given options on first launch (in new object - no reference)
*/
if(typeof elem.data(pluginGivenOptions) === "undefined") {
elem.data(pluginGivenOptions, $.extend(true, {}, options));
}
/**
* settings and defaults
* settings modification will affect elem.data(pluginName) and vice versa
*/
var settings = elem.data(pluginName);
if(typeof settings === "undefined") {
var bootstrap_version = "3";
if(options.hasOwnProperty("bootstrap_version") && options["bootstrap_version"] == "2") {
bootstrap_version = "2";
}
var defaults = methods.getDefaults.call(elem, bootstrap_version);
// deep merge ('true' arg) is required, as there are object attibutes (paginationOptions, filterOptions)
settings = $.extend(true, {}, defaults, options);
} else {
settings = $.extend(true, {}, settings, options);
}
elem.data(pluginName, settings);
// initialize plugin status
if(typeof elem.data(pluginStatus) === "undefined") {
elem.data(pluginStatus, {});
}
if(!settings.row_primary_key) {
settings.selected_ids = [];
} else {
switch(settings.rowSelectionMode) {
case "single":
if(settings.selected_ids.length > 1) {
settings.selected_ids = [];
}
break;
case false:
settings.selected_ids = [];
break;
}
}
var container_id = elem.attr("id");
// apply container style
elem.removeClass().addClass(settings.containerClass);
// bind events
elem.unbind("onCellClick").bind("onCellClick", settings.onCellClick);
elem.unbind("onRowClick").bind("onRowClick", settings.onRowClick);
elem.unbind("onDatagridError").bind("onDatagridError", settings.onDatagridError);
elem.unbind("onDebug").bind("onDebug", settings.onDebug);
elem.unbind("onDisplay").bind("onDisplay", settings.onDisplay);
// initialize plugin html
var tools_id = create_id(settings.tools_id_prefix, container_id),
columns_list_id = create_id(settings.columns_list_id_prefix, container_id),
default_columns_list = "",
sorting_list_id = create_id(settings.sorting_list_id_prefix, container_id),
default_sorting_list = "",
sorting_radio_name = create_id(settings.sorting_radio_name_prefix, container_id) + "_",
startPos, newPos,
selected_rows_id = create_id(settings.selected_rows_id_prefix, container_id),
selection_list_id = create_id(settings.selection_list_id_prefix, container_id),
table_container_id = create_id(settings.table_container_id_prefix, container_id),
table_id = create_id(settings.table_id_prefix, container_id),
no_results_id = create_id(settings.no_results_id_prefix, container_id),
filter_toggle_id = create_id(settings.filter_toggle_id_prefix, container_id),
custom_html1_id = create_id(settings.custom_html1_id_prefix, container_id),
custom_html2_id = create_id(settings.custom_html2_id_prefix, container_id),
pagination_id = create_id(settings.pagination_id_prefix, container_id),
filter_container_id = create_id(settings.filter_container_id_prefix, container_id),
filter_rules_id = create_id(settings.filter_rules_id_prefix, container_id),
filter_tools_id = create_id(settings.filter_tools_id_prefix, container_id),
elem_html = "", tools_html = "";
// create basic html structure ---------------------------------
elem_html += '<div id="' + tools_id + '" class="' + settings.toolsClass + '"></div>';
elem_html += '<div id="' + table_container_id + '" class="' + settings.dataTableContainerClass + '">';
elem_html += '<table id="' + table_id + '" class="' + settings.dataTableClass + '"></table>';
elem_html += '</div>';
elem_html += '<div id="' + no_results_id + '" class="' + settings.noResultsClass + '">' + rsc_bs_dg.no_records_found + '</div>';
if(settings.customHTMLelementID1) {
elem_html += '<div id="' + custom_html1_id + '"></div>';
}
elem_html += '<div id="' + pagination_id + '"></div>';
if(settings.customHTMLelementID2) {
elem_html += '<div id="' + custom_html2_id + '"></div>';
}
if(settings.useFilters) {
elem_html += '<div id="' + filter_container_id + '" class="' + settings.filterContainerClass + '">';
elem_html += '<div id="' + filter_rules_id + '"></div>';
elem_html += '<div id="' + filter_tools_id + '" class="' + settings.filterToolsClass + '">';
elem_html += '<button class="' + settings.filterApplyBtnClass + '">' + rsc_bs_dg.filters_apply + '</button>';
elem_html += '<button class="' + settings.filterResetBtnClass + '">' + rsc_bs_dg.filters_reset + '</button>';
elem_html += '</div>';
}
elem_html += '</div>';
elem.html(elem_html);
$("#" + no_results_id).hide();
var elem_tools = $("#" + tools_id),
elem_table = $("#" + table_id),
elem_pagination = $("#" + pagination_id);
// create toolbar ----------------------------------------------
// columns list
tools_html += '<div class="btn-group pull-right">';
tools_html += '<button type="button" class="' + settings.columnsListLaunchButtonClass + '" data-toggle="dropdown" title="' + rsc_bs_dg.columns + '">';
tools_html += '<span class="' + settings.columnsListLaunchButtonIconClass + '"></span>';
tools_html += '<span class="caret"></span>';
tools_html += '</button>';
tools_html += '<ul id="' + columns_list_id + '" class="' + settings.columnsListClass + '">';
var col_name, col_checked,
col_list_len = 0;
for(var i in settings.columns) {
if(column_is_function(settings.columns[i])) {
continue;
}
col_list_len++;
col_name = get_column_header(settings.columns[i]);
col_checked = column_is_visible(settings.columns[i]) ? " checked" : "";
default_columns_list += '<li><a href="javascript:void(0);">' +
'<label class="' + settings.columnsListLabelClass + '">' +
'<input type="checkbox" class="' + settings.columnsListCheckClass + '"' + col_checked + '> ' + col_name +
'</label>' +
'</a></li>';
}
default_columns_list += '<li class="not-sortable ' + settings.columnsListDividerClass + '"></li>';
var row_index_checked = settings.showRowNumbers ? " checked" : "";
default_columns_list += '<li class="not-sortable columns-li-padding"><label class="' + settings.columnsListLabelClass + '"><input type="checkbox" class="' + settings.columnsListCheckClass + '"' + row_index_checked + '> ' + rsc_bs_dg.columns_show_row_numbers + '</label></li>';
default_columns_list += '<li class="not-sortable ' + settings.columnsListDividerClass + '"></li>';
default_columns_list += '<li class="not-sortable columns-li-padding"><button class="' + settings.columnsListDefaultButtonClass + '">' + rsc_bs_dg.columns_default + '</button></li>';
//save default columns list
if(typeof elem.data(pluginStatus)["default_columns_list"] === "undefined") {
elem.data(pluginStatus)["default_columns_list"] = default_columns_list;
}
tools_html += default_columns_list;
tools_html += '</ul>';
tools_html += '</div>';
// sorting list ------------------------------------------------
tools_html += '<div class="btn-group pull-right">';
tools_html += '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" title="' + rsc_bs_dg.sorting + '">';
tools_html += '<span class="' + settings.sortingListLaunchButtonIconClass + '"></span>';
tools_html += '<span class="caret"></span>';
tools_html += '</button>';
tools_html += '<ul id="' + sorting_list_id + '" class="dropdown-menu dropdown-menu-right">';
for(var i in settings.sorting) {
var sort_name = get_sorting_name(settings.sorting[i]),
checked_asc = settings.sorting[i]["order"] == "ascending" ? " checked" : "",
checked_desc = settings.sorting[i]["order"] == "descending" ? " checked" : "",
checked_none = settings.sorting[i]["order"] == "none" ? " checked" : "";
default_sorting_list += '<li><a href="javascript:void(0);">' +
'<label class="' + settings.sortingLabelCheckboxClass + '"><input type="radio" name="' + sorting_radio_name + i + '"' + checked_asc + '>' + rsc_bs_dg.sort_ascending + '</label>' +
'<label class="' + settings.sortingLabelCheckboxClass + '"><input type="radio" name="' + sorting_radio_name + i + '"' + checked_desc + '>' + rsc_bs_dg.sort_descending + '</label>' +
'<label class="' + settings.sortingLabelCheckboxClass + '"><input type="radio" name="' + sorting_radio_name + i + '"' + checked_none + '>' + rsc_bs_dg.sort_none + '</label>' +
'<span class="' + settings.sortingNameClass + '">' + sort_name + '</span>' +
'</a></li>';
}
default_sorting_list += '<li class="not-sortable ' + settings.columnsListDividerClass + '"></li>';
default_sorting_list += '<li class="not-sortable columns-li-padding"><button class="' + settings.columnsListDefaultButtonClass + '">' + rsc_bs_dg.sorting_default + '</button></li>';
//save default columns list
if(typeof elem.data(pluginStatus)["default_sorting_list"] === "undefined") {
elem.data(pluginStatus)["default_sorting_list"] = default_sorting_list;
}
tools_html += default_sorting_list;
tools_html += '</ul>';
tools_html += '</div>';
// selection list ----------------------------------------------
if(settings.row_primary_key &&
(settings.rowSelectionMode == "single" || settings.rowSelectionMode == "multiple")) {
tools_html += '<div class="btn-group pull-right">';
tools_html += '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" title="' + rsc_bs_dg.select + '">';
tools_html += '<span class="' + settings.selectButtonIconClass + '"></span>';
tools_html += '<span id="' + selected_rows_id + '" class="' + settings.selectedRowsClass + '">' + settings.selected_ids.length + '</span>';
tools_html += '<span class="caret"></span>';
tools_html += '</button>';
tools_html += '<ul id="' + selection_list_id + '" class="dropdown-menu dropdown-menu-right">';
if(settings.rowSelectionMode == "multiple") {
tools_html += '<li><a href="javascript:void(0);">' + rsc_bs_dg.select_all_in_page + '</a></li>';
tools_html += '<li><a href="javascript:void(0);">' + rsc_bs_dg.deselect_all_in_page + '</a></li>';
tools_html += '<li><a href="javascript:void(0);">' + rsc_bs_dg.select_inverse_in_page + '</a></li>';
tools_html += '<li class="not-sortable ' + settings.columnsListDividerClass + '"></li>';
}
tools_html += '<li><a href="javascript:void(0);">' + rsc_bs_dg.deselect_all + '</a></li>';
tools_html += '</ul>';
tools_html += '</div>';
}
// filter toggle button ----------------------------------------
if(settings.useFilters) {
tools_html += '<button id="' + filter_toggle_id + '" class="btn btn-default pull-right" title="' + rsc_bs_dg.filters + '"><span class="' + settings.filterToggleButtonIconClass + '"></span></button>';
}
elem_tools.html(tools_html);
// initialize grid ---------------------------------------------
var grid_init = methods.displayGrid.call(elem, false);
/**
* EVENTS ******************************************************
*/
//TOOLS - columns list -----------------------------------------
var elem_columns_list = $("#" + columns_list_id);
// Prevent closing on click
elem_columns_list.click(function(e) {
e.stopPropagation();
});
// show - hide column
elem_columns_list.off("click", "input[type=checkbox]").on("click", "input[type=checkbox]", function() {
var col_index = $($("#" + columns_list_id + " li input[type=checkbox]")).index(this);
if(col_index < col_list_len) {
var checked_columns = $("#" + columns_list_id + " li").not(".not-sortable").find("input[type=checkbox]:checked").length;
if(checked_columns == 1) {
$("#" + columns_list_id + " li input[type=checkbox]:checked:first").slice(0, col_list_len).attr("disabled", true);
} else {
$("#" + columns_list_id + " li input[type=checkbox]").slice(0, col_list_len).removeAttr("disabled");
}
set_column_visible(settings.columns[col_index], $(this).is(":checked"));
} else {
// show - hide row index
settings.showRowNumbers = $(this).is(":checked");
}
methods.displayGrid.call(elem, false);
});
// change column index
if(settings.useSortableLists) {
if(col_list_len > 2) {
elem_columns_list.sortable({
items: "li:not(.not-sortable)",
start: function(event, ui) {
startPos = ui.item.index();
},
stop: function(event, ui) {
newPos = ui.item.index();
if(newPos !== startPos) {
array_move(settings.columns, startPos, newPos);
methods.displayGrid.call(elem, false);
}
}
});
}
}
// display default columns
elem_columns_list.off("click", "li:last button").on("click", "li:last button", function() {
for(var i in elem.data(pluginGivenOptions)["columns"]) {
settings.columns[i] = $.extend(true, {}, elem.data(pluginGivenOptions)["columns"][i]);
}
var checked = elem.data(pluginGivenOptions)["showRowNumbers"];
if(typeof checked === "undefined") {
checked = methods.getDefaults.call(elem, settings.bootstrap_version)["showRowNumbers"];
}
settings.showRowNumbers = checked;
elem_columns_list.html(elem.data(pluginStatus)["default_columns_list"]);
methods.displayGrid.call(elem, false);
});
// TOOLS - sorting list ----------------------------------------
var elem_sorting_list = $("#" + sorting_list_id),
sort_len = settings.sorting.length;
// Prevent closing on click
elem_sorting_list.click(function(e) {
e.stopPropagation();
});
// change sorting type
elem_sorting_list.off("click", "input[type=radio]").on("click", "input[type=radio]", function() {
var radio_index = $($("#" + sorting_list_id + " li input[type=radio]")).index(this),
sorting_index = Math.floor(radio_index / 3),
sorting_type = "";
switch(radio_index % 3) {
case 0:
sorting_type = "ascending";
break;
case 1:
sorting_type = "descending";
break;
case 2:
sorting_type = "none";
break;
}
if(settings.sorting[sorting_index]["order"] != sorting_type) {
settings.sorting[sorting_index]["order"] = sorting_type;
methods.displayGrid.call(elem, false);
}
});
// change sorting index
if(settings.useSortableLists) {
if(sort_len > 2) {
elem_sorting_list.sortable({
items: "li:not(.not-sortable)",
start: function(event, ui) {
startPos = ui.item.index();
},
stop: function(event, ui) {
newPos = ui.item.index();
if(newPos !== startPos) {
array_move(settings.sorting, startPos, newPos);
methods.displayGrid.call(elem, false);
}
}
});
}
}
// display default sorting
elem_sorting_list.off("click", "li:last button").on("click", "li:last button", function() {
for(var i in elem.data(pluginGivenOptions)["sorting"]) {
settings.sorting[i] = $.extend(true, {}, elem.data(pluginGivenOptions)["sorting"][i]);
}
elem_sorting_list.html(elem.data(pluginStatus)["default_sorting_list"]);
methods.displayGrid.call(elem, false);
});
// row selection -----------------------------------------------
if(settings.row_primary_key &&
(settings.rowSelectionMode == "single" || settings.rowSelectionMode == "multiple")) {
var row_prefix_len = (table_id + "_tr_").length;
// click on row
elem_table.off("click", "tbody tr").on("click", "tbody tr", function() {
var row_id = parseInt($(this).attr("id").substr(row_prefix_len)),
row_status,
idx = methods.selectedRows.call(elem, "selected_index", row_id);
if(idx > -1) {
methods.selectedRows.call(elem, "remove_id", idx);
methods.selectedRows.call(elem, "mark_deselected", row_id);
row_status = "deselected";
} else {
if(settings.rowSelectionMode == "single") {
methods.selectedRows.call(elem, "clear_all_ids");
methods.selectedRows.call(elem, "mark_page_deselected");
}
methods.selectedRows.call(elem, "add_id", row_id);
methods.selectedRows.call(elem, "mark_selected", row_id);
row_status = "selected";
}
// update selected rows counter
methods.selectedRows.call(elem, "update_counter");
elem.triggerHandler("onRowClick", {row_id: row_id, row_status: row_status});
});
// selection list
var elem_selection_list = $("#" + selection_list_id);
elem_selection_list.off("click", "li").on("click", "li", function() {
var sel_index = $(this).index();
if(settings.rowSelectionMode == "single") {
methods.selectedRows.call(elem, "clear_all_ids");
methods.selectedRows.call(elem, "mark_page_deselected");
} else if(settings.rowSelectionMode == "multiple") {
var selector_table_tr = "#" + table_id + " tbody tr",
row_prefix_len = (table_id + "_tr_").length,
row_id, idx;
switch(sel_index) {
case 0:
$(selector_table_tr).each(function() {
row_id = parseInt($(this).attr("id").substr(row_prefix_len));
idx = methods.selectedRows.call(elem, "selected_index", row_id);
if(idx == -1) {
methods.selectedRows.call(elem, "add_id", row_id);
}
});
methods.selectedRows.call(elem, "mark_page_selected");
break;
case 1:
$(selector_table_tr).each(function() {
row_id = parseInt($(this).attr("id").substr(row_prefix_len));
idx = methods.selectedRows.call(elem, "selected_index", row_id);
if(idx > -1) {
methods.selectedRows.call(elem, "remove_id", idx);
}
});
methods.selectedRows.call(elem, "mark_page_deselected");
break;
case 2:
$(selector_table_tr).each(function() {
row_id = parseInt($(this).attr("id").substr(row_prefix_len));
idx = methods.selectedRows.call(elem, "selected_index", row_id);
if(idx > -1) {
methods.selectedRows.call(elem, "remove_id", idx);
} else {
methods.selectedRows.call(elem, "add_id", row_id);
}
});
methods.selectedRows.call(elem, "mark_page_inversed");
break;
case 4:
methods.selectedRows.call(elem, "clear_all_ids");
methods.selectedRows.call(elem, "mark_page_deselected");
break;
}
}
// update selected rows counter
methods.selectedRows.call(elem, "update_counter");
});
}
// click on cell -----------------------------------------------
elem_table.off("click", "tbody tr td").on("click", "tbody tr td", function() {
var col_index = $(this).index();
var row_index = $(this).parent("tr").index();
elem.triggerHandler("onCellClick", {col: col_index, row: row_index});
});
// simple columns sorting --------------------------------------
elem_table.off("click", "thead th").on("click", "thead th", function() {
var th_index = $(this).index(),
th_text = $(this).text();
if(settings.showRowNumbers) {
if(th_index == 0) {
return false;
} else {
th_index--;
}
}
// get column field
var visible_index = -1, th_field = "", th_sortable = false;
for(var i in settings.columns) {
if(column_is_visible(settings.columns[i])) {
visible_index++;
if(visible_index == th_index) {
th_field = settings.columns[i].field;
th_sortable = column_is_sortable(settings.columns[i]);
break;
}
}
}
if(!th_sortable) {
return false;
}
if(th_field) {
// get sorting order for this field (if any)
var current_order = "", new_order;
for(var i in settings.sorting) {
if(settings.sorting[i].field == th_field) {
current_order = settings.sorting[i].order;
break;
}
}
switch(current_order) {
case "ascending":
new_order = "descending";
break;
case "descending":
new_order = "none";
break;
case "none":
new_order = "ascending";
break;
default:
new_order = "ascending";
}
settings.sorting = [
{
sortingName: th_text,
field: th_field,
order: new_order
}
];
// update sorting list
var checked_asc = new_order == "ascending" ? " checked" : "",
checked_desc = new_order == "descending" ? " checked" : "",
checked_none = new_order == "none" ? " checked" : "";
var sorting_list_html = '<li><a href="javascript:void(0);">' +
'<label class="' + settings.sortingLabelCheckboxClass + '"><input type="radio" name="' + sorting_radio_name + '0' + '"' + checked_asc + '>' + rsc_bs_dg.sort_ascending + '</label>' +
'<label class="' + settings.sortingLabelCheckboxClass + '"><input type="radio" name="' + sorting_radio_name + '0' + '"' + checked_desc + '>' + rsc_bs_dg.sort_descending + '</label>' +
'<label class="' + settings.sortingLabelCheckboxClass + '"><input type="radio" name="' + sorting_radio_name + '0' + '"' + checked_none + '>' + rsc_bs_dg.sort_none + '</label>' +
'<span class="' + settings.sortingNameClass + '">' + th_text + '</span>' +
'</a></li>';
sorting_list_html += '<li class="not-sortable ' + settings.columnsListDividerClass + '"></li>';
sorting_list_html += '<li class="not-sortable columns-li-padding"><button class="' + settings.columnsListDefaultButtonClass + '">' + rsc_bs_dg.sorting_default + '</button></li>';
elem_sorting_list.html(sorting_list_html);
// display grid
methods.displayGrid.call(elem, false);
}
});
// PAGINATION --------------------------------------------------
$.when(grid_init).then(function(data, textStatus, jqXHR) {
var total_rows = data["total_rows"];
var pagination_options = settings.paginationOptions,
bs_grid_pagination_options = {
// defined by bs_grid
currentPage: settings.pageNum,
rowsPerPage: settings.rowsPerPage,
maxRowsPerPage: settings.maxRowsPerPage,
totalPages: Math.ceil(total_rows / settings.rowsPerPage),
totalRows: total_rows,
bootstrap_version: settings.bootstrap_version,
onChangePage: function(event, params) {
settings.pageNum = params.currentPage;
settings.rowsPerPage = params.rowsPerPage;
methods.displayGrid.call(elem, false);
}
};
$.extend(pagination_options, bs_grid_pagination_options);
elem_pagination.bs_pagination(pagination_options);
// custom html ---------------------------------------------
// (not an event, but page renders better displaying custom html after grid rendering)
if(settings.customHTMLelementID1) {
$("#" + custom_html1_id).html($("#" + settings.customHTMLelementID1).html());
}
if(settings.customHTMLelementID2) {
$("#" + custom_html2_id).html($("#" + settings.customHTMLelementID2).html());
}
});
// FILTERS -----------------------------------------------------
if(settings.useFilters) {
var elem_filter_toggle = $("#" + filter_toggle_id),
elem_filter_container = $("#" + filter_container_id),
elem_filter_rules = $("#" + filter_rules_id),
elem_filter_tools = $("#" + filter_tools_id);
// initialize jui_filter_rules plugin ----------------------
var filter_options = settings.filterOptions,
bs_grid_internal_filter_options = {
bootstrap_version: settings.bootstrap_version,
onValidationError: function(event, data) {
elem.triggerHandler("onDatagridError", data);
},
onSetRules: function() {
}
};
filter_options = $.extend({}, filter_options, bs_grid_internal_filter_options);
elem_filter_rules.jui_filter_rules(filter_options);
if(filter_options.filter_rules.length > 0) {
elem_filter_container.show();
} else {
elem_filter_container.hide();
}
/* filter toogle */
elem_tools.off("click", "#" + filter_toggle_id).on("click", "#" + filter_toggle_id, function() {
if(elem_filter_container.is(":visible")) {
elem_filter_container.slideUp();
if(settings.filterOptions.filter_rules.length > 0) {
// mark filter toggle as active
elem_filter_toggle.addClass(settings.filterToggleActiveClass);
}
} else {
elem_filter_container.slideDown();
// mark filter toggle as inactive
elem_filter_toggle.removeClass(settings.filterToggleActiveClass);
}
});
/* filter tools */
elem_filter_tools.off("click", "button").on("click", "button", function() {
var btn_index = $(this).index(),
a_rules = elem_filter_rules.jui_filter_rules("getRules", 0, []);
if(a_rules == false) {
return false;
}
switch(btn_index) {
case 0:
//elem_filter_rules.jui_filter_rules("markAllRulesAsApplied");
settings.filterOptions.filter_rules = a_rules;
// Reset selected rows
settings.selected_ids = [];
break;
case 1:
elem_filter_rules.jui_filter_rules("clearAllRules");
settings.filterOptions.filter_rules = [];
break;
}
settings.pageNum = 1;
methods.displayGrid.call(elem, true);
});
}
});
},
/**
* Get plugin version
* @returns {string}
*/
getVersion: function() {
return "0.9.2";
},
/**
* Get default values
* @example $(element).bs_grid("getDefaults", "3");
* @return {Object}
*/
getDefaults: function(bootstrap_version) {
var default_settings = {
pageNum: 1,
rowsPerPage: 10,
maxRowsPerPage: 100,
row_primary_key: "",
rowSelectionMode: "single", // "multiple", "single", false
selected_ids: [],
/**
* MANDATORY PROPERTIES: field
* UNIQUE PROPERTIES: field
* {field: "customer_id", header: "Code", visible: "no", is_function: "no", "headerClass": "th_code hidden-xs", "dataClass": "td_code hidden-xs"},
*/
columns: [],
/**
* MANDATORY PROPERTIES: field, order
* UNIQUE PROPERTIES: field
* order is one of "ascending", "descending", "none"
* {sortingName: "Code", field: "customer_id", order: "none"},
*/
sorting: [],
/**
* See bs_pagination plugin documentation
*/
paginationOptions: {
containerClass: "well pagination-container",
visiblePageLinks: 5,
showGoToPage: true,
showRowsPerPage: true,
showRowsInfo: true,
showRowsDefaultInfo: true,
disableTextSelectionInNavPane: true
}, // "currentPage", "rowsPerPage", "maxRowsPerPage", "totalPages", "totalRows", "bootstrap_version", "onChangePage" will be ignored
/**
* See jui_filter_rules plugin documentation
*/
filterOptions: {
filters: [],
filter_rules: []
}, // "bootstrap_version", "onSetRules", "onValidationError" will be ignored
useFilters: true,
showRowNumbers: false,
showSortingIndicator: true,
useSortableLists: true,
customHTMLelementID1: "",
customHTMLelementID2: "",
/* STYLES ----------------------------------------------------*/
bootstrap_version: "3",
// bs 3
containerClass: "grid_container",
noResultsClass: "alert alert-warning no-records-found",
toolsClass: "tools",
columnsListLaunchButtonClass: "btn btn-default dropdown-toggle",
columnsListLaunchButtonIconClass: "glyphicon glyphicon-th",
columnsListClass: "dropdown-menu dropdown-menu-right",
columnsListLabelClass: "columns-label",
columnsListCheckClass: "col-checkbox",
columnsListDividerClass: "divider",
columnsListDefaultButtonClass: "btn btn-primary btn-xs btn-block",
sortingListLaunchButtonIconClass: "glyphicon glyphicon-sort",
sortingLabelCheckboxClass: "radio-inline",
sortingNameClass: "sorting-name",
selectButtonIconClass: "glyphicon glyphicon-check",
selectedRowsClass: "selected-rows",
filterToggleButtonIconClass: "glyphicon glyphicon-filter",
filterToggleActiveClass: "btn-info",
sortingIndicatorAscClass: "glyphicon glyphicon-chevron-up text-muted",
sortingIndicatorDescClass: "glyphicon glyphicon-chevron-down text-muted",
dataTableContainerClass: "table-responsive",
dataTableClass: "table table-bordered table-hover",
commonThClass: "th-common",
selectedTrClass: "warning",
filterContainerClass: "well filters-container",
filterToolsClass: "",
filterApplyBtnClass: "btn btn-primary btn-sm filters-button",
filterResetBtnClass: "btn btn-default btn-sm filters-button",
// prefixes
tools_id_prefix: "tools_",
columns_list_id_prefix: "columns_list_",
sorting_list_id_prefix: "sorting_list_",
sorting_radio_name_prefix: "sort_radio_",
selected_rows_id_prefix: "selected_rows_",
selection_list_id_prefix: "selection_list_",
filter_toggle_id_prefix: "filter_toggle_",
table_container_id_prefix: "tbl_container_",
table_id_prefix: "tbl_",
no_results_id_prefix: "no_res_",
custom_html1_id_prefix: "custom1_",
custom_html2_id_prefix: "custom2_",
pagination_id_prefix: "pag_",
filter_container_id_prefix: "flt_container_",
filter_rules_id_prefix: "flt_rules_",
filter_tools_id_prefix: "flt_tools_",
// misc
debug_mode: "no",
// events
onCellClick: function() {
},
onRowClick: function() {
},
onDatagridError: function() {
},
onDebug: function() {
},
onDisplay: function() {
}
};
if(bootstrap_version == "2") {
default_settings.bootstrap_version = "2";
// bs 2
default_settings.columnsListLaunchButtonIconClass = "icon-th";
default_settings.sortingListLaunchButtonIconClass = "icon-resize-vertical";
default_settings.sortingLabelCheckboxClass = "radio inline";
default_settings.selectButtonIconClass = "icon-check";
default_settings.filterToggleButtonIconClass = "icon-filter";
default_settings.filterToggleActiveClass = "btn-info";
default_settings.sortingIndicatorAscClass = "icon-chevron-up muted";
default_settings.sortingIndicatorDescClass = "icon-chevron-down muted";
default_settings.dataTableContainerClass = "table-responsive";
default_settings.dataTableClass = "table table-bordered table-hover";
default_settings.filterApplyBtnClass = "btn btn-primary filters-button ";
default_settings.filterResetBtnClass = "btn btn-default filters-button";
}
return default_settings;
},
/**
* Get any option set to plugin using its name (as string)
*
* @example $(element).bs_grid("getOption", some_option);
* @param {String} opt
* @return {*}
*/
getOption: function(opt) {
var elem = this;
return elem.data(pluginName)[opt];
},
/**
* Get all options
* @example $(element).bs_grid("getAllOptions");
* @return {*}
*/
getAllOptions: function() {
var elem = this;
return elem.data(pluginName);
},
/**
* Destroy plugin
* @example $(element).bs_grid("destroy");
*/
destroy: function() {
var elem = this,
container_id = elem.attr("id"),
pagination_container_id = create_id(methods.getOption.call(elem, "pagination_id_prefix"), container_id),
filter_rules_id = create_id(methods.getOption.call(elem, "filter_rules_id_prefix"), container_id);
$("#" + pagination_container_id).removeData();
$("#" + filter_rules_id).removeData();
elem.removeData();
},
/**
*
* @param {string} action
* @param {int} id the id or its (zero based) index in selected IDs
* @returns {*}
*/
selectedRows: function(action, id) {
var elem = this,
container_id = elem.attr("id"),
s = methods.getAllOptions.call(elem),
table_id = create_id(methods.getOption.call(elem, "table_id_prefix"), container_id),
selectedTrClass = methods.getOption.call(elem, "selectedTrClass"),
selector_table_tr = "#" + table_id + " tbody tr",
table_tr_prefix = "#" + table_id + "_tr_";
switch(action) {
case "get_ids":
return s.selected_ids;
break;
case "clear_all_ids":
s.selected_ids = [];
break;
case "update_counter":
var selected_rows_id = create_id(methods.getOption.call(elem, "selected_rows_id_prefix"), container_id);
$("#" + selected_rows_id).text(s.selected_ids.length);
break;
case "selected_index":
return $.inArray(id, s.selected_ids);
break;
case "add_id":
s.selected_ids.push(id);
break;
case "remove_id":
s.selected_ids.splice(id, 1);
break;
case "mark_selected":
$(table_tr_prefix + id).addClass(selectedTrClass);
break;
case "mark_deselected":
$(table_tr_prefix + id).removeClass(selectedTrClass);
break;
case "mark_page_selected":
$(selector_table_tr).addClass(selectedTrClass);
break;
case "mark_page_deselected":
$(selector_table_tr).removeClass(selectedTrClass);
break;
case "mark_page_inversed":
$(selector_table_tr).toggleClass(selectedTrClass);
break;
}
},
/**
* Set a class to page column
* @example $(element).bs_grid("setPageColClass", 1, "headerClass", "dataClass");
* @param {Number} col_index
* @param {String} headerClass
* @param {String} dataClass
*/
setPageColClass: function(col_index, headerClass, dataClass) {
var elem = this,
container_id = elem.attr("id"),
data_table_selector = "#" + create_id(methods.getOption.call(elem, "table_id_prefix"), container_id);
if(headerClass !== "") {
$(data_table_selector + " th").eq(col_index).addClass(headerClass);
}
if(dataClass !== "") {
$(data_table_selector + " tr").each(function() {
$(this).find("td").eq(col_index).addClass(dataClass);
});
}
},
/**
* Set a class to page column
* @example $(element).bs_grid("removePageColClass", 1, "headerClass", "dataClass");