DataTables Filter Cell Content not DB Value

Viewed 954

Using DataTables 1.10.19

I have a MySQL DB, the structure is as follows;

+------+-----------------+----------+----------+
| name | email           | status   | complete |    
+------+-----------------+----------+----------+
| Joe  | me@example.com  | 1        |1         |
+------+-----------------+----------+----------+
| Jim  | you@example.com | 1        |0         |
+------+-----------------+----------+----------+
| Sara | him@example.com | 0        |0         |
+------+-----------------+----------+----------+

I'm using this script to retrieve data from the db.

My datatable filter works as expected when searching for 0 and 1, but this filters both status and complete columns. I would like to search for the words active / inactive and complete / incomplete instead of 1 and 0.

I'm using the datatables columns.render option to render a custom output in these columns based on the row results.

My DataTable code is;

$('#example').dataTable({
    "ajaxSource": "results.php", // output below
    "columns": [{
        "data": "name"
    }, {
        "data": "email"
    }, {
        "data": "status",
        "render": function(data, type, row, meta) {
            // row[2] returns an int of 0 or 1 from the db. inactive/active
            if (row[2] == 0) {
                return `inactive`;
            }
            if (row[2] == 1) {
                return `active`;
            }
        }
    }, {
        "data": "complete",
        "render": function(data, type, row, meta) {
            // row[2] returns an int of 0 or 1 from the db. incomplete/complete
            if (row[3] == 0) {
                return `incomplete`;
            }
            if (row[3] == 1) {
                return `complete`;
            }
        }
    }, ]
});

The results.php file returns the following;

"data": [
    [
      "Joe",
      "me@example.com  ",
      "1",
      "1",
    ],
    [
      "Jim",
      "you@example.com  ",
      "1",
      "0",
    ],
    [
      "Sara",
      "him@example.com  ",
      "0",
      "0",
    ],
]

My front end HTML table looks like this;

+------+-----------------+----------+------------+
| name | email           | status   |complete    |
+------+-----------------+----------+------------+
| Joe  | me@example.com  | active   |complete    |
+------+-----------------+----------+------------+
| Jim  | you@example.com | active   |incomplete  | 
+------+-----------------+----------+------------+
| Sara | him@example.com | inactive |incomplete  |
+------+-----------------+----------+------------+

Currently the filter seems to filter the db int value and not the cell text.

How can I search for the words instead of 0 and 1.

1 Answers

The below seems to work just fine. Typing "Inactive" shows only Sara as a result. What are you doing differently?

Edit: I have updated the snippet to match your latest code. It seems you are passing down an array of arrays for your data, so I am surprised that your table is even initializing, as the data options are not valid in that case (how can data select the "name" attribute if it doesn't exist in your result set? It should be the array index). After updating the data options to the array index, the table searches your rendered table correctly. Searching "Incomplete" returns Jim/ Sara, while searching "Inactive" returns Sara.

$(document).ready(function() {
  var data = getDummyData();
  $('#example').dataTable({
    "data": data,
    "columns": [{
        "data": 0
      },
      {
        "data": 1
      },
      {
        "data": 2,
        "render": function(data, type, row, meta) {
          // row[2] returns an int of 0 or 1 from the db. inactive/active
          if (data == 0) {
            return `inactive`;
          }
          if (data == 1) {
            return `active`;
          }
        }
      },
      {
        "data": 3,
        "render": function(data, type, row, meta) {
          // row[2] returns an int of 0 or 1 from the db. incomplete/complete
          if (data == 0) {
            return `incomplete`;
          }
          if (data == 1) {
            return `complete`;
          }
        }
      },
    ]
  });
});

function getDummyData() {
  return [
    [
      "Joe",
      "me@example.com  ",
      "1",
      "1",
    ],
    [
      "Jim",
      "you@example.com  ",
      "1",
      "0",
    ],
    [
      "Sara",
      "him@example.com  ",
      "0",
      "0",
    ],
  ]
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

<table id="example">
  <thead>
    <tr>
      <th>name</th>
      <th>email</th>
      <th>status</th>
      <th>complete</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

Related