slickgrid.js
6.3 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
var loader = new Slick.Data.RemoteModel(
$dj.ajax_get_page, $dj.ajax_search_index);
function search_string() {
"use strict";
return $('#text-search').val();
}
var slickgrid = {
grid: null,
active_id: function(cell) {
"use strict";
if (cell === undefined)
cell = slickgrid.grid.getActiveCell();
var row = cell.row;
if (row === undefined)
return undefined;
return slickgrid.grid.getDataItem(row).id;
},
scroll_search: function() {
"use strict";
// data = aktualne filtry i sort_rules
loader.searchRow(search_string(), function (row) {
slickgrid.grid.scrollRowToTop(row);
slickgrid.grid.setActiveCell(row, 0);
});
},
search: function() {
"use strict";
if ($dj.filtering_mode) {
loader.setSearch(search_string());
slickgrid.grid.scrollRowToTop(0);
slickgrid.ensure_data();
slickgrid.grid.setActiveCell(0, 0);
} else {
slickgrid.scroll_search();
}
},
ensure_data: function() {
"use strict";
var vp = slickgrid.grid.getViewport();
loader.ensureData(vp.top, vp.bottom);
},
reload_data: function() {
"use strict";
var vp = slickgrid.grid.getViewport();
loader.reloadData(vp.top, vp.bottom);
}
};
var options = {
editable: false,
enableAddRow: false,
enableCellNavigation: true
};
var loadingIndicator = null;
function set_columns() {
"use strict";
var chosen_column_ids = $('#column-list').val();
var chosen_columns = $.map(chosen_column_ids, function(id) {
return slickgrid.all_columns[id];
});
slickgrid.grid.setColumns(chosen_columns);
slickgrid.grid.autosizeColumns();
/*$.ajaxJSON({
method: 'post',
url: $dj.ajax_save_columns,
data: {columns: chosen_columns}
});*/ //TODO
}
$(function() {
"use strict";
slickgrid.element = $('#' + slickgrid.grid_element_id);
slickgrid.grid = new Slick.Grid(
slickgrid.element, loader.data, slickgrid.columns, options);
//slickgrid.grid.setSelectionModel(new Slick.RowSelectionModel());
slickgrid.grid.onViewportChanged.subscribe(function (e, args) {
slickgrid.ensure_data();
});
slickgrid.grid.onSort.subscribe(function (e, args) {
loader.setSort(args.sortCol.field, args.sortAsc ? 1 : -1);
slickgrid.ensure_data();
});
var timeout_handler;
var last_active_row;
slickgrid.grid.onActiveCellChanged.subscribe(function(e, cell) {
if (cell.row === last_active_row)
return;
if (last_active_row !== undefined && edit.changed) {
if (!window.confirm('Czy chcesz porzucić niezapisane zmiany?')) {
slickgrid.grid.setActiveCell(last_active_row, 0);
return;
}
}
last_active_row = cell.row;
if (cell.row !== undefined) {
clearTimeout(timeout_handler);
timeout_handler = setTimeout(function() {
common.busy_on();
slickgrid.load_content(slickgrid.active_id(cell));
}, 500);
}
});
loader.onDataLoading.subscribe(function () {
if (!loadingIndicator) {
loadingIndicator = $(
"<span class='loading-indicator'><label>Ładowanie..." +
"</label></span>").appendTo(document.body);
var $g = slickgrid.element;
loadingIndicator
.css("position", "absolute")
.css("top", $g.position().top + $g.height() / 2 - loadingIndicator.height() / 2)
.css("left", $g.position().left + $g.width() / 2 - loadingIndicator.width() / 2);
}
loadingIndicator.show();
});
loader.onDataLoaded.subscribe(function (e, args) {
for (var i = args.from; i <= args.to; i++) {
slickgrid.grid.invalidateRow(i);
}
slickgrid.grid.updateRowCount();
slickgrid.grid.render();
loadingIndicator.fadeOut();
layout.adjust_grid_size();
var num = slickgrid.grid.getDataLength();
document.title = slickgrid.grid_caption + ' (' + num + ')';
if (!slickgrid.grid.getActiveCell()) {
var vp = slickgrid.grid.getViewport();
slickgrid.grid.setActiveCell(vp.top, 0);
}
});
var search_timeout_handler;
$("#text-search").keyup(function (e) {
if (e.which === 13) {
slickgrid.search();
} else if ($dj.auto_search) {
clearTimeout(search_timeout_handler);
search_timeout_handler = setTimeout(slickgrid.search, 500);
}
});
if ($dj.filtering_mode) {
loader.setSearch(search_string());
}
loader.setSort($dj.sort_rules || slickgrid.initial_sort_rules);
loader.setFilters([]);
$('#search-button').click(slickgrid.search);
// load the first page
slickgrid.grid.onViewportChanged.notify();
$('#choose-columns-dialog').dialog({
autoOpen: false,
width: 'auto',
modal: true,
buttons: [
{
text: "Anuluj",
click: function () {
$(this).dialog("close");
}
},
{
text: "Zatwierdź",
click: function() {
set_columns();
$(this).dialog("close");
}
}
]
});
var column_list = $('#column-list');
$('#show-columns-button').click(function() {
$('#choose-columns-dialog').dialog('open');
column_list.multiselect('refresh');
});
var column_ids = [];
function add_column_option(column, selected) {
var option = $('<option/>');
option.attr('value', column.id);
option.prop('selected', selected);
option.text(column.name);
column_list.append(option);
}
$.each(slickgrid.columns, function(i, column) {
add_column_option(column, true);
column_ids.push(column.id);
});
$.each(slickgrid.all_columns, function(id, column) {
if (column_ids.indexOf(id) === -1)
add_column_option(column, false);
});
column_list.multiselect({
sortable: true
});
layout.adjust_tabs();
});