简体   繁体   中英

JQuery datatable server-side columns collection

I'm using Asp.Net MVC, all I need it's get collection of columns on server without creating a custom model binder.

If we look Datatable docs we will see columns[i].name parameter.

So I created model:

public class DtSentParameters
{
    public int Draw { get; set; }
    public int Start { get; set; }
    public int Length { get; set; }

    public IList<DtColumn> Columns { get; set; } //We are talking about this collection
}

Where DtColumn

public class DtColumn
{
    public string Name { get; set; }
    public string Data { get; set; }
}

Controller

public JsonResult GetSales(DtSentParameters data)
{
    //here placed some logic, but it's not important for us.
    return Json(data);
}

Datatable JS

dataTable({
    serverSide: true,
    ajax: {
        "url": url,
        "type": "POST"
    },
    processing: true,
    columns: [
        { data: "Date", name: "Date" },
        { data: "ClientAlias", name: "ClientAlias" }
    ],
    scrollCollapse: true,
    paging: true,
    jQueryUI: false
});

And everything works fine, I got my data and I can use paging. So i'm trying to add ordering, and here is a problem. my Columns.Count is equal 2 but name and data are empty.

I checked Request.Form.AllKeys

Request.Form.AllKeys

What we have, parameter columns[i].name looks like columns[i][name] . By the way Request.Form["columns[1][name]"] returns ClientAlias so it has right value but wrong name.

So maybe someone know why parameter name is different from official docs, and how can I change it.

I see only one desigions of my problem

  • Custom model binder.

But all I want it's just use Datatables as docs says

Any ideas how to get on server columns[i].name not columns[i][name] ?

I solved my problem, with using an undocumented or good hidden feature. At least I didn't find it HERE

All I done it's changed my dataTable init script

dataTable({
  serverSide: true,
  ajax: {
      url: url,
      type: "POST",
      contentType: 'application/json; charset=utf-8', //set contentType,
      data: function(d) {
            return JSON.stringify(d);}      //stringify object by myself
       }
   });

And it's alive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM