Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing data in another column in Datatables

I'm using Datatables and I'm trying to access data in another column. It's unclear to me if I should be using columns.data or if I should be taking another approach by using getting the column index instead?

Objective

  • In the second render function, I want the first makeSlug(data) to be referencing the "data": "district" and the second one to remain the same and reference "data": "school"

scripts.js

"columns": [
{ "data": "district",
    "render": function (data, type, row, meta) {
        return '<a href="/schools/' + makeSlug(data) + '">' + data + '</a>';
    }
},
{ "data": "school",
    "render": function (data, type, row, meta) {
        return '<a href="/schools/' + makeSlug(data) + '/' + makeSlug(data) + '">' + data + '</a>';
    }
},
{ "data": "subject"},
{ "data": "rate"},
{ "data": "test_takers"}
],
like image 224
Andrew Nguyen Avatar asked May 26 '17 17:05

Andrew Nguyen


1 Answers

Third argument row is an array containing full data set for the row. Use row['district'] to access district property.

For example:

{ 
   "data": "school",
   "render": function (data, type, row, meta) {
        return '<a href="/schools/' + makeSlug(row['district']) + '/' + makeSlug(data) + '">' + data + '</a>';
    }
}
like image 142
Gyrocode.com Avatar answered Oct 17 '22 21:10

Gyrocode.com