0

I successfully getting the data table populated if I use the sAjaxSource = "filepath/file"

However if I pass the PHP json data and try to collect it its not populating the data in the datatable,

How can I directly use the json data without calling the data from objects.txt file. ?

json_data  = { "aaData": [
    {
        "Rendering_engine": "Trident",
        "Browser": "Internet Explorer 4.0",
        "Platforms": "Win 95+",
        "Engine_Version": "4",
        "CSS_Grade": " b"

    },
    {
        "Rendering_engine": "rident",
        "Browser": "Internet Explorer 5.0",
        "Platforms": "Win 95+",
        "Engine_Version": "5",
        "CSS_Grade": " a"

    }
] }

php code

$r = //sql query    
$ma['aaData'] =$r;
                        $data = json_encode($ma);
                        var_dump($data);

script

    <script>
    $(document).ready(function() {

        var json_data_chk =  <?php echo($data); ?>;
        var json_data = JSON.stringify(json_data_chk);
        alert(json_data);
// code     
var sImageUrl = "img/";


        var oTable = $('#hidden-table-info').dataTable( {
            "bProcessing": true,
           "sAjaxSource": "objects.txt",
            "bServerSide": false,
             "aoColumns": [
                {
                   "mDataProp": null,
                   "sClass": "control center",
                   "sDefaultContent": '<img src="'+sImageUrl+'details_open.png'+'">'
                },
                { "mDataProp": "Rendering_engine" },
                { "mDataProp": "Browser" },
                { "mDataProp": "Platforms" },

            ],
// code

    </script>

EDIT: json_data is the variable that is having the same json data as objects.txt but instead of calling the file i want to use the data from this variable

6
  • If the data is obtained with an ajax call then you should set bServerSide: true and give the url of the ajax request to sAjaxSource. ie sAjaxSource: $url Commented Sep 12, 2014 at 13:45
  • The data is within the variable and need to collect it... I am able to do it that way form the file. but want to get it directly from the variable instead of writing to some file Commented Sep 12, 2014 at 13:47
  • @Hamid json_data is the variable that is havind the same json data as objects.txt but instead of calling the file i want to use the data from this variable Commented Sep 12, 2014 at 13:49
  • In that case, you can always construct the table dom and then call $().dataTable(); on it. Commented Sep 12, 2014 at 13:52
  • 1
    Also, look at this: stackoverflow.com/q/9669812/2139684 Commented Sep 12, 2014 at 13:53

1 Answer 1

0

you can use this instruction

$('#example').dataTable( {
  "ajax": "json_data"
} );

or this one :

 "ajax": {
    "url": "data.json",
    "type": "POST"
  }

Reference :https://datatables.net/reference/option/ajax

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

Comments

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.