How do I sort the data using if?
How do I sort the data using if?
 gunseli            
            
                Posts: 14Questions: 8Answers: 0
gunseli            
            
                Posts: 14Questions: 8Answers: 0            
            I have a table in MVC that I pull the table from the database and sort the data according to the following condition.
var lineitems = Model.lineItems.OrderBy(l => l.MasterNumber == "0" ? int.Parse(l.Number) : int.Parse(l.MasterNumber)).ToList();
When I want to use this table with Datatable editor, I can't give the above condition. A method I can use the order option on the View side according to the if condition is there? Or how can I sort the data in the response when sending data by the Controller?
CONTROLLER
             var response = new Editor(db, "V_LineItemEdit")
                       .Model<LineItemSupplierEditModel>("V_LineItemEdit")
                    .Field(new Field("V_LineItemEdit.Id")
                    .Validator(Validation.NotEmpty()))
                    .Field(new Field("V_LineItemEdit.Number")
                    .Validator(Validation.NotEmpty()))
                    .Field(new Field("V_LineItemEdit.Impa")
                    .Validator(Validation.NotEmpty()))
                    .Field(new Field("V_LineItemEdit.Description")
                    .Validator(Validation.NotEmpty()))
                    .Field(new Field("V_LineItemEdit.Unit")
                    .Validator(Validation.NotEmpty()))
                     .Field(new Field("V_LineItemEdit.Qtty")
                     .Validator(Validation.Numeric())
                    .Validator(Validation.NotEmpty()))
                      .Field(new Field("V_LineItemEdit.Price")
                      .Validator(Validation.Numeric())
                     .SetFormatter(Format.IfEmpty(null)))
                     .Field(new Field("V_LineItemEdit.AltQtty")
                     .Validator(Validation.Numeric())
                     .SetFormatter(Format.IfEmpty(null)))
                     .                   
                      .Pkey("Id")
                      .Where("ReferanceNumberId", id)
                      .Where("IsRemoved", false)    
                      .Process(formData)
                      .Data();
                return Json(response, JsonRequestBehavior.AllowGet);
VIEW
        editor = new $.fn.dataTable.Editor({
            ajax: "/TechnicalPricing/LineItemsTable/" + id,
            table: "#items"+id,
            fields: [
                {
                    label: "Id",
                    name: "V_LineItemEdit.Id"
                },
                {
                    label: "Number",
                    name: "V_LineItemEdit.Number"
                },
                {
                    label: "Impa",
                    name: "V_LineItemEdit.Impa"
                },
                {
                    label: "Description",
                    name: "V_LineItemEdit.Description"
                },
                {
                    label: "Unit",
                    name: "V_LineItemEdit.Unit"
                },
                {
                    label: "Qtty",
                    name: "V_LineItemEdit.Qtty"
                },
                {
                    label: "Price",
                    name: "V_LineItemEdit.Price"
                },
                {
                    label: "AltQtty",
                    name: "V_LineItemEdit.AltQtty"
                }
            ]
        });
        var table = $('#items'+id).DataTable({
            dom: "Bfrtip",
            ajax: "/TechnicalPricing/LineItemsTable/" + id,
            "ordering": false,
            "paging": false,
            pageLength: 10,    
            columns: [
                { data: "V_LineItemEdit.Id" },
                { data: "V_LineItemEdit.Number", className:' text-left font-bold'  },
                { data: "V_LineItemEdit.Impa", className: 'editable text-left font-bold' },
                { data: "V_LineItemEdit.Description",  className: 'editable text-left font-bold'  },
                { data: "V_LineItemEdit.Unit",  className: 'editable text-left font-bold'  },
                { data: "V_LineItemEdit.Qtty",  className: 'editable text-left font-bold', render: $.fn.dataTable.render.number('.', ',', 2) },
                { data: "V_LineItemEdit.Price", className: 'editable text-left font-bold' ,render: $.fn.dataTable.render.number('.', ',', 2)  },
                { data: "V_LineItemEdit.AltQtty",  className: 'editable text-left font-bold' ,render: $.fn.dataTable.render.number('.', ',', 2)  },
            ],
           order: [[ 1, 'asc' ]],
           select: {
                style: 'os',
                selector: 'td:first-child'
            },
            buttons: [
            ]
        });
Answers
You are using client-side processing, so DataTables is sorting the data on the client-side. In this case you'd need to use a custom sort function to get the ordering you want.
If you sent the master number to the client (have it in a hidden column) then you could just use multi-column sorting which would be easier.
Allan
Thanks for reply.
But,If master number is a zero I need to convert number to integer. Would you please send me example ? so i can see how to using.
Hi @gunseli ,
You would use
columns.renderto do that conversion.Cheers,
Colin