-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
the filter & server mode now play nicely with the colReorder extension #532
Changes from all commits
572ed69
dd4d18d
5917f34
6631dc5
50fc64a
bc15293
b3172de
6ccd478
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,15 +257,28 @@ HTMLWidgets.widget({ | |
options.ajax.dataSrc = function(json) { | ||
DT_rows_all = $.makeArray(json.DT_rows_all); | ||
DT_rows_current = $.makeArray(json.DT_rows_current); | ||
return json.data; | ||
var data = json.data; | ||
if (!colReorderEnabled()) return data; | ||
var table = $table.DataTable(), order = table.colReorder.order(), flag = true, i, j, row; | ||
for (i = 0; i < order.length; ++i) if (order[i] !== i) flag = false; | ||
if (flag) return data; | ||
for (i = 0; i < data.length; ++i) { | ||
row = data[i].slice(); | ||
for (j = 0; j < order.length; ++j) data[i][j] = row[order[j]]; | ||
} | ||
return data; | ||
}; | ||
} | ||
|
||
var thiz = this; | ||
if (instance.fillContainer) $table.on('init.dt', function(e) { | ||
thiz.fillAvailableHeight(el, $(el).innerHeight()); | ||
}); | ||
|
||
// If the page contains serveral datatables and one of which enables colReorder, | ||
// the table.colReorder.order() function will exist but throws error when called. | ||
// So it seems like the only way to know if colReorder is enabled or not is to | ||
// check the options. | ||
var colReorderEnabled = function() { return "colReorder" in options; }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it have to be a function? I mean, does this work? var colReorderEnabled = "colReorder" in options; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I used the non-function version at first and worked fine... However, I overthinked that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. It should not matter much. I'm fine to use a function here. |
||
var table = $table.DataTable(options); | ||
$el.data('datatable', table); | ||
|
||
|
@@ -613,22 +626,29 @@ HTMLWidgets.widget({ | |
if (typeof filter === 'undefined' || !$td.data('filter')) return true; | ||
|
||
var r = filter.val(), v, r0, r1; | ||
var i_data = function(i) { | ||
if (!colReorderEnabled()) return i; | ||
var order = table.colReorder.order(), k; | ||
for (k = 0; k < order.length; ++k) if (order[k] === i) return k; | ||
return i; // in theory it will never be here... | ||
} | ||
v = data[i_data(i)]; | ||
if (type === 'number' || type === 'integer') { | ||
v = parseFloat(data[i]); | ||
v = parseFloat(v); | ||
// how to handle NaN? currently exclude these rows | ||
if (isNaN(v)) return(false); | ||
r0 = parseFloat(scaleBack(r[0], scale)) | ||
r1 = parseFloat(scaleBack(r[1], scale)); | ||
if (v >= r0 && v <= r1) return true; | ||
} else if (type === 'date' || type === 'time') { | ||
v = new Date(data[i]); | ||
v = new Date(v); | ||
r0 = new Date(r[0] / scale); r1 = new Date(r[1] / scale); | ||
if (v >= r0 && v <= r1) return true; | ||
} else if (type === 'factor') { | ||
if (r.length === 0 || inArray(data[i], r)) return true; | ||
if (r.length === 0 || inArray(v, r)) return true; | ||
} else if (type === 'logical') { | ||
if (r.length === 0) return true; | ||
if (inArray(data[i] === '' ? 'na' : data[i], r)) return true; | ||
if (inArray(v === '' ? 'na' : v, r)) return true; | ||
} | ||
return false; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there is a way to reorder the data on the server side. If there is, the implementation could be much easier (
data[order]
in R instead of a nested loop in JS). Of course, it depends on whether we have the order information on the server side.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but the
datatables
library doesn't send the reordered column info to the server by default. We need to register a callback in the eventcolumn-reorder
, which sends the info to the R side. If this method is better, I guess it's not very difficult to make the change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, let's just forget it. I'm not too concerned about the performance of the nested loops here since
data
should typically be small enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the concern too... For the deep copy and nested loops... but as you said, it should be fine in the most cases.