0

I have data which looks like this.

{
  "data": [
  { 
   "c1":  "datapt00",
   "size": 40
  },
  { 
   "c1":  "datapt001",
   "size": 80
  }
  ]
}

In HTML I am doing,

$(document).ready(function) { 
$('#example').DataTAble ( { 
"ajax": {
"url": "/data",
}} ); });

Is ther ean easy way to manipulate my data so its Datatable compliant?

1 Answer 1

1

This should do it:

var jsonData = {
  "data": [
  { 
   "c1":  "datapt00",
   "size": 40
  },
  { 
   "c1":  "datapt001",
   "size": 80
  }
  ]
};
$('#example').DataTable({
    "ajax": {
        "type": 'POST',
        "dataType": 'json',
        "url": '/echo/json/',
        "data": {
            "json": JSON.stringify(jsonData)
        },
        "dataSrc": "data"
    },
    "columns": [{
        "data": "c1"
    }, {
        "data": "size"
    }]
});

Working JSFiddle here: https://jsfiddle.net/annoyingmouse/70d01vo0/

Sign up to request clarification or add additional context in comments.

2 Comments

what happens if I am not using jsonData. I am getting my data from "url"
Ah, bless you! There's no way of using AJAX on JSFiddle so this is the mechanism for getting json on JSFiddle. I think you'll probably be wanting to use something along the lines of "ajax": {"url": "/data", "dataSrc": "data"}. Hope that that makes sense and helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.