First attempt at DataTables. Error: Requested unknown parameter '0' from the data source for row 0

First attempt at DataTables. Error: Requested unknown parameter '0' from the data source for row 0

I3ennersI3enners Posts: 2Questions: 0Answers: 0
edited March 2012 in General
Hi there

Can someone tell me what I am doing wrong here? I get the error above and null in all the columns and rows of my table. Am I using aoColumnDefs correctly?

my datatable call:

[code]
$('#libraryItemList').dataTable({
"bServerSide": true,
"sAjaxSource": "Library/IndexAJAX",
"bProcessing": true,
"aoColumnDefs": [
{ "sName": "Title", "aTargets": ["Title"] },
{ "sName": "PublicationYear","aTargets":["PublicationYear"] },
{ "sName": "Location","aTargets": ["Location"] },
{ "sName": "LibraryShelf","aTargets": ["LibraryShelf"] },
{ "sName": "Summary", "aTargets": ["Summary"] },
{ "sName": "Author", "aTargets": ["Author"] },
{ "sName": "Category", "aTargets": ["Category"]},
{ "sName": "ISBN", "aTargets": ["ISBN"] },
{ "sName": "ISSN", "aTargets": ["ISSN"] },
{ "sName": "BusinessGroup", "aTargets": ["BusinessGroup"] },
{ "sName": "Officer", "aTargets": ["Officer"] },
{ "sName": "InputtedOn", "aTargets": ["InputtedOn"] },
{ "sName": "LibraryUser", "aTargets": ["LibraryUser"] },
{ "sName": "IsElectronicDoc", "aTargets": ["IsElectronicDoc"] }
]
});
[/code]

the html table:

[code]





Title


PublicationYear


Location


LibraryShelf


Summary


Author


Category


ISBN


ISSN


BusinessGroup


Officer


InputtedOn


LibraryUser


isElectronicDoc






[/code]

The JSON i am trying to parse:

[code]
{"sEcho":"1",
"iTotalRecords":10,
"iTotalDisplayRecords":10,
"aaData":[{
"Title":"FIRST AID AT WORK: YOUR QUESTIONS ANSWERED",
"PublicationYear":1997,
"Location":"ELECTRONIC DOCUMENT",
"LibraryShelf":"ELECTRONIC DOCUMENT",
"Summary":"Ref IND[G]214L. Leaflet to answer some basic questions about first aid provision at work.",
"Author":"HEALTH AND SAFETY EXECUTIVE",
"Category":"HEALTH AND SAFETY",
"ISBN":null,
"ISSN":null,
"BusinessGroup":"",
"Officer":"",
"InputtedOn":"\/Date(1242946800000)\/",
"LibraryUser":"LFJones",
"IsElectronicDoc":true
},
{
"Title":"NEED FOR OLD BUILDINGS TO BREATHE",
"PublicationYear":1987,
"Location":"SUBJECT SEQUENCE A-Z",
"LibraryShelf":"ARCHITECTURAL & HISTORIC BUILDINGS/CONSERVATION",
"Summary":"Information Sheet 4",
"Author":"SOCIETY FOR THE PROTECTION OF ANCIENT BUILDINGS",
"Category":"ARCHAEOLOGY/HISTORIC HERITAGE",
"ISBN":null,
"ISSN":null,
"BusinessGroup":"",
"Officer":"",
"InputtedOn":"\/Date(708649200000)\/",
"LibraryUser":"LFJones",
"IsElectronicDoc":false
}]}
[/code]

plus another 8 records

thanks for your time

Replies

  • allanallan Posts: 63,189Questions: 1Answers: 10,411 Site admin
    Thanks for providing the information you did! (if you want to do it a little easier in future you can use the debugger: http://debug.datatables.net :-) ).

    I think the problem is with this line:

    [code]
    "aoColumnDefs": [
    { "sName": "Title", "aTargets": ["Title"] },
    ...
    [/code]

    and the other column definitions. What that line is telling DataTables is to apply the name 'Title' to columns which have a class of 'Title' on their header cell. You already have the title in place, and no class, so this isn't quite right. What I would suggest you want is actually:

    [code]
    "aoColumns": [
    { "mDataProp": "Title" },
    ...
    [/code]

    This blog post explains mDataProp in detail: http://datatables.net/blog/Extended_data_source_options_with_DataTables

    Allan
  • I3ennersI3enners Posts: 2Questions: 0Answers: 0
    Thanks Allan that fixed it. I was in too much of a .net mode and assumed class was an object and meant the datacolumn name not a css class. I also didn't notice that my data was in an object rather a string array. doh.

    Back to playing with paging and sorting now!

    cheers

    Ben
This discussion has been closed.