Display buttons in columns at the end of a row depending on another value in that row.

Display buttons in columns at the end of a row depending on another value in that row.

GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

I am trying to display buttons in columns at the end of a row depending on another value in that row. I have found a similar question in the forum and have modified that answer to give me:

             columnDefs: [{
                // puts a button in the last column
                targets: [-1], render: function (a, b, data, d) {
                    alert("data.accId: " + data.accId + " sessionStorage.getItem('ssAccountID'): " + sessionStorage.getItem('ssAccountID'));
                    if (data.accId === sessionStorage.getItem('ssAccountID')) {
                        return "<button type='button' id='deleteCampSite'>Delete</button>";
                    }
                    return "";
                },

                // puts a button in the second last column
                targets: [-2], render: function (a, b, data, d) {
                    if (data.accId === sessionStorage.getItem('ssAccountID')) {
                        return "<button type='button' id='updateCampSite'>Update</button>";
                    }
                    return "";
                }
            }],

However, that gives me:

How can I get both buttons to show (Delete in last column and Update in second last column) please?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Is this using Editor?

    If not, it should work the same for both columns. We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    Hi Colin,

    This is the test case:

    http://live.datatables.net/munalocu/1/

    How should it be coded please?

    Kind regards,

    Glyn

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921
    edited March 2020 Answer ✓

    There were a lot of errors in the example. Made the following changes:

    1. Added the last two columns in each row
    2. Changed render: function (a, b, data, d) { to render: function (data, type, row) {. Did this just to follow the Datatables standard parameter naming.
    3. Changed if (table.age === 61) { to if (row[3] == 61) {. Did this because table is not the variable to use. To access the row data use the row parameter. This example is using array based data not object so row[3] is used to access the age column. Used == instead of === to allow for comparing strings to integers.

    Here is the updated example:
    http://live.datatables.net/munalocu/2/edit

    Kevin

  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    Thank you Kevin. In my particular case it looks like (in case anyone else needs to know):

                columnDefs: [
                    //puts a button in the last column
                    {targets: [-1], 
                        render: function (data, type, row) {
                        if (data.accId === sessionStorage.getItem('ssAccountID')) {
                            return "<button type='button' id='deleteCampSite'>Delete</button>";
                            }
                            return "";
                        }
                    },
    
                    // puts a button in the second last column
                    {targets: [-2], 
                        render: function (data, type, row) {
                        if (data.accId === sessionStorage.getItem('ssAccountID')) {
                            return "<button type='button' id='updateCampSite'>Update</button>";
                            }
                            return "";
                        }
                    },
                ],
    
This discussion has been closed.