Can't get datatables to work with json string

Can't get datatables to work with json string

ssuessssuess Posts: 2Questions: 0Answers: 0

For some reason, despite following the tutorials to the letter (at least I think I am), I can't get the following to work. It gives me the dreaded:

**DataTables warning: table id=actiontables - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4**

Here is my code, someone please tell me what I am doing wrong..

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

var myjsons =[{"one":"195","two":"thing","three":"something","four":"1481200571","five":"test string","six":"www.google.com"}];   
    
jQuery('#actiontables').DataTable( {
        data: myjsons,
        columns: [
            { myjsons: 'one' },
            { myjsons: 'two' },
            { myjsons: 'three' },
            { myjsons: 'four' },
            { myjsons: 'five' },
            { myjsons: 'six' }
        ]
    } );  

}); </script>
<table id="actiontables" class="display" width="100%"></table>

Replies

  • ssuessssuess Posts: 2Questions: 0Answers: 0

    omfg, I can't believe this. I just got it working by renaming my json "data" (from "myjsons")...what is the point of having a var if you can't name it what you want?? Hope this helps some other poor soul.

  • mackmackmackmack Posts: 15Questions: 1Answers: 2

    Just try this:

    <script>
       jQuery(document).ready(function(){
            var myjsons = [{"one":"195", "two":"thing", "three":"something", "four":"1481200571", "five":"test   string", "six":"www.google.com"}]; jQuery('#actiontables').DataTable( {
            "aaData": myjsons,
            "aoColumns": [
                { "mDataProp": 'one'},
                { "mDataProp": 'two' },
                { "mDataProp": 'three' },
                { "mDataProp": 'four' },
                { "mDataProp": 'five' },
                { "mDataProp": 'six' }
           ]
        } ); 
    
    }); </script>
    <table id="actiontables" class="display" width="100%"></table>
    
  • kthorngrenkthorngren Posts: 20,264Questions: 26Answers: 4,764
    edited January 2017

    Try changing:

            columns: [
                { myjsons: 'one' },
                { myjsons: 'two' },
                { myjsons: 'three' },
                { myjsons: 'four' },
                { myjsons: 'five' },
                { myjsons: 'six' }
            ]
    

    to:

            columns: [
                { data: 'one' },
                { data: 'two' },
                { data: 'three' },
                { data: 'four' },
                { data: 'five' },
                { data: 'six' }
            ]
    

    Checkout this page for details:
    https://datatables.net/manual/data/

    Kevin

This discussion has been closed.