forked from sebastieno/ajax-web-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sor.ajaxWebGrid.js
118 lines (97 loc) · 4.31 KB
/
sor.ajaxWebGrid.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
// AjaxWebGrid 1.1.0
// (c) Sébastien Ollivier - http://sebastienollivier.fr/blog/asp-net-mvc/plugin-jquery-ajaxwebgrid-1-1-0
(function ($) {
var ajaxWebGrid = function (element, options) {
this.settings = $.extend({}, {
gridActionUrl: "",
noResultContainerSelector: null,
sortFieldNameProperty: "sortFieldName",
sortDirectionProperty: "sortDirection",
pageIndexProperty: "page",
addCustomParameters: null,
refreshGridSuccess: function (data, grid, options) {
$(grid).replaceWith(data);
if (options.noResultContainerSelector) {
$(options.noResultContainerSelector).replaceWith(data);
}
},
refreshGridFailed: null
}, options);
this.grid = element;
var self = this;
$($(this.grid).parent()).on("click", this.grid.selector + ' thead a, ' + this.grid.selector + ' tfoot a', function (e) {
e.preventDefault();
var queryString = $(this).attr("href").substr($(this).attr("href").indexOf("?") + 1);
self.refreshGrid(queryString);
});
};
ajaxWebGrid.prototype = function () {
var getQueryStringValues = function (queryString) {
var result = {};
if (!queryString) {
return result;
}
var queryStringSplitted = queryString.split('&');
for (var i = 0; i < queryStringSplitted.length; i++) {
var elements = queryStringSplitted[i].split('=');
var parameterKey = elements[0];
var parameterValue = elements[1];
if (!result[parameterKey]) {
result[parameterKey] = parameterValue;
}
}
return result;
};
var getParameters = function (queryStringValues) {
var parameters = {};
parameters[this.settings.sortFieldNameProperty] = queryStringValues[this.settings.sortFieldNameProperty];
parameters[this.settings.sortDirectionProperty] = queryStringValues[this.settings.sortDirectionProperty];
parameters[this.settings.pageIndexProperty] = queryStringValues[this.settings.pageIndexProperty];
if (this.settings.addCustomParameters) {
var customParameters = this.settings.addCustomParameters();
for (var parameterName in customParameters) {
if (customParameters.hasOwnProperty(parameterName)) {
parameters[parameterName] = customParameters[parameterName];
}
}
}
return parameters;
};
var makeAjaxCall = function (parameters) {
var self = this;
$.ajax({
url: self.settings.gridActionUrl,
type: "GET",
data: parameters,
dataType: "html",
success: function (data) {
if (self.settings.refreshGridSuccess) {
self.settings.refreshGridSuccess(data, $(self.grid.selector), self.settings);
}
$(self.grid.selector).data('ajaxWebGrid', self);
if (self.settings.noResultContainerSelector) {
$(self.settings.noResultContainerSelector).data('ajaxWebGrid', self);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (self.settings.refreshGridFailed) {
self.settings.refreshGridFailed(XMLHttpRequest, textStatus, errorThrown, $(self.grid.selector), self.settings);
}
}
});
};
var refreshGrid = function (queryString) {
var queryStringValues = getQueryStringValues.call(this, queryString);
var parameters = getParameters.call(this, queryStringValues);
makeAjaxCall.call(this, parameters);
};
return {
refreshGrid: refreshGrid
};
}();
$.fn.asAjaxWebGrid = function (options) {
var newAjaxWebGrid = new ajaxWebGrid(this, options);
$(this).data('ajaxWebGrid', newAjaxWebGrid);
return this;
};
} (jQuery));