gridmvc.js
29.7 KB
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
/***
* Grid.Mvc
* Examples and documentation at: http://gridmvc.codeplex.com
* Version: 3.0.0
* Requires: window.jQuery v1.3+
* LGPL license: http://gridmvc.codeplex.com/license
*/
window.pageGrids = window.pageGrids || {};
$.fn.extend({
gridmvc: function () {
var aObj = [];
$(this).each(function () {
if (!$(this).data("gridmvc")) {
var options = { lang: $(this).attr("data-lang"), selectable: $(this).attr("data-selectable") == "true", multiplefilters: $(this).attr("data-multiplefilters") == "true" };
var grid = new GridMvc(this, options);
var name = $(this).attr("data-gridname");
if (name.length > 0)
window.pageGrids[$(this).attr("data-gridname")] = grid;
aObj.push(grid);
$(this).data("gridmvc", grid);
} else {
aObj.push($(this).data("gridmvc"));
}
});
if (aObj.length == 1)
return aObj[0];
return aObj;
}
});
GridMvc = (function ($) {
function gridMvc(container, options) {
this.jqContainer = $(container);
options = options || {};
this.options = $.extend({}, this.defaults(), options);
this.init();
}
gridMvc.prototype.init = function () {
//load current lang options:
this.lang = GridMvc.lang[this.options.lang];
if (typeof (this.lang) == 'undefined')
this.lang = GridMvc.lang.en;
this.events = [];
if (this.options.selectable)
this.initGridRowsEvents();
//Register default filter widgets:
this.filterWidgets = [];
this.addFilterWidget(new TextFilterWidget());
this.addFilterWidget(new NumberFilterWidget());
this.addFilterWidget(new DateTimeFilterWidget());
this.addFilterWidget(new BooleanFilterWidget());
this.openedMenuBtn = null;
this.initFilters();
};
/***
* Handle Grid row events
*/
gridMvc.prototype.initGridRowsEvents = function () {
var $this = this;
this.jqContainer.on("click", ".grid-row", function () {
$this.rowClicked.call(this, $this);
});
};
/***
* Trigger on Grid row click
*/
gridMvc.prototype.rowClicked = function ($context) {
if (!$context.options.selectable)
return;
var row = $(this).closest(".grid-row");
if (row.length <= 0)
return;
var gridRow = {};
row.find(".grid-cell").each(function () {
var columnName = $(this).attr("data-name");
if (columnName.length > 0)
gridRow[columnName] = $(this).text();
});
var evt = $.Event("RowClicked");
$context.notifyOnRowSelect(gridRow, evt);
if (!evt.isDefaultPrevented())
$context.markRowSelected(row);
};
/***
* Mark Grid row as selected
*/
gridMvc.prototype.markRowSelected = function (row) {
this.jqContainer.find(".grid-row.grid-row-selected").removeClass("grid-row-selected");
row.addClass("grid-row-selected");
};
/***
* Default Grid.Mvc options
*/
gridMvc.prototype.defaults = function () {
return {
selectable: true,
multiplefilters: false,
lang: 'en'
};
};
/***
* ============= EVENTS =============
* Methods that provides functionality for grid events
*/
gridMvc.prototype.onRowSelect = function (func) {
this.events.push({ name: "onRowSelect", callback: func });
};
gridMvc.prototype.notifyOnRowSelect = function (row, e) {
e.row = row;
this.notifyEvent("onRowSelect", e);
};
gridMvc.prototype.notifyEvent = function (eventName, e) {
for (var i = 0; i < this.events.length; i++) {
if (this.events[i].name == eventName)
if (!this.events[i].callback(e)) break;
}
};
/***
* ============= FILTERS =============
* Methods that provides functionality for filtering
*/
/***
* Method search all filter buttons and register 'onclick' event handlers:
*/
gridMvc.prototype.initFilters = function () {
var filterHtml = this.filterMenuHtml();
var self = this;
this.jqContainer.find(".grid-filter").each(function () {
$(this).click(function () {
return self.openFilterPopup.call(this, self, filterHtml);
});
});
};
/***
* Shows filter popup window and render filter widget
*/
gridMvc.prototype.openFilterPopup = function (self, html) {
//retrive all column filter parameters from html attrs:
var columnType = $(this).attr("data-type") || "";
//determine widget
var widget = self.getFilterWidgetForType(columnType);
//if widget for specified column type not found - do nothing
if (widget == null)
return false;
//if widget allready rendered - just open popup menu:
if (this.hasAttribute("data-rendered")) {
var or = self.openMenuOnClick.call(this, self);
self.setupPopupInitialPosition($(this));
if (!or && typeof (widget.onShow) != 'undefined')
widget.onShow();
return or;
}
var columnName = $(this).attr("data-name") || "";
var filterData = $(this).attr("data-filterdata") || "";
var widgetData = $(this).attr("data-widgetdata") || "{}";
var filterDataObj = self.parseFilterValues(filterData) || {};
var filterUrl = $(this).attr("data-url") || "";
//mark filter as rendered
$(this).attr("data-rendered", "1");
//append base popup layout:
$(this).append(html);
//determine widget container:
var widgetContainer = $(this).find(".grid-popup-widget");
//onRender target widget
if (typeof (widget.onRender) != 'undefined')
widget.onRender(widgetContainer, self.lang, columnType, filterDataObj, function (values) {
self.closeOpenedPopups();
self.applyFilterValues(filterUrl, columnName, values, false);
}, $.parseJSON(widgetData));
//adding 'clear filter' button if needed:
if ($(this).find(".grid-filter-btn").hasClass("filtered") && widget.showClearFilterButton()) {
var inner = $(this).find(".grid-popup-additional");
inner.append(self.getClearFilterButton(filterUrl));
inner.find(".grid-filter-clear").click(function () {
self.applyFilterValues(filterUrl, columnName, "", true);
});
}
var openResult = self.openMenuOnClick.call(this, self);
if (typeof (widget.onShow) != 'undefined')
widget.onShow();
self.setupPopupInitialPosition($(this));
return openResult;
};
gridMvc.prototype.setupPopupInitialPosition = function (popup) {
var drop = popup.find(".grid-dropdown");
function getInfo() {
var arrow = popup.find(".grid-dropdown-arrow");
return { arrow: arrow, currentDropLeft: parseInt(drop.css("left")), currentArrowLeft: parseInt(arrow.css("left")) };
}
var dropLeft = drop.offset().left;
if (dropLeft < 0) {
var info = getInfo();
info.arrow.css({ left: (info.currentArrowLeft + dropLeft - 10) + "px" });
drop.css({ left: (info.currentDropLeft - dropLeft + 10) + "px" });
return;
}
var dropWidth = drop.width();
var offsetRight = $(window).width() - (dropLeft + dropWidth);
if (offsetRight < 0) {
var info = getInfo();
info.arrow.css({ left: (info.currentArrowLeft - offsetRight + 10) + "px" });
drop.css({ left: (info.currentDropLeft + offsetRight - 10) + "px" });
}
};
/***
* Returns layout of filter popup menu
*/
gridMvc.prototype.filterMenuHtml = function () {
return '<div class="dropdown dropdown-menu grid-dropdown" style="display: none;">\
<div class="grid-dropdown-arrow"></div>\
<div class="grid-dropdown-inner">\
<div class="grid-popup-widget"></div>\
<div class="grid-popup-additional"></div>\
</div>\
</div>';
};
/***
* Returns layout of 'clear filter' button
*/
gridMvc.prototype.getClearFilterButton = function () {
return '<ul class="menu-list">\
<li><a class="grid-filter-clear" href="javascript:void(0);">' + this.lang.clearFilterLabel + '</a></li>\
</ul>';
};
/***
* Register filter widget in widget collection
*/
gridMvc.prototype.addFilterWidget = function (widget) {
this.filterWidgets.push(widget);
};
/***
* Parse filter settings from data attribute
*/
gridMvc.prototype.parseFilterValues = function (filterData) {
var opt = $.parseJSON(filterData);
var filters = [];
for (var i = 0; i < opt.length; i++) {
filters.push({ filterValue: this.urldecode(opt[i].FilterValue), filterType: opt[i].FilterType, columnName: opt[i].ColumnName });
}
return filters;
};
gridMvc.prototype.urldecode = function (str) {
return decodeURIComponent((str + '').replace(/\+/g, '%20'));
};
/***
* Return registred widget for specific column type name
*/
gridMvc.prototype.getFilterWidgetForType = function (typeName) {
for (var i = 0; i < this.filterWidgets.length; i++) {
if ($.inArray(typeName, this.filterWidgets[i].getAssociatedTypes()) >= 0)
return this.filterWidgets[i];
}
return null;
};
/***
* Replace existed filter widget
*/
gridMvc.prototype.replaceFilterWidget = function (typeNameToReplace, widget) {
for (var i = 0; i < this.filterWidgets.length; i++) {
if ($.inArray(typeNameToReplace, this.filterWidgets[i].getAssociatedTypes()) >= 0) {
this.filterWidgets.splice(i, 1);
this.addFilterWidget(widget);
return true;
}
}
return false;
};
/***
* Applies selected filter value by redirecting to another url:
*/
gridMvc.prototype.applyFilterValues = function (initialUrl, columnName, values, skip) {
var filters = this.jqContainer.find(".grid-filter");
if (initialUrl.length > 0)
initialUrl += "&";
var url = "";
if (!skip) {
url += this.getFilterQueryData(columnName, values);
}
if (this.options.multiplefilters) { //multiple filters enabled
for (var i = 0; i < filters.length; i++) {
if ($(filters[i]).attr("data-name") != columnName) {
var filterData = this.parseFilterValues($(filters[i]).attr("data-filterdata"));
if (filterData.length == 0) continue;
if (url.length > 0) url += "&";
url += this.getFilterQueryData($(filters[i]).attr("data-name"), filterData);
} else {
continue;
}
}
}
window.location.search = initialUrl + url;
};
gridMvc.prototype.getFilterQueryData = function (columnName, values) {
var url = "";
for (var i = 0; i < values.length; i++) {
url += "grid-filter=" + encodeURIComponent(columnName) + "__" + values[i].filterType + "__" + encodeURIComponent(values[i].filterValue);
if (i != values.length - 1)
url += "&";
}
return url;
};
/***
* ============= POPUP MENU =============
* Methods that provides base functionality for popup menus
*/
gridMvc.prototype.openMenuOnClick = function (self) {
if ($(this).hasClass("clicked")) return true;
self.closeOpenedPopups();
$(this).addClass("clicked");
var popup = $(this).find(".dropdown-menu");
if (popup.length == 0) return true;
popup.show();
popup.addClass("opened");
self.openedMenuBtn = $(this);
$(document).bind("click.gridmvc", function (e) {
self.documentCallback(e, self);
});
return false;
};
gridMvc.prototype.documentCallback = function (e, $context) {
e = e || event;
var target = e.target || e.srcElement;
var box = $(".dropdown-menu.opened").get(0);
var html = $("html").get(0);
if (typeof box != "undefined") {
do {
if (box == target) {
// Click occured inside the box, do nothing.
return;
}
if (html == target) {
box.style.display = "none";
$(box).removeClass("opened");
}
target = target.parentNode;
} while (target); // Click was outside the box, hide it.
}
if ($context.openedMenuBtn != null)
$context.openedMenuBtn.removeClass("clicked");
$(document).unbind("click.gridmvc");
};
gridMvc.prototype.closeOpenedPopups = function () {
var openedPopup = $(".dropdown-menu.opened");
openedPopup.hide();
openedPopup.removeClass("opened");
if (this.openedMenuBtn != null)
this.openedMenuBtn.removeClass("clicked");
};
/* Grid.Mvc clients functions */
gridMvc.prototype.selectable = function (enable) {
this.options.selectable = enable;
};
return gridMvc;
})(window.jQuery);
/***
* ============= LOCALIZATION =============
* You can localize Grid.Mvc by creating localization files and include it on the page after this script file
* This script file provides localization only for english language.
* For more documentation see: http://gridmvc.codeplex.com/documentation
*/
if (typeof (GridMvc.lang) == 'undefined')
GridMvc.lang = {};
GridMvc.lang.en = {
filterTypeLabel: "Type: ",
filterValueLabel: "Value:",
applyFilterButtonText: "Apply",
filterSelectTypes: {
Equals: "Equals",
StartsWith: "StartsWith",
Contains: "Contains",
EndsWith: "EndsWith",
GreaterThan: "Greater than",
LessThan: "Less than"
},
code: 'en',
boolTrueLabel: "Yes",
boolFalseLabel: "No",
clearFilterLabel: "Clear filter"
};
/***
* ============= FILTER WIDGETS =============
* Filter widget allows onRender custom filter user interface for different columns.
* For example if your added column is of type "DateTime" - widget can onRender calendar for picking filter value.
* This script provider base widget for default .Net types: System.String, System.Int32, System.Decimal etc.
* If you want to provide custom filter functionality - you can assign your own widget type for column and write widget for this types.
* For more documentation see: http://gridmvc.codeplex.com/documentation
*/
/***
* TextFilterWidget - Provides filter interface for text columns (of type "System.String")
* This widget onRenders filter interface with conditions, which specific for text types: contains, starts with etc.
*/
TextFilterWidget = (function ($) {
function textFilterWidget() { }
/***
* This method must return type of columns that must be associated with current widget
* If you not specify your own type name for column (see 'SetFilterWidgetType' method), GridMvc setup column type name from .Net type ("System.DateTime etc.)
*/
textFilterWidget.prototype.getAssociatedTypes = function () { return ["System.String"]; };
/***
* This method invokes when filter widget was shown on the page
*/
textFilterWidget.prototype.onShow = function () {
var textBox = this.container.find(".grid-filter-input");
if (textBox.length <= 0) return;
textBox.focus();
};
/***
* This method specify whether onRender 'Clear filter' button for this widget.
*/
textFilterWidget.prototype.showClearFilterButton = function () { return true; };
/***
* This method will invoke when user first clicked on filter button.
* container - html element, which must contain widget layout;
* lang - current language settings;
* typeName - current column type (if widget assign to multipile types, see: getAssociatedTypes);
* filterType - current filter type (like equals, starts with etc);
* filterValue - current filter value;
* cb - callback function that must invoked when user want to filter this column. Widget must pass filter type and filter value.
*/
textFilterWidget.prototype.onRender = function (container, lang, typeName, values, cb) {
this.cb = cb;
this.container = container;
this.lang = lang;
this.value = values.length > 0 ? values[0] : { filterType: 2, filterValue: "" };//support only one filter value
this.renderWidget();
this.registerEvents();
};
/***
* Internal method that build widget layout and append it to the widget container
*/
textFilterWidget.prototype.renderWidget = function () {
var html = '<div class="form-group">\
<label>' + this.lang.filterTypeLabel + '</label>\
<select class="grid-filter-type form-control">\
<option value="1" ' + (this.value.filterType == "1" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.Equals + '</option>\
<option value="2" ' + (this.value.filterType == "2" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.Contains + '</option>\
<option value="3" ' + (this.value.filterType == "3" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.StartsWith + '</option>\
<option value="4" ' + (this.value.filterType == "4" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.EndsWith + '</option>\
</select>\
</div>\
<div class="form-group">\
<label>' + this.lang.filterValueLabel + '</label>\
<input type="text" class="grid-filter-input form-control" value="' + this.value.filterValue + '" />\
</div>\
<div class="grid-filter-buttons">\
<button type="button" class="btn btn-primary grid-apply" >' + this.lang.applyFilterButtonText + '</button>\
</div>';
this.container.append(html);
};
/***
* Internal method that register event handlers for 'apply' button.
*/
textFilterWidget.prototype.registerEvents = function () {
//get apply button from:
var applyBtn = this.container.find(".grid-apply");
//save current context:
var $context = this;
//register onclick event handler
applyBtn.click(function () {
//get selected filter type:
var type = $context.container.find(".grid-filter-type").val();
//get filter value:
var value = $context.container.find(".grid-filter-input").val();
//invoke callback with selected filter values:
var filterValues = [{ filterType: type, filterValue: value }];
$context.cb(filterValues);
});
//register onEnter event for filter text box:
this.container.find(".grid-filter-input").keyup(function (event) {
if (event.keyCode == 13) { applyBtn.click(); }
if (event.keyCode == 27) { GridMvc.closeOpenedPopups(); }
});
};
return textFilterWidget;
})(window.jQuery);
/***
* NumberFilterWidget - Provides filter interface for number columns
* This widget onRenders filter interface with conditions, which specific for number types: great than, less that etc.
* Also validates user's input for digits
*/
NumberFilterWidget = (function ($) {
function numberFilterWidget() { }
numberFilterWidget.prototype.showClearFilterButton = function () { return true; };
numberFilterWidget.prototype.getAssociatedTypes = function () {
return ["System.Int32", "System.Double", "System.Decimal", "System.Byte", "System.Single", "System.Float", "System.Int64"];
};
numberFilterWidget.prototype.onShow = function () {
var textBox = this.container.find(".grid-filter-input");
if (textBox.length <= 0) return;
textBox.focus();
};
numberFilterWidget.prototype.onRender = function (container, lang, typeName, values, cb) {
this.cb = cb;
this.container = container;
this.lang = lang;
this.typeName = typeName;
this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };//support only one filter value
this.renderWidget();
this.registerEvents();
};
numberFilterWidget.prototype.renderWidget = function () {
var html = '<div class="form-group">\
<label>' + this.lang.filterTypeLabel + '</label>\
<select class="grid-filter-type form-control">\
<option value="1" ' + (this.value.filterType == "1" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.Equals + '</option>\
<option value="5" ' + (this.value.filterType == "5" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.GreaterThan + '</option>\
<option value="6" ' + (this.value.filterType == "6" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.LessThan + '</option>\
</select>\
</div>\
<div class="form-group">\
<label>' + this.lang.filterValueLabel + '</label>\
<input type="text" class="grid-filter-input form-control" value="' + this.value.filterValue + '" />\
</div>\
<div class="grid-filter-buttons">\
<button type="button" class="btn btn-primary grid-apply">' + this.lang.applyFilterButtonText + '</button>\
</div>';
this.container.append(html);
};
numberFilterWidget.prototype.registerEvents = function () {
var $context = this;
var applyBtn = this.container.find(".grid-apply");
applyBtn.click(function () {
var type = $context.container.find(".grid-filter-type").val();
var value = $context.container.find(".grid-filter-input").val();
var filters = [{ filterType: type, filterValue: value }];
$context.cb(filters);
});
var txt = this.container.find(".grid-filter-input");
txt.keyup(function (event) {
if (event.keyCode == 13) { applyBtn.click(); }
if (event.keyCode == 27) { GridMvc.closeOpenedPopups(); }
})
.keypress(function (event) { return $context.validateInput.call($context, event); });
if (this.typeName == "System.Byte")
txt.attr("maxlength", "3");
};
numberFilterWidget.prototype.validateInput = function (evt) {
var $event = evt || window.event;
var key = $event.keyCode || $event.which;
key = String.fromCharCode(key);
var regex;
switch (this.typeName) {
case "System.Byte":
case "System.Int32":
case "System.Int64":
regex = /[0-9]/;
break;
default:
regex = /[0-9]|\.|\,/;
}
if (!regex.test(key)) {
$event.returnValue = false;
if ($event.preventDefault) $event.preventDefault();
}
};
return numberFilterWidget;
})(window.jQuery);
/***
* DateTimeFilterWidget - Provides filter interface for date columns (of type "System.DateTime").
* If datepicker script included, this widget will render calendar for pick filter values
* In other case he onRender textbox field for specifing date value (more info at http://window.jQueryui.com/)
*/
DateTimeFilterWidget = (function ($) {
function dateTimeFilterWidget() { }
dateTimeFilterWidget.prototype.getAssociatedTypes = function () { return ["System.DateTime"]; };
dateTimeFilterWidget.prototype.showClearFilterButton = function () { return true; };
dateTimeFilterWidget.prototype.onRender = function (container, lang, typeName, values, applycb, data) {
this.datePickerIncluded = typeof ($.fn.datepicker) != 'undefined';
this.cb = applycb;
this.data = data;
this.container = container;
this.lang = lang;
this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };//support only one filter value
this.renderWidget();
this.registerEvents();
};
dateTimeFilterWidget.prototype.renderWidget = function () {
var html = '<div class="form-group">\
<label>' + this.lang.filterTypeLabel + '</label>\
<select class="grid-filter-type form-control">\
<option value="1" ' + (this.value.filterType == "1" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.Equals + '</option>\
<option value="5" ' + (this.value.filterType == "5" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.GreaterThan + '</option>\
<option value="6" ' + (this.value.filterType == "6" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.LessThan + '</option>\
</select>\
</div>' +
(this.datePickerIncluded ?
'<div class="grid-filter-datepicker"></div>'
:
'<div class="form-group">\
<label>' + this.lang.filterValueLabel + '</label>\
<input type="text" class="grid-filter-input form-control" value="' + this.value.filterValue + '" />\
</div>\
<div class="grid-filter-buttons">\
<input type="button" class="btn btn-primary grid-apply" value="' + this.lang.applyFilterButtonText + '" />\
</div>');
this.container.append(html);
//if window.jQueryUi included:
if (this.datePickerIncluded) {
var datePickerOptions = this.data || {};
datePickerOptions.format = datePickerOptions.format || "yyyy-mm-dd";
datePickerOptions.language = datePickerOptions.language || this.lang.code;
var $context = this;
var dateContainer = this.container.find(".grid-filter-datepicker");
dateContainer.datepicker(datePickerOptions).on('changeDate', function (ev) {
var type = $context.container.find(".grid-filter-type").val();
var filterValues = [{ filterType: type, filterValue: ev.format() }];
$context.cb(filterValues);
});
if (this.value.filterValue)
dateContainer.datepicker('update', this.value.filterValue);
}
};
dateTimeFilterWidget.prototype.registerEvents = function () {
var $context = this;
var applyBtn = this.container.find(".grid-apply");
applyBtn.click(function () {
var type = $context.container.find(".grid-filter-type").val();
var value = $context.container.find(".grid-filter-input").val();
var filterValues = [{ filterType: type, filterValue: value }];
$context.cb(filterValues);
});
this.container.find(".grid-filter-input").keyup(function (event) {
if (event.keyCode == 13) {
applyBtn.click();
}
});
};
return dateTimeFilterWidget;
})(window.jQuery);
/***
* BooleanFilterWidget - Provides filter interface for boolean columns (of type "System.Boolean").
* Renders two button for filter - true and false
*/
BooleanFilterWidget = (function ($) {
function booleanFilterWidget() { }
booleanFilterWidget.prototype.getAssociatedTypes = function () { return ["System.Boolean"]; };
booleanFilterWidget.prototype.showClearFilterButton = function () { return true; };
booleanFilterWidget.prototype.onRender = function (container, lang, typeName, values, cb) {
this.cb = cb;
this.container = container;
this.lang = lang;
this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };//support only one filter value
this.renderWidget();
this.registerEvents();
};
booleanFilterWidget.prototype.renderWidget = function () {
var html = '<label>' + this.lang.filterValueLabel + '</label>\
<ul class="menu-list">\
<li><a class="grid-filter-choose ' + (this.value.filterValue == "true" ? "choose-selected" : "") + '" data-value="true" href="javascript:void(0);">' + this.lang.boolTrueLabel + '</a></li>\
<li><a class="grid-filter-choose ' + (this.value.filterValue == "false" ? "choose-selected" : "") + '" data-value="false" href="javascript:void(0);">' + this.lang.boolFalseLabel + '</a></li>\
</ul>';
this.container.append(html);
};
booleanFilterWidget.prototype.registerEvents = function () {
var $context = this;
var applyBtn = this.container.find(".grid-filter-choose");
applyBtn.click(function () {
var filterValues = [{ filterType: "1", filterValue: $(this).attr("data-value") }];
$context.cb(filterValues);
});
};
return booleanFilterWidget;
})(window.jQuery);
//startup init:
(function ($) {
if (!$) return;//jquery not referenced
$(function () {
$(".grid-mvc").each(function () {
$(".grid-mvc").gridmvc();
});
});
})(window.jQuery);