How to return specific rows with custom button?
How to return specific rows with custom button?
ArielSAdamsNASA
Posts: 105Questions: 15Answers: 0
Description of problem:
I want to create a custom button to return rows that the user liked. How do you return specific rows?
$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
{
text: 'My button',
action: function ( e, dt, node, config ) {
alert( 'Button activated' );
return user.like.objects.all();
}
}
]
} );
} );
This question has accepted answers - jump to:
Answers
Use the
rows()
row-selector
as a function to return only those rows that match your criteria.Kevin
@kthorngren
Is it possible to return rows that meet a certain criteria, but that data is not in the table? I have a column named Likes, that returns the number of likes that row has, but it does not contain who liked the row. I want to return rows that the logged in user has liked.
Its possible but how will you know which rows the user liked?
If you have a way to map the users to the liked rows then you can just get those rows. If you need help with this please provide a simple test case with an example of your row data and how the users are mapped to the liked rows.
Kevin
@kthorngren
I could use a django statement such as user.like.objects.all()
I think I can show which rows the user liked after receiving a demo on how to update the table. Could you provide a demo on how to update a table after clicking a button that returns specific rows based on a parameter?
There are lots of options depending on what exactly you are doing.
I'm guessing this is not the custom button above? You can create a click event for the button. Use the technique in this example to get the row, assuming the button is in the row. In the click event use jQuery ajax to send the parameters to the Django script using the
data
object. In thesuccess
function userow().data()
to update the row data.If you need help with this please provide a test case showing what you are trying to do so we can provide more specific help. You can get an ajax loaded Datatables starter test case here.
Kevin
@kthorngren
https://jsfiddle.net/8z73xynb/3/
For my button, I want to show only the rows that have New York in it.
As I mentioned use the
row-selector
as a function to get the rows that meet your condition. Like this:https://jsfiddle.net/heo8tsz9/
Kevin
@kthorngren
How would you update the table to only show New York? The fiddle provided does not do that and I tried adding table.draw();
Use
search()
orcolumn().search()
to search the table and filter the row display.Kevin
@kthorngren
Not sure how to place an object in the search, it just populates the search with the text "[object Object]"
https://jsfiddle.net/heo8tsz9/
I'm not sure what object you are trying to use. This searches for New York in column 2.
https://jsfiddle.net/brcvgjt4/
Kevin
@kthorngren
Ah, I understand now. I wonder if this will work with a django statement like user.like.objects.all().
If you want to search for data that is supplied by Django you will need to use an ajax request as described above. Or supply the needed data in the row data so its accessible anytime
Kevin
@kthorngren Thank you for your help. How would one toggle the My button?
https://jsfiddle.net/brcvgjt4/
Do you mean to programmatically click the button? If yes then use
button().trigger()
.Kevin