-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[technical-support-137] New feature to modify columns
- Loading branch information
1 parent
b6e74f4
commit 00e1a6f
Showing
4 changed files
with
161 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<div class="dropdown-container"> | ||
<div class="dropdown"> | ||
<button class="btn btn-default dropdown-toggle toggle-button-right" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> | ||
<i class="glyphicon glyphicon-cog"></i> | ||
<strong>Edit Columns</strong> | ||
</button> | ||
<ul class="dropdown-menu" id="column-visibility" aria-labelledby="v"> | ||
<% Kaui.account_search_columns.call()[0].each_with_index do |title, index| %> | ||
<li class="list-group-item-manual" data-id="<%= index %>"> | ||
<label class="label-group-item-manual"> | ||
<input type="checkbox" class="column-toggle" draggable="true" data-column="<%= index %>" checked> <%= title %> | ||
<span class="glyphicon glyphicon-option-vertical icon-drag" aria-hidden="true"></span> | ||
</label> | ||
</li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
</div> | ||
|
||
<style> | ||
.dropdown-menu#column-visibility { | ||
max-height: 300px; | ||
width: 200px; | ||
overflow-y: auto; | ||
} | ||
|
||
.dropdown-menu { | ||
padding: 5px; | ||
} | ||
|
||
.toggle-button-right { | ||
float: right; | ||
margin-bottom: 10px; | ||
background-color: white; | ||
color: black; | ||
text-transform: none; | ||
border: 1px solid #ccc; | ||
padding: 8px 15px; | ||
} | ||
|
||
.icon-drag { | ||
float: right; | ||
padding: 5px; | ||
} | ||
|
||
.dropdown-container { | ||
display: flex; | ||
justify-content: flex-end; | ||
} | ||
|
||
.label-group-item-manual { | ||
margin: 5px; | ||
width: -webkit-fill-available; | ||
cursor: grab; | ||
} | ||
.label-group-item-manual:active { | ||
cursor: grabbing; | ||
} | ||
</style> | ||
|
||
<%= javascript_tag do %> | ||
$(document).ready(function() { | ||
updateDropdownOrder(); | ||
|
||
function loadState() { | ||
var state = JSON.parse(localStorage.getItem('DataTables_accounts-table_' + window.location.pathname)); | ||
return state || { columns: [], columnOrder: [] }; | ||
} | ||
|
||
function updateDropdownOrder() { | ||
var state = loadState(); | ||
var columnOrder = state.ColReorder; | ||
var $list = $('#column-visibility'); | ||
var thElements = document.querySelectorAll('#accounts-table th'); | ||
var $columnTitles = Array.from(thElements).map(function(th) { | ||
return th.textContent.trim(); | ||
}); | ||
if (columnOrder !== undefined) { | ||
$list.empty(); | ||
columnOrder.forEach(function(colIdx, index) { | ||
var $item = $('<li>', { class: "list-group-item-manual", "data-id": index }); | ||
var column = state.columns[colIdx]; | ||
var col_name = $columnTitles[colIdx]; | ||
var $label = $('<label>', { | ||
class: "label-group-item-manual", | ||
}); | ||
var $checkbox = $("<input>", { | ||
type: "checkbox", | ||
value: colIdx, | ||
checked: column.visible, | ||
"data-column": colIdx, | ||
class: "column-toggle" | ||
}); | ||
$label.append($checkbox).append(" " + col_name); | ||
var $icon = $("<span>", { class: "glyphicon glyphicon-option-vertical icon-drag"}); | ||
$label.append($icon); | ||
$item.append($label); | ||
$list.append($item); | ||
}); | ||
} | ||
resetDataColumn(); | ||
resetDataId(); | ||
} | ||
|
||
$("#column-visibility").sortable({ | ||
axis: "y", | ||
containment: "parent", | ||
stop: function(event, ui) { | ||
var order = $("#column-visibility").sortable('toArray', {attribute: 'data-id'}); | ||
reorderTableColumns(order); | ||
} | ||
}); | ||
$("#column-visibility").disableSelection(); | ||
|
||
function reorderTableColumns(order) { | ||
var table = $('#accounts-table').DataTable(); | ||
var columnIndexes = order.map(Number); | ||
console.log('New column order:', columnIndexes); | ||
table.colReorder.order(columnIndexes); | ||
resetDataColumn(); | ||
resetDataId(); | ||
} | ||
|
||
function resetDataId() { | ||
var elements = document.querySelectorAll('.list-group-item-manual'); | ||
elements.forEach(function(element, index) { | ||
element.setAttribute('data-id', index); | ||
}); | ||
} | ||
|
||
function resetDataColumn() { | ||
var elements = document.querySelectorAll('.column-toggle'); | ||
elements.forEach(function(element, index) { | ||
element.setAttribute('data-column', index); | ||
}); | ||
} | ||
|
||
$('.column-toggle').on('change', function() { | ||
var table = $('#accounts-table').DataTable(); | ||
var column = table.column($(this).attr('data-column')); | ||
column.visible(!column.visible()); | ||
}); | ||
}); | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters