jqgrid.js
10.6 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
var sort_rules = $dj.sort_rules, colModel = $dj.colModel,
colNames = $dj.colNames, jqgrid_filteroptions;
var timeoutHnd;
var changed;
var jqgrid = {
grid: undefined,
ctrl: undefined // do pokazywania, ze laduje
};
function put_filter(filter) {
"use strict";
jqgrid.grid.jqGrid('getGridParam', 'postData').filters = filter;
var filter_element = $('.searchFilter')[0],
coord = $.jgrid.findPos(filter_element);
jqgrid.grid.jqGrid('searchGrid', $.extend(jqgrid_filteroptions, {
left: coord[0] - filter_element.offsetLeft,
top: coord[1] - filter_element.offsetTop,
recreateFilter: true,
loadDefaults: true
}));
}
jqgrid.put_filter = put_filter;
function refresh_column_headers() {
"use strict";
var thd= $("thead:first", $('#scroll')[0].grid.hDiv).get(0);
var rules = $.evalJSON(
jqgrid.grid.jqGrid('getGridParam', 'postData').sort_rules);
$("tr th span.s-ico",thd).hide();
$("tr th span.ui-icon",thd).addClass('ui-state-disabled');
$("text").remove('.sort_priority');
for (var i = 0; i < rules.length; i++) {
$("tr th[id='scroll_"+rules[i].field+"'] span.s-ico",thd).show();
$("tr th[id='scroll_"+rules[i].field+"'] span.ui-icon-" + rules[i].order,thd).removeClass('ui-state-disabled');
$('tr th div#jqgh_'+rules[i].field+"'", thd).append('<text class="sort_priority">'+(i+1)+'</text>');
}
}
jqgrid.refresh_column_headers = refresh_column_headers;
function sort_column(colName, index, order) {
"use strict";
var data = jqgrid.grid.jqGrid('getGridParam', 'postData');
var sort_rules = [{field: colName, order: order}];
if ($.isFunction(jqgrid.prepare_sort_rules)) {
var old_sort_rules = $.evalJSON(data.sort_rules);
jqgrid.prepare_sort_rules(sort_rules, old_sort_rules);
}
data.sort_rules = $.toJSON(sort_rules);
jqgrid.grid.jqGrid('setGridParam', {setOnComplete: true});
refresh_column_headers();
}
$(function() {
"use strict";
jqgrid_filteroptions = {
multipleSearch: true,
overlay: false,
top: 300,
left: 300,
sopt: ['eq','ne','bw','bn','ew','en','cn','nc', 're', 'nr'],
ops: [
{"name": "eq", "description": "equal", "operator":"="},
{"name": "ne", "description": "not equal", "operator":"<>"},
{"name": "lt", "description": "less", "operator":"<"},
{"name": "le", "description": "less or equal","operator":"<="},
{"name": "gt", "description": "greater", "operator":">"},
{"name": "ge", "description": "greater or equal", "operator":">="},
{"name": "bw", "description": "begins with", "operator":"LIKE"},
{"name": "bn", "description": "does not begin with", "operator":"NOT LIKE"},
{"name": "in", "description": "in", "operator":"IN"},
{"name": "ni", "description": "not in", "operator":"NOT IN"},
{"name": "ew", "description": "ends with", "operator":"LIKE"},
{"name": "en", "description": "does not end with", "operator":"NOT LIKE"},
{"name": "cn", "description": "contains", "operator":"LIKE"},
{"name": "nc", "description": "does not contain", "operator":"NOT LIKE"},
{"name": "nu", "description": "is null", "operator":"IS NULL"},
{"name": "nn", "description": "is not null", "operator":"IS NOT NULL"},
{"name": "re", "description": "matches regex", "operator":"REGEXP"},
{"name": "nr", "description": "does not match regex", "operator":"NOT REGEXP"},
{"name": "se", "description": "equal for sure", "operator":"="},
{"name": "sd", "description": "different for sure", "operator":"<>"},
{"name": "me", "description": "maybe equal", "operator":"="},
{"name": "md", "description": "maybe different", "operator":"<>"}
],
beforeShowSearch: function(f) {
f.closest('.ui-jqdialog').appendTo('body')
.dialog('option', 'position', 'center');
return true;
},
onReset: function() {
var init = {
"groupOp": "AND",
rules: [{"field": jqgrid.main_field, "op": "eq", "data": ""}]
};
put_filter(init);
},
onSearch: function() {
$('#phrase_box').val('');
jqgrid.grid.jqGrid('getGridParam', 'postData').force_reload = true;
},
onInitializeSearch: jqgrid.add_buttons
};
if (!colModel) {
colModel = jqgrid.initialColModel;
}
if (!colNames) {
colNames = jqgrid.initialColNames;
}
jqgrid.grid = $('#scroll');
var session_data = {};
if ($dj.filters) {
session_data.filters = $dj.filters;
}
if (sort_rules) {
sort_rules = $.evalJSON(sort_rules);
} else {
sort_rules = jqgrid.initial_sort_rules;
}
session_data.sort_rules = $.toJSON(sort_rules);
jqgrid.grid.jqGrid({
url: $dj.ajax_get_page,
findIdUrl : $dj.ajax_find_id,
datatype: "json",
colNames: colNames,
colModel: colModel,
rowNum: 50,
rowList : [20,30,50],
scroll: 1,
autowidth: true,
height: $('#left').height() - 27 - $('#search-panel').height(),
mtype: "GET",
rownumbers: false,
gridview: true,
//pager: '#pager',
viewrecords: true,
caption: jqgrid.grid_caption,
sortname: jqgrid.main_field,
sortorder: 'asc',
beforeSelectRow: function() {
var ok = true;
if (changed) {
ok = window.confirm('Czy chcesz porzucić niezapisane zmiany?');
}
return ok;
},
onSelectRow: jqgrid.load_content,
beforeRequest: function() {
var empty, filters;
if (!jqgrid.grid.jqGrid('getGridParam', 'search')) {
empty = true;
} else {
filters = jqgrid.grid.jqGrid('getGridParam', 'postData').filters;
if (typeof(filters) === "string") {
filters = $.parseJSON(filters);
}
if (typeof(filters) !== "object" || filters === null || !filters.rules) {
empty = true;
} else {
empty = (filters.rules.length === 0);
}
}
$('#filter-button').children().first().toggleClass('ui-state-highlight', !empty);
},
gridComplete: function() {
if (jqgrid.grid.jqGrid('getGridParam', 'setOnComplete') === true) {
jqgrid.grid.jqGrid('scrollToLastSelection');
jqgrid.grid.jqGrid('setGridParam', {'setOnComplete' : false});
}
},
onSortCol: sort_column,
loadComplete: function(data) {
var num = data.records;
document.title = jqgrid.grid_caption + ' (' + num + ')';
if (jqgrid.grid.jqGrid('getGridParam', 'postData').force_reload === true) {
jqgrid.grid.jqGrid('getGridParam', 'postData').force_reload = false;
setTimeout(findAndScroll, 100);
}
},
remapColumns: $dj.remap,
postData: session_data,
search: Boolean(session_data.filters)
});
// nawigacja klawiaturą powoduje zmulenie,
// bo wszystkie leksemy muszą się załadować
//jqgrid.grid.jqGrid('bindKeys');
$('#filter-button').click(function() {
jqgrid.grid.jqGrid('searchGrid', jqgrid_filteroptions);
});
refresh_column_headers();
findAndScroll();
$('#' + jqgrid.edit_form_id).live('submit', jqgrid.edit_form_submit);
$('#' + jqgrid.edit_form_cancel_id).live('click', function() {
var id = $('input[name=id]', this.form).value();
$('#edit').load($dj.ajax_edit_form, 'id='+id, jqgrid.edit_form_init);
});
var keyup = function() {
if (this.value !== this.defaultValue) {
show_changed();
}
};
$('#' + jqgrid.edit_form_id + ' input').live('keyup', keyup);
$('#' + jqgrid.edit_form_id + ' textarea').live('keyup', keyup);
$('#' + jqgrid.edit_form_id + ' select').live('change', keyup);
$('#' + jqgrid.edit_form_id + ' input[type=checkbox]').live(
'change', show_changed);
$('#searchButton').click(findAndScroll);
$('#phrase_box').keydown(doSearch);
// ajax niepotrzebny...
$("#sort-dialog-div").load($dj.ajax_sort_rules,
{
colModel : $.toJSON(jqgrid.grid.jqGrid('getGridParam', 'colModel')),
colNames : $.toJSON(jqgrid.grid.jqGrid('getGridParam', 'colNames'))
},
function(){
$("#sort-dialog-form").dialog({
autoOpen: false,
height: 'auto',
width: 'auto',
modal: true
});
});
$('#open-sort-dialog').click(function() {
$('#sort-dialog-form').dialog("open");
var rules = $.evalJSON($('#scroll').jqGrid(
'getGridParam', 'postData').sort_rules);
$('#sort-rule-list').find('li').removeClass('ui-state-default');
$.each(rules.reverse(), function(i, rule) {
var colName = rule.field,
li = $('li#' + colName);
if ($.isFunction(jqgrid.sort_rule_special_features))
jqgrid.sort_rule_special_features(rule);
li.addClass('ui-state-default');
li.find('select').val(rule.order);
$('#sort-rule-list').prepend(li);
});
});
$('#show-columns-button').click(function(){
jqgrid.grid.jqGrid('columnChooser', {
done: function(perm) {
if (perm) this.jqGrid('remapColumns', perm, true);
var col_model = jqgrid.grid.jqGrid('getGridParam', 'colModel');
var col_names = jqgrid.grid.jqGrid('getGridParam', 'colNames');
var remap = jqgrid.grid.jqGrid('getGridParam', 'remapColumns');
$.ajaxJSON({
method: 'post',
url: $dj.ajax_save_columns,
data: {col_model: col_model, col_names: col_names, remap: remap}
});
}
});
});
$('.searchFilter input').live('keypress', function(e) {
if (e.which === 13) { // enter
$(this).change();
// powinno być .ui-search, niegroźny byk w jqGrid
setTimeout(function () {
$('.searchFilter').next().find('.ui-reset').click();}, 100);
}
});
jqgrid.init_form_widgets();
});
function doSearch(){
"use strict";
if (!$dj.auto_search)
return;
if(timeoutHnd)
clearTimeout(timeoutHnd);
timeoutHnd = setTimeout(findAndScroll, 500);
}
function findAndScroll(){
"use strict";
var data = jqgrid.grid.jqGrid('getGridParam', 'postData');
data.mask = $("#phrase_box").val();
// przeładowanie można pominąć jeśli nie jesteśmy w trybie filtrowania
if ($dj.filtering_mode) {
jqgrid.grid.trigger('reloadGrid');
}
$.get($dj.ajax_location, data, function(resp) {
var selectedRow = resp.selected_id + "";
var rowIndex = resp.rowIndex;
var recordsCount = resp.records;
if (recordsCount > 0) {
jqgrid.grid.jqGrid('scrollAndSet', selectedRow, rowIndex, recordsCount);
}
});
}
jqgrid.findAndScroll = findAndScroll;
function show_changed() {
"use strict";
if (!changed) {
changed = true;
$('#' + jqgrid.edit_form_submit_id).button('enable');
$('#' + jqgrid.edit_form_cancel_id).button('enable');
$('#edit-tab').html('<strong>Edycja (niezapisane)</strong>');
}
}
jqgrid.show_changed = show_changed;
function hide_changed() {
"use strict";
if (changed) {
changed = false;
$('#edit-tab').html('Edycja');
}
}
jqgrid.hide_changed = hide_changed;