mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Persist column visibility and ordering across requests and different parts tables.
This commit is contained in:
parent
4020aab049
commit
82a6ab1d4a
3 changed files with 33 additions and 43 deletions
|
@ -23,9 +23,33 @@ export default class extends Controller {
|
||||||
|
|
||||||
static targets = ['dt'];
|
static targets = ['dt'];
|
||||||
|
|
||||||
|
static values = {
|
||||||
|
stateSaveTag: String
|
||||||
|
};
|
||||||
|
|
||||||
/** The datatable instance associated with this controller instance */
|
/** The datatable instance associated with this controller instance */
|
||||||
_dt;
|
_dt;
|
||||||
|
|
||||||
|
getStateSaveKey() {
|
||||||
|
let key = 'dt_state_'
|
||||||
|
|
||||||
|
if(this.stateSaveTagValue) { //If a tag is provided, use it to store the state
|
||||||
|
key += this.stateSaveTagValue;
|
||||||
|
} else { //Otherwise generate one from the current url
|
||||||
|
key += window.location.pathname;
|
||||||
|
}
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
stateSaveCallback(settings, data) {
|
||||||
|
localStorage.setItem( this.getStateSaveKey(), JSON.stringify(data) );
|
||||||
|
}
|
||||||
|
|
||||||
|
stateLoadCallback(settings) {
|
||||||
|
return JSON.parse( localStorage.getItem(this.getStateSaveKey()) );
|
||||||
|
}
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
//$($.fn.DataTable.tables()).DataTable().fixedHeader.disable();
|
//$($.fn.DataTable.tables()).DataTable().fixedHeader.disable();
|
||||||
//$($.fn.DataTable.tables()).DataTable().destroy();
|
//$($.fn.DataTable.tables()).DataTable().destroy();
|
||||||
|
@ -55,6 +79,9 @@ export default class extends Controller {
|
||||||
}],
|
}],
|
||||||
select: this.isSelectable(),
|
select: this.isSelectable(),
|
||||||
rowCallback: this._rowCallback.bind(this),
|
rowCallback: this._rowCallback.bind(this),
|
||||||
|
stateSave: true,
|
||||||
|
stateSaveCallback: this.stateSaveCallback.bind(this),
|
||||||
|
stateLoadCallback: this.stateLoadCallback.bind(this),
|
||||||
})
|
})
|
||||||
//Register error handler
|
//Register error handler
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
@ -78,46 +105,6 @@ export default class extends Controller {
|
||||||
promise.then(this._afterLoaded.bind(this));
|
promise.then(this._afterLoaded.bind(this));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Register links.
|
|
||||||
/*promise.then(function() {
|
|
||||||
|
|
||||||
//Set the correct title in the table.
|
|
||||||
let title = $('#part-card-header-src');
|
|
||||||
$('#part-card-header').html(title.html());
|
|
||||||
$(document).trigger('ajaxUI:dt_loaded');
|
|
||||||
|
|
||||||
|
|
||||||
if($table.data('part_table')) {
|
|
||||||
//@ts-ignore
|
|
||||||
$('#dt').on( 'select.dt deselect.dt', function ( e, dt, items ) {
|
|
||||||
let selected_elements = dt.rows({selected: true});
|
|
||||||
let count = selected_elements.count();
|
|
||||||
|
|
||||||
if(count > 0) {
|
|
||||||
$('#select_panel').removeClass('d-none');
|
|
||||||
} else {
|
|
||||||
$('#select_panel').addClass('d-none');
|
|
||||||
}
|
|
||||||
|
|
||||||
$('#select_count').text(count);
|
|
||||||
|
|
||||||
let selected_ids_string = selected_elements.data().map(function(value, index) {
|
|
||||||
return value['id']; }
|
|
||||||
).join(",");
|
|
||||||
|
|
||||||
$('#select_ids').val(selected_ids_string);
|
|
||||||
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
//Attach event listener to update links after new page selection:
|
|
||||||
$('#dt').on('draw.dt column-visibility.dt', function() {
|
|
||||||
//ajaxUI.registerLinks();
|
|
||||||
$(document).trigger('ajaxUI:dt_loaded');
|
|
||||||
});
|
|
||||||
});*/
|
|
||||||
|
|
||||||
console.debug('Datatables inited.');
|
console.debug('Datatables inited.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,9 @@ datatables:
|
||||||
|
|
||||||
language_from_cdn: false
|
language_from_cdn: false
|
||||||
|
|
||||||
|
# Set to none, as we override the bundle mechanism with our own custom one
|
||||||
|
persist_state: none
|
||||||
|
|
||||||
# Set options, as documented at https://datatables.net/reference/option/
|
# Set options, as documented at https://datatables.net/reference/option/
|
||||||
options:
|
options:
|
||||||
lengthMenu : [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]]
|
lengthMenu : [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]]
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
{% macro datatable(datatable, controller = 'elements/datatables/datatables') %}
|
{% macro datatable(datatable, controller = 'elements/datatables/datatables', state_save_tag = null) %}
|
||||||
<div {{ stimulus_controller(controller) }} data-dt-settings='{{ datatable_settings(datatable)|escape('html_attr') }}' data-dt-url="{{ app.request.requestUri }}">
|
<div {{ stimulus_controller(controller) }} data-dt-settings='{{ datatable_settings(datatable)|escape('html_attr') }}' data-dt-url="{{ app.request.requestUri }}">
|
||||||
<div {{ stimulus_target(controller, 'dt') }}>
|
<div {{ stimulus_target(controller, 'dt') }}>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
|
@ -17,9 +17,9 @@
|
||||||
{{ _self.datatable(dt, 'elements/datatables/log') }}
|
{{ _self.datatable(dt, 'elements/datatables/log') }}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% macro partsDatatableWithForm(datatable) %}
|
{% macro partsDatatableWithForm(datatable, state_save_tag = 'parts') %}
|
||||||
<form method="post" action="{{ path("table_action") }}"
|
<form method="post" action="{{ path("table_action") }}"
|
||||||
{{ stimulus_controller('elements/datatables/parts') }} data-dt-settings='{{ datatable_settings(datatable)|escape('html_attr') }}' data-dt-url="{{ app.request.requestUri }}"
|
{{ stimulus_controller('elements/datatables/parts', {"stateSaveTag": state_save_tag}) }} data-dt-settings='{{ datatable_settings(datatable)|escape('html_attr') }}' data-dt-url="{{ app.request.requestUri }}"
|
||||||
{{ stimulus_action('elements/datatables/parts', 'confirmDeletionAtSubmit') }} data-delete-title="{% trans %}part_list.action.delete-title{% endtrans %}"
|
{{ stimulus_action('elements/datatables/parts', 'confirmDeletionAtSubmit') }} data-delete-title="{% trans %}part_list.action.delete-title{% endtrans %}"
|
||||||
data-delete-message="{% trans %}part_list.action.delete-message{% endtrans %}">
|
data-delete-message="{% trans %}part_list.action.delete-message{% endtrans %}">
|
||||||
<input type="hidden" name="_token" value="{{ csrf_token('table_action') }}">
|
<input type="hidden" name="_token" value="{{ csrf_token('table_action') }}">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue