KulOmkar
KulOmkar

Reputation: 263

Sort date through datatables in c# mvc

I am trying to sort all columns which is defined through the datatables , I have enough code for sort, as all the other columns are get sorting .But the problem is occurring for date columns,Its converted into format 103 dd/mm/yyyy and i have to sort it without converting it into string .Its build in c# MVC 4.5 . My code related to it as following

#region Sort
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
var sortOrder = Request["sSortDir_0"].ToString();
var sortColumnName = GridSettingsBuilder.GetGridColumnName(EnumList.GridSettingTypeEnum.EmployeeActivity, sortColumnIndex);
string sortString = string.Empty;
if (!string.IsNullOrEmpty(sortColumnName))
{
     sortString = sortColumnName + " " + sortOrder;
}
activityList = activityList.OrderBy(sortString).ToList<GetEmployeeActivityList>();
#endregion

the code on view page

window["@Model.TableId"] = $(table).dataTable(
                        {
                            "bJQueryUI": true,
                            "processing": true,
                            "serverSide": true,
                            "aoColumnDefs": [
                                { "aTargets": [6],
                                        "bSortable": true,
                                        "sWidth": "8%",
                                        "mRender": function(data, type, full) {
                                            var input = data;//FormatDate(data);
                                            if(data == "" || data == null)
                                            {

                                                return '<div class="input-group date rptDateFrom" ><input readonly="true" type="text" name="rptDateFro" class="form-control input-small" id="txtidex" style="display:none;" onkeydown="return false"  /><span style="visibility: hidden;" class="input-group-addon"><span style="visibility: visible;" class="glyphicon glyphicon-calendar"></span></span> </div>';
                                                                                                }
                                            else{


                                                return '<div style="text-align:center; id="' + EmployeeActivityID + '" class="EditPlannedDate"">' + input + '<span style="color:black;text-align:center;"></span></div>';
                                            }
"bSort": true,

I used moment.min.js only ,

Please refer this image and help me enter image description here

I want to sort planned date column , i already tried by date-eu.js and with cases like columnDefs: [ { type: 'date-eu'} etc. can someone suggest me correct way to achieve it

Upvotes: 0

Views: 1288

Answers (1)

davidkonrad
davidkonrad

Reputation: 85578

date-eu is deprecated. Include the date-uk sorting plugin, and use date-uk as sType for the column :

aoColumnDefs: [
  { aTargets: [6],
    sType: 'date-uk', //<---
    bSortable: true,
    sWidth: "8%",
    mRender: function(data, type, full) {
...

Now that you are in fact using moment.js, you could also consider the dedicated datetime-moment plugin which is far more more flexible. You do not have to define that a certain column need a special date format, you simply "register" the formats that should be available :

$.fn.dataTable.moment( 'dd/mm/YYYY' );

Upvotes: 1

Related Questions