1

I'm making one jQuery DataTable, in that I need to make some changes before rendering the data table.

Sample data:

[{
  "time": "2018-07-18T15:16:10.557Z",
  "data": "AQAAH"
}, {
  "time": "2018-07-18T15:13:10.557Z",
  "data": "AQAAH"
}]

I want to change the value of the data column before rendering onto the DataTable, like data:

AQAAH to 0100001c

I want to convert the data from base64 to hex, then I want to render in the DataTable.

Code:

function getddata() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      var Readings = JSON.parse(xmlhttp.responseText);

      $(document).ready(function() {
        $('#example').DataTable({
          data: Readings,
          columns: [{
            "data": "time"
          }, {
            "data": "data"
          }]
        });
      });
      //data table code -close
    }
  };
  xmlhttp.open("GET", "url", true);
  xmlhttp.setRequestHeader("Content-type", "Application/json");
  xmlhttp.send();
}
Rick
  • 4,030
  • 9
  • 24
  • 35
krishna mohan
  • 111
  • 2
  • 13

1 Answers1

1

You can use column.render option in DataTables. Check the documentation for more details.

$('#example').DataTable({
  data: Readings,
  columns: [{
    "data": "time"
  }, {
    "data": "data"
  }],
  columnDefs: [{
      "render": function ( data, type, row ) {
        // here you can convert data from base64 to hex and return it
        return data
      },
      "targets": 1
   }]
});

I believe this question will help you in the conversion.

Lasithe
  • 1,916
  • 1
  • 15
  • 20