0
var headers = [{ "mDataProp": "Agents" },{ "mDataProp": "Buyer" },{ "mDataProp": "Date" }];
$(document).ready(function () {   
    $('#data-table').dataTable({
        "bFilter": false,
        "bLengthChange": false,
        "iDisplayLength": 25,
        "bJQueryUI": true,
        "bServerSide": true,
        "bProcessing": true,
        "sPaginationType": "full_numbers",
        "aoColumns": headers,
        "sAjaxSource": '/report/TestPaging',         
    });
});

If I remove the aoColumns from my code the datatable is generated normally, but when I add aoColumns I get :

DataTables warning (table id = 'data-table'): Requested unknown parameter 'Agents' from the data source for row 0

HTML:

<table id="data-table">
                <thead>
                    <tr>                          
                        <th>tmp</th>
                        <th>tmp</th>
                        <th>tmp</th>
                    </tr>
                </thead>

                <tbody>
                </tbody>
            </table>

How can I configure header names? I need them for my sorting. Do they have to be same as in "th" tags? Json response from my controller is ok because it renders fine when I remove aoColumns

I have string[][](var data) with 3 strings in each line and return it as

  Json(new{
            sEcho = param.sEcho,
            iTotalRecords = visitRepository.Query.Count(),
            iTotalDisplayRecords = visitRepository.Query.Count(),
            aaData = data
        }, JsonRequestBehavior.AllowGet);
3
  • How does your objects / json look like? Commented Mar 2, 2014 at 20:32
  • That shouldn't be the problem,table generates just fine without aoColumns line. Commented Mar 2, 2014 at 20:54
  • see my answer, I think, no - I am certain - it actually is a JSON issue :) Try rename one of the JSON antries in the fiddle, just one col, like the first "Agents", and you can reproduce the error. Commented Mar 2, 2014 at 20:57

1 Answer 1

2

The error you are experiencing is caused by a mismatch between the contents of the JSON and your aoColumns definition. The names you are targeting in aoColumns must be exactly the same as those in the JSON, and the length of each object in the JSON must be equal to the number of columns in the original HTML table. See the docs for clarification. In your case, the JSON should look like this :

[{
  "Agents": "tM4Ga0zX",
  "Buyer": "ZKwMEiIa",
  "Date": "K3lS2yn9"
},
...]

...And your JSON doesnt follow that scheme. If you are not using aoColumns, then the data is inserted by index, not by name - and that is why it is working for you without it.

You configure header names (titles) by the sTitle property :

aoColumns: [
  { mDataProp: "Agents", sTitle : "Agents" }, 
  { mDataProp: "Buyer", sTitle : "Buyer" },
  { mDataProp: "Date", sTitle : "Date" }
]

see this demonstration based on your question -> http://jsfiddle.net/kLR7G/

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

5 Comments

So I can't just use string[][] and return it as JSON?
Here is a great answer to that (asp.net-mvc-3 and sAjaxSource) -> stackoverflow.com/questions/7835960/…. The important thing, as for avoiding the error, is to form aaData correct, try the suggestion in the accepted answer in the link, and leave sEcho, iTotalRecords etc out.
problem is that my columns aren't pre-set, they can be many things depending on other conditions. I was hoping to just name headers so I can sort with dynamic linq,but formatting json will be a bit harder with this format
Well, why dont you generate the whole table serverside? I do exactly that myself when I dont know the result in front. I return the whole thing, <table id="something"><thead><tr><th> and the complete <tbody>, and then I call dataTables upon that. The disadvantage by this approach is that you are limited to more or less 5000 rows before the browsers are reporting timeouts, because dataTables is slow when initializing (jQuery functions is very slow). Sad to say, I know very little to asp.net, can only say why datatables are throwing the error and how to avoid it :(
I have a few thousand rows,so I have to use pagination to reduce load time. And to use pagination I can't generate whole table because that would take too long, this way I can get table in miliseconds. I'll see what I can do about generating json response to be in line with headers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.