How to return specific rows with custom button?

How to return specific rows with custom button?

ArielSAdamsNASAArielSAdamsNASA 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

  • kthorngrenkthorngren Posts: 21,362Questions: 26Answers: 4,956

    Use the rows() row-selector as a function to return only those rows that match your criteria.

    Kevin

  • ArielSAdamsNASAArielSAdamsNASA Posts: 105Questions: 15Answers: 0

    @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.

  • kthorngrenkthorngren Posts: 21,362Questions: 26Answers: 4,956

    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

  • ArielSAdamsNASAArielSAdamsNASA Posts: 105Questions: 15Answers: 0
    edited March 2022

    @kthorngren

    Its possible but how will you know which rows the user liked?

    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?

    $(document).ready(function() {
        $('#example').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                {
                    text: 'My button',
                    action: function ( e, dt, node, config ) {
                        alert( 'Button activated' );
                       // How to return rows that matches New York
                        return table.rows() =  "New York";
                    }
                }
            ]
        } );
    } );
    
  • kthorngrenkthorngren Posts: 21,362Questions: 26Answers: 4,956

    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.

    after clicking a button

    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 the success function use row().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

  • ArielSAdamsNASAArielSAdamsNASA Posts: 105Questions: 15Answers: 0

    @kthorngren

    https://jsfiddle.net/8z73xynb/3/

    For my button, I want to show only the rows that have New York in it.

  • kthorngrenkthorngren Posts: 21,362Questions: 26Answers: 4,956

    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

  • ArielSAdamsNASAArielSAdamsNASA Posts: 105Questions: 15Answers: 0

    @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();

  • kthorngrenkthorngren Posts: 21,362Questions: 26Answers: 4,956

    Use search() or column().search() to search the table and filter the row display.

    Kevin

  • ArielSAdamsNASAArielSAdamsNASA Posts: 105Questions: 15Answers: 0

    @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/

  • kthorngrenkthorngren Posts: 21,362Questions: 26Answers: 4,956

    I'm not sure what object you are trying to use. This searches for New York in column 2.
    https://jsfiddle.net/brcvgjt4/

    Kevin

  • ArielSAdamsNASAArielSAdamsNASA Posts: 105Questions: 15Answers: 0

    @kthorngren

    Ah, I understand now. I wonder if this will work with a django statement like user.like.objects.all().

  • kthorngrenkthorngren Posts: 21,362Questions: 26Answers: 4,956
    edited March 2022 Answer ✓

    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

  • ArielSAdamsNASAArielSAdamsNASA Posts: 105Questions: 15Answers: 0

    @kthorngren Thank you for your help. How would one toggle the My button?

    https://jsfiddle.net/brcvgjt4/

  • kthorngrenkthorngren Posts: 21,362Questions: 26Answers: 4,956
    Answer ✓

    Do you mean to programmatically click the button? If yes then use button().trigger().

    Kevin

Sign In or Register to comment.