How to get other columns json variables?

How to get other columns json variables?

Chris230291Chris230291 Posts: 34Questions: 10Answers: 1
edited February 2023 in Free community support

I am trying to figure out how to build my tables using ajax and json as I heard it performs better than building the entire table using jinja2 and then letting datatables redraw it.

json

{
  "data": [
    {
      "data": "enabled",
      "data-2": "2",
      "data-3": "3",
      "data-4": "4",
      "data-5": "5",
      "data-6": "6"
    },
    ...
  ]
}

javascript

                {
                    "data": "enabled",
                    "data-2": "data-2",
                    "data-3": "data-3",
                    "render": function (data, data-2, data-3) {
                        let r ='<input type="checkbox" data-2="' + data-2 + '" data-3="' + data-3+ '"'
                        if (data == "enabled") {
                            r = r +' checked';
                        }
                        r = r + '>'
                        return r
                    }
                },

I would like to reference data-2 and data-3 in the data column as well as some other columns.
Is this possible?
Or perhaps there is a better way to set "shared" data between all columns on that row?

Answers

  • Chris230291Chris230291 Posts: 34Questions: 10Answers: 1

    I think I have figured it out after finding a previous answer by @colin

    {
        "data": "enabled",
        "render": function (data, type, row, meta) {
            ...
            return r
        }
    },
    

    Then using row.data-2 I can use the data-2 value from the json in the data column.

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Yes. See the columns.render docs for details. Also see this example.

    Kevin

Sign In or Register to comment.