Find the NEXT FILTERED result when using keydown function
Find the NEXT FILTERED result when using keydown function
The setup is standard - a datatable. All rows are clickable When I click on a row, I use th following function to get the data of the "selected" row
[code]
ClickCard = function() {
$('body').on('click',"tr",function() {
chosen = formatTable.fnGetPosition( $(this).closest('tr')[0] );
$('tr').removeClass("selected");
$(this).addClass("selected")
var card = formatTable.fnGetData( chosen )
SelectCard(card);
})
}
[/code]
I use the following code to select the previous/next row and get the data from it when using arrow up/down keys.
[code]
MoveArrows = function () {
$(document).keydown(function(e){
if (e.keyCode == 40)
{
chosen++;
$('tr').removeClass('selected');
$('#selectedSets tbody').find('tr').eq(chosen).addClass('selected');
SelectCard(formatTable.fnGetData( chosen ))
return false;
}
if (e.keyCode == 38) {
chosen--;
$('tr').removeClass('selected');
$('#selectedSets tbody').find('tr').eq(chosen).addClass('selected');
SelectCard(formatTable.fnGetData( chosen ))
return false;
}
})
}
[/code]
This code works perfectly well when I have not applied any filters the datatable, but when filters are applied the function does not work correctly. The problem is that datatables keeps the original row numbers, while jquery renumber rows.
Example. I have clicked tr 1
In nonfiltered table
1
2
3
on arrow down MoveArrows will return the data of tr 2
Lets say after filter the situation is
1
3
Now MoveArrows will still return data for tr 2, even though it is filtered out, but I want it to return data for tr 3. Something like findfnexed filtered out??
[code]
ClickCard = function() {
$('body').on('click',"tr",function() {
chosen = formatTable.fnGetPosition( $(this).closest('tr')[0] );
$('tr').removeClass("selected");
$(this).addClass("selected")
var card = formatTable.fnGetData( chosen )
SelectCard(card);
})
}
[/code]
I use the following code to select the previous/next row and get the data from it when using arrow up/down keys.
[code]
MoveArrows = function () {
$(document).keydown(function(e){
if (e.keyCode == 40)
{
chosen++;
$('tr').removeClass('selected');
$('#selectedSets tbody').find('tr').eq(chosen).addClass('selected');
SelectCard(formatTable.fnGetData( chosen ))
return false;
}
if (e.keyCode == 38) {
chosen--;
$('tr').removeClass('selected');
$('#selectedSets tbody').find('tr').eq(chosen).addClass('selected');
SelectCard(formatTable.fnGetData( chosen ))
return false;
}
})
}
[/code]
This code works perfectly well when I have not applied any filters the datatable, but when filters are applied the function does not work correctly. The problem is that datatables keeps the original row numbers, while jquery renumber rows.
Example. I have clicked tr 1
In nonfiltered table
1
2
3
on arrow down MoveArrows will return the data of tr 2
Lets say after filter the situation is
1
3
Now MoveArrows will still return data for tr 2, even though it is filtered out, but I want it to return data for tr 3. Something like findfnexed filtered out??
This discussion has been closed.
Replies
[code]
MoveArrows = function () {
$(document).keydown(function(e){
if (e.keyCode == 40)
{
chosen++;
// console.log($(".selected").closest("table").attr("id"))
$('tr').removeClass('selected');
$('#selectedSets tbody').find('tr').eq(chosen).addClass('selected');
SelectCard(formatTable.fnGetData( chosen ))
return false;
}
if (e.keyCode == 38) {
chosen--;
$('tr').removeClass('selected');
// var data = formatTable._('tr:next', {"filter": "applied"});
// console.log(data)
$('#selectedSets tbody').find('tr').eq(chosen).addClass('selected');
SelectCard(formatTable.fnGetData( chosen ))
return false;
}
})
}
[/code]