Problem with JSON format - Help and explanations needed

Problem with JSON format - Help and explanations needed

phronphron Posts: 5Questions: 0Answers: 0
edited March 2013 in DataTables 1.9
Hello
I don't understand why my datas does not display, and I have the classical "Requested unknown parameter 0 from the datasource". What is wrong in my JSON format ?
I used DataTables debugger result is here : http://debug.datatables.net/eduleh

Any help would be appreciated.
If it can help I'm using Rails 3.2.11 with the datatables gem (https://github.com/rweng/jquery-datatables-rails) on windows8

Cheers

Replies

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    > What is wrong in my JSON format ?

    Nothing. However, you are returning an array of objects, so you need to tell DataTables which property from the objects to use in each column. For this you use mData . See: http://datatables.net/blog/Extended_data_source_options_with_DataTables

    Allan
  • phronphron Posts: 5Questions: 0Answers: 0
    I don't understand ....
    when I add 'aoColumns' to my initialisation (I put all the columns), it throws an error "oCol is undefined".
    When I run DataTables debugger it has only one column with index 0, where the column Title is what it should be, but the value is the last attribute value (created_by)
    You can see it at http://debug.datatables.net/ugujoj

    the CoffeeScript source is below if it can be of any help (maybe the problem is here, I'm not very familiar with CoffeeScript)

    [code]
    jQuery ->
    $("#users-grid").dataTable
    sDom: "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>"
    sPaginationType: 'bootstrap'
    # bJQueryUI: true
    iDisplayLength: 5
    aLengthMenu: [[5,10, 25, 50, 100, -1], [5,10, 25, 50, 100, "All"]]
    bProcessing: true
    bServerSide: true
    sAjaxSource: $("#users-grid").data('source')
    aoColumns: [
    mData: "id",
    mData: "email",
    mData: "password_digest",
    mData: "remember_token",
    mData: "password_reset_token",
    mData: "password_reset_sent_at",
    mData: "activation_token",
    mData: "active",
    mData: "blocked",
    mData: "created_at",
    mData: "updated_at",
    mData: "created_by"
    ]
    [/code]

    What am I missing / misunderstanding / doing bad ?
    Thanks for taking the time to help me
    Cheers
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    edited March 2013
    Edit - sorry didn't realise you were using CoffeeScript.

    What does the generated Javascript look like?
  • phronphron Posts: 5Questions: 0Answers: 0
    edited March 2013
    Not sure wher to find the generated Javascript. Is it what you expect ?


    [code]
    (function() {

    jQuery(function() {
    return $("#users-grid").dataTable({
    sDom: "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
    sPaginationType: 'bootstrap',
    iDisplayLength: 5,
    aLengthMenu: [[5, 10, 25, 50, 100, -1], [5, 10, 25, 50, 100, "All"]],
    bProcessing: true,
    bServerSide: true,
    sAjaxSource: $("#users-grid").data('source'),
    aoColumns: [
    {
    mData: "id",
    mData: "email",
    mData: "password_digest",
    mData: "remember_token",
    mData: "password_reset_token",
    mData: "password_reset_sent_at",
    mData: "activation_token",
    mData: "active",
    mData: "blocked",
    mData: "created_at",
    mData: "updated_at",
    mData: "created_by"
    }
    ]
    });
    });



    }).call(this);


    [/code]

    If it can be of any help using google chrome debugging tools I have

    in Console :
    [code]
    Uncaught TypeError: Cannot call method 'fnSetData' of undefined jquery.dataTables.js:821
    _fnSetCellData jquery.dataTables.js:821
    _fnGatherData jquery.dataTables.js:566
    (anonymous function) jquery.dataTables.js:6780
    jQuery.extend.each jquery.js:649
    jQuery.fn.jQuery.each jquery.js:271
    DataTable jquery.dataTables.js:6367
    (anonymous function) users.js:4
    fire jquery.js:1038
    self.fireWith jquery.js:1149
    jQuery.extend.ready jquery.js:434
    completed
    [/code]

    In Resources
    The error appears in jquery.DataTables.js around line 826
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    That's probably what I expected, but it's wrong. You have only one object in the array, with the same property defined multiple times. It would be like:

    [code]
    var i = 1;
    i = 2;
    [/code]

    and expect i to be both 1 and 2...

    Take a look again at the example I pated above which has an array of 5 elements. how you do that in CoffeeScript I don't know.

    Allan
  • phronphron Posts: 5Questions: 0Answers: 0
    Sorry what example you've paste above ?
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    http://datatables.net/blog/Extended_data_source_options_with_DataTables

    Specifically:

    [code]
    $('#example').dataTable( {
    "aoColumns": [
    { "mData": "cell1" },
    { "mData": "cell2" },
    { "mData": "cell3" }
    ]
    } );
    [/code]

    Allan
  • phronphron Posts: 5Questions: 0Answers: 0
    Thanks for all Allan,
    I think the problem is due to CoffeeScript (to my lack of knowledge of CoffeScript to be exact) I will retry in pure jQuery...
    Cheers
This discussion has been closed.