ColReorder not sort correctly, what i make bad?

ColReorder not sort correctly, what i make bad?

moskardonmoskardon Posts: 4Questions: 1Answers: 0

HI,
I call a function on my controller to fill one datatable, but on move any column, sort method seems take previous index of column.
js code:
dTable = $('#table-results').DataTable
({
"colReorder": {
fixedColumnsLeft: 1
},
"processing": true,
"serverSide": true,
"pageLength": 20,
"language": objLanguage,
"scrollX": true,
"paging": true,
"ajax":
{
"url": "./DataListResult",
"type": "POST",
"dataType": "JSON",
"data":
{
landlordname: $("#input-landlord-name").val().trim(),
location: $("#input-location").val().trim(),
role: $("#select-role").val(),
VAT: $("#input-VAT").val().trim()
}
},
"columns": [
{
data: "IdLandlord",
render: function (data, type, row) {
return "<a href='./Edit/"+data+"' >"+ data+ "</a>";
}
},
{ data: "LegacyCode" },
{ data: "PrefixName" },
{ data: "Name" },
{ data: "Adress" },
{ data: "Country" },
{ data: "PostalCode" },
{ data: "IdIdentificationTypeName" },
{ data: "IdentficatonNumber" },
{ data: "Location" },
{ data: "Contact" },
{ data: "Phone1" },
{ data: "Phone2" },
{ data: "Phone3" },
{ data: "Email1" },
{ data: "Email2" },
{ data: "Email3" },
{ data: "VAT" }
]
});

c# code:
public ActionResult DataListResult(string landlordname, string location, int? role, string VAT)
{
JsonResult result;

        //Initialization.
        string search = Request.Form.GetValues("search[value]")[0];
        string draw = Request.Form.GetValues("draw")[0];
        string order = Request.Form.GetValues("order[0][column]")[0];
        string orderDir = Request.Form.GetValues("order[0][dir]")[0];
        int startRec = Convert.ToInt32(Request.Form.GetValues("start")[0]);
        int pageSize = Convert.ToInt32(Request.Form.GetValues("length")[0]);

        try
        {
            var model = _landlordService.GetFilter(landlordname, location, role, VAT).ToList();
            int totalRecords = model.Count;


            model = this.SortByColumnWithOrder(order, orderDir, model);

            //Filter record count.
            int recFilter = model.Count;

            //Apply pagination.   
            model = model.Skip(startRec).Take(pageSize).ToList();



            result = this.Json(new
            {
                draw = Convert.ToInt32(draw),
                recordsTotal = totalRecords,
                recordsFiltered = recFilter,
                data = model
            }, JsonRequestBehavior.AllowGet);
        }
        catch (Exception e)
        {
            Log.Error(e.Message, e);
            throw;
        }
        return result;
    }

Run correctly but only if colReorder is on false.

I do not know what else I can try.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin

    "serverSide": true,

    Do you really need server-side processing. If not, remove it and it will work. If you do, then you need to change your server-side script to take account of the fact that columns can be reordered.

    Allan

  • moskardonmoskardon Posts: 4Questions: 1Answers: 0

    I need send :
    landlordname: $("#input-landlord-name").val().trim(),
    location: $("#input-location").val().trim(),
    role: $("#select-role").val(),
    VAT: $("#input-VAT").val().trim()

    That data is to filter the load of the table and i do not know another way to send it in the ajax call.

  • moskardonmoskardon Posts: 4Questions: 1Answers: 0

    I have seen that when moving the columns, in the html code the index or data-column-index property, it changes to leave the same order again. Could that be the problem? since when changing position, the index would have to move to know where to order. At what time does this property change when changing the column position? I will try to skip the function that changes those values.

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin
    Answer ✓

    When the table's columns are reordered, the order of the column array that DataTables sends to the server is changed. That is not configurable (i.e. that behaviour cannot be disabled).

    You need to use the data property that is sent for each column (see the manual) to determine the column in the database that each column now represents.

    Allan

  • moskardonmoskardon Posts: 4Questions: 1Answers: 0

    I understand, I have assigned a name to each column and with the indicator where you click to order I can locate it and reorder with linq.
    thank you very much

This discussion has been closed.