Update the ordering upon table load

This commit is contained in:
Luka Murn 2018-04-20 09:22:23 +02:00
parent 21e7e7057e
commit caab0cf55c
2 changed files with 37 additions and 10 deletions

View file

@ -1,5 +1,7 @@
//= require jquery-ui/widgets/sortable
<% environment.context_class.instance_eval { include RepositoryDatatableHelper } %>
var RepositoryDatatable = (function(global) {
'use strict';
@ -101,16 +103,12 @@ var RepositoryDatatable = (function(global) {
$(row).addClass('selected');
}
},
// Next 2 options are provided by server-side default state
// (and get overriden once state load from server kicks in)
order: <%= Constants::REPOSITORY_TABLE_DEFAULT_STATE[:order][0].to_s %>,
<% # Next 2 options are provided by server-side default state
# (and get overriden once state load from server kicks in) %>
order: <%= default_table_order_as_js_array %>,
columns: (function() {
var numOfColumns = $(TABLE_ID).data('num-columns');
var columns =
<%= Constants::REPOSITORY_TABLE_DEFAULT_STATE[:columns].keys.sort.map do |k|
col = Constants::REPOSITORY_TABLE_DEFAULT_STATE[:columns][k]
col.slice(:visible, :searchable)
end.to_json %>;
var columns = <%= default_table_columns %>;
for (var i = 0; i < numOfColumns; i++) {
if (columns[i] == undefined) {
// This should only occur for custom columns
@ -153,6 +151,12 @@ var RepositoryDatatable = (function(global) {
type: 'POST',
success: function(json) {
myData = json.state;
// Fix the order - convert it from index-keyed JS object that
// is returned from the server state into true JS array;
// e.g. {0: [2, 'asc'], 1: [3, 'desc']}
// is converted into [[2, 'asc'], [3, 'desc']]
myData.order = _.toArray(myData.order);
}
});
return myData;
@ -173,10 +177,11 @@ var RepositoryDatatable = (function(global) {
loadFirstTime = false;
},
fnInitComplete: function(oSettings) {
// Reload correct column order and visibility (if you refresh page)
// First two columns are fixed
TABLE.column(0).visible(true);
TABLE.column(1).visible(true);
// Reload correct column order and visibility (if you refresh page)
for (var i = 2; i < TABLE.columns()[0].length; i++) {
var visibility = false;
if (myData.columns && myData.columns[i]) {
@ -188,6 +193,13 @@ var RepositoryDatatable = (function(global) {
TABLE.column(i).visible(visibility);
TABLE.setColumnSearchable(i, visibility);
}
// Re-order table as per loaded state
if (myData.order) {
TABLE.order(myData.order);
TABLE.draw();
}
// Datatables triggers this action about 3 times
// sometimes on the first iteration the oSettings._colReorder is null
// and the fnOrder rises an error that breaks the table

View file

@ -65,4 +65,19 @@ module RepositoryDatatableHelper
can_create_repositories?(team) ||
can_manage_repository_rows?(team)
end
end
# The order must be converted from Ruby Hash into a JS array -
# because arrays in JS are in truth regular JS objects with indexes as keys
def default_table_order_as_js_array
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:order].keys.sort.map do |k|
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:order][k]
end.to_s
end
def default_table_columns
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:columns].keys.sort.map do |k|
col = Constants::REPOSITORY_TABLE_DEFAULT_STATE[:columns][k]
col.slice(:visible, :searchable)
end.to_json
end
end