Add extra column and set it's values from another column.render

Add extra column and set it's values from another column.render

Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0
edited April 2020 in DataTables

Hi!

I'm using the ajax option to fetch my data. My json objects have 8 nodes, but I've got 9 columns in my table because I'm using columns.render for an extra column. Now I want to add a 10th column, which values I want to set from the column I'm using columns.render on. How do I do that?

(I know I can add one more columns.render column, but I don't know how to reference it since it does not have an identifier.)

Thanks!

$('#myTable').DataTable( {
        "ajax": "https://xxxxxxxxxxxxxx.json",
        "pageLength": 50,
        "columns": [
            { "data": "FirstName" },
            { "data": "LastName" },
            { "data": "Mail" },
            { "data": "Role" },
            { "data": "Registered" },
            { "data": "Gym" },
            { "data": "Country" },
            { "data": "Phone" },
            { 
                "data": "Phone",
                "render": function ( data, type, row, meta ) {

                    // HERE I WANT TO SET THE VALUE OF NEXT COLUMN!!!

                }
            }

        ]
    } );

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,444Questions: 26Answers: 4,974
    edited April 2020

    The row parameter, as noted in the columns.render docs, contain the data for the row. You could do something like this:

            "columns": [
                { "data": "FirstName" },
                { "data": "LastName" },
                { "data": "Mail" },
                { "data": "Role" },
                { "data": "Registered" },
                { "data": "Gym" },
                { "data": "Country" },
                { "data": "Phone" },
                {
                    "data": "Phone",
                    "render": function ( data, type, row, meta ) {
     
                        // HERE I WANT TO SET THE VALUE OF NEXT COLUMN!!!
     
                    }
                },
                {
                    "data": null,
                    "render": function ( data, type, row, meta ) {
     
                        return row.Firstname + ' ' + row.Lastname;  // Column will display firstname lastname
     
                    }
                } 
            ]
    

    Does this answer your question?

    Kevin

  • Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0
    edited April 2020

    @kthorngren Thanks for your answer Kevin! I don't think you answered my question though. I know how to get data from other columns, but in this case I need to set data in a new column. My problem is that I don't know how to reference this new column. See the javascript comment in my code.

  • Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0

    I just found out about the "columns.name" option so now I know how to reference the new column. But how do I set the value of it?

  • kthorngrenkthorngren Posts: 21,444Questions: 26Answers: 4,974

    Are you trying to take a rendered value and use it in another column? There isn't a way to do that in columns.render. See if rowCallback or drawCallback will do what you want.

    Kevin

  • Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0

    @kthorngren Where my JavaScript comment is in my question, I'm doing some calculations. Depending on the result of the calculations, I want to set the value of the next column.

  • Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0

    @kthorngren Where my JavaScript comment is, I'm using row.Country to get the data of the column called Country. Instead of getting_data this way, how can i _set it?

  • kthorngrenkthorngren Posts: 21,444Questions: 26Answers: 4,974
    Answer ✓

    Like I said there isn't a way to do this with columns.render. You can't set the value of another column nor can you retrieve rendered data from another column. Maybe you can also do the calculation in the "next" column to determine the value to set.

    Kevin

  • Rawland_HustleRawland_Hustle Posts: 94Questions: 16Answers: 0

    @kthorngren Okay, thank you!

This discussion has been closed.