Skip to content
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

Fix column filter when savedstate is activated #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ajax_datatable/static/ajax_datatable/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,24 @@ window.AjaxDatatableViewUtils = (function() {

if (data.show_column_filters) {

var savedstate = table.api().state.loaded();
var filter_row = '<tr class="datatable-column-filter-row">';
$.each(data.columns, function(index, item) {
if (item.visible) {
if (item.searchable) {
var html = '';
var initial_search_value = item.initialSearchValue ? item.initialSearchValue : ''
if (!initial_search_value && savedstate) {
var saved_search = savedstate.columns[index].search.search
initial_search_value = saved_search ? saved_search : initial_search_value
}
if ('choices' in item && item.choices) {

// See: https://www.datatables.net/examples/api/multi_filter_select.html
var select = $('<select data-index="' + index.toString() + '"><option value=""></option></select>');
$(item.choices).each(function(index, choice) {
var option = $("<option>").attr('value', choice[0]).text(choice[1]);
if (choice[0] === item.initialSearchValue) {
if (choice[0] === initial_search_value) {
option.attr('selected', 'selected');
}
select.append(option);
Expand All @@ -151,7 +157,7 @@ window.AjaxDatatableViewUtils = (function() {
.attr('type', 'text')
.attr('data-index', index)
.attr('placeholder', '...')
.attr('value', item.initialSearchValue ? item.initialSearchValue : '')
.attr('value', initial_search_value)
html = $('<div>').append(input).html();
}
if (item.className) {
Expand Down