0

Hi I have the following structure:

    $(document).ready(function () {
    $('#employee_data').dataTable({
      'ajax': {
        "processing": true,
        "serverSide": true,
        "url": 'get-data.php',
        "dataType": 'json',
        dataSrc: '',
      },
      "columns": [
        {"data": "num"},
        {"data": "date"},
        {"data": "value"},
        {"data": "max"},
        {"data": "min"},
        {"data": "name"}
      ],
    });
  });

How can I write conditions in the "value" column?

Example:

if (data["Num"] === '1' && (data['Value'] >= 120 || data['Value'] <= 20 || isNaN(data['Value']))) {
              return ' class="class1" ';
dearsina
  • 4,774
  • 2
  • 28
  • 34
Jess163
  • 38
  • 4

1 Answers1

0

You Can Write That Condition Like This, Here is My Code Use The Render for return the data.

{ "data": "connected" ,
    render: function ( data, type, row ) {
    let html = ``;
    if(data == '1'){
        html = `<button type="button" class="btn btn-block btn-success btn-sm">Connected</button>`;
    }
    else {
        html = `<button type="button" class="btn btn-block btn-danger btn-sm">Disconnected</button>`;
    }
    return html;
} }
  • Can I take data from another column in the condition? – Jess163 Aug 12 '21 at 05:58
  • Small point: I recommend you use `===` instead of `==` as a general habit - [here is why](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons). – andrewJames Aug 12 '21 at 13:34
  • @Jess163 - note the parameters for the render function: `( data, type, row )`. The `row` parameter gives you access to every field/cell in the current row. You can see that data for yourself by logging `row` to the browser's console: `console.log(row);` – andrewJames Aug 12 '21 at 13:37