afnFiltering.push() filtering issue
afnFiltering.push() filtering issue
pixelgeek
Posts: 18Questions: 2Answers: 0
I am using version 1.9.4 and attempting to do some custom sorting on a table. I have two selects that I am getting options from to filter a table based on some data attributes in the rows.
I have two issues.
1) When I check one or more options and then sort using the column heading sort tools the table resets to show all records. Do I need to call the .push() function again after having the table sort?
2) The custom filters only work with a single option. I am storing the data from the checked inputs into two global variables
[code]
selectSchoolFilters = $.map($('#for_school input:checked'), function(e) { return e.value })
selectTypeFilters = $.map($('#for_type input:checked'), function(e) { return e.value })
listTable.dataTable().fnDraw(true)
[/code]
I then test to see if the rows data-type attribute is one of the items in the array (excuse the newbie code. I write for legibility :-)
[code]
$.fn.dataTableExt.afnFiltering.push(
function(oSettings, aData, iDataIndex) {
var tr = $(oSettings.aoData[iDataIndex].nTr)
if(!tr.closest('table').is('#card_list_table')) {
return true
}
var typePassed = true
// don't do anything if the type filters array is empty
if (selectTypeFilters.length) {
var listIndex = $.inArray(tr.data('type'), selectTypeFilters)
if (listIndex == -1) {
typePassed = false
}
}
var schoolPassed = true
if (selectSchoolFilters.length) {
// nothing here yet
}
return typePassed && schoolPassed
}
)
[/code]
When I debug this or use the console to output the variable in play it appears to work but anything other than a single value in the selectTypeFilters array will cause the table to display data that is not one of the options selected.
I've tried several different options and nothing seems to work.
Any comments or suggestions would be appreciated.
You can see an older version of this with the same issues at
http://wp.arcanewonders.com/mw/mw.php
Select a "Mage" to show the table in question
I have two issues.
1) When I check one or more options and then sort using the column heading sort tools the table resets to show all records. Do I need to call the .push() function again after having the table sort?
2) The custom filters only work with a single option. I am storing the data from the checked inputs into two global variables
[code]
selectSchoolFilters = $.map($('#for_school input:checked'), function(e) { return e.value })
selectTypeFilters = $.map($('#for_type input:checked'), function(e) { return e.value })
listTable.dataTable().fnDraw(true)
[/code]
I then test to see if the rows data-type attribute is one of the items in the array (excuse the newbie code. I write for legibility :-)
[code]
$.fn.dataTableExt.afnFiltering.push(
function(oSettings, aData, iDataIndex) {
var tr = $(oSettings.aoData[iDataIndex].nTr)
if(!tr.closest('table').is('#card_list_table')) {
return true
}
var typePassed = true
// don't do anything if the type filters array is empty
if (selectTypeFilters.length) {
var listIndex = $.inArray(tr.data('type'), selectTypeFilters)
if (listIndex == -1) {
typePassed = false
}
}
var schoolPassed = true
if (selectSchoolFilters.length) {
// nothing here yet
}
return typePassed && schoolPassed
}
)
[/code]
When I debug this or use the console to output the variable in play it appears to work but anything other than a single value in the selectTypeFilters array will cause the table to display data that is not one of the options selected.
I've tried several different options and nothing seems to work.
Any comments or suggestions would be appreciated.
You can see an older version of this with the same issues at
http://wp.arcanewonders.com/mw/mw.php
Select a "Mage" to show the table in question
This discussion has been closed.
Replies
One thing at the moment:
> $('#for_school input, #for_type, #packs_container select').on('change', function () {
The `#for_type` I think needs an `input` on the selector.
I actually don't see, beyond that, why it isn't working... It rather looks like it should be!
Could you change:
> if (!tr.closest('table').is('#card_list_table')) {
To be:
[code]
if ( oSettings.nTable.id !== 'card_list_table' ) {
return true;
}
[/code]
That will be more efficient, but I don't think it will solve the issue directly.
One other thing - I don't think it will help either, but it might be worth a shot - could you try the DataTables 1.10 pre release from git? Looking at my example, it doesn't suffer from this click on sort and all rows reappear, so there is something odd going on, but we never know, perhaps 1.10 has changed something around... https://github.com/DataTables/DataTables/tree/1_10_wip/media/js
Allan
The code online is an older version that exhibits the same errors. The code I posted above is from a local version that while faster still shows the same errors.
I'll add the changes you suggested and also try out the 1.10 version. I won't be able to do that until this weekend though.
If the error still continues is there any information I can provide you to help investigate the problem?
Allan
Thanks
We are going to be doing another filtering solution instead of the custom filtering for the meantime.