How can I get jQuery DataTables to sort on hidden value, but search on displayed value?

后端 未结 9 1574
暖寄归人
暖寄归人 2020-12-08 09:47

I have a simple DataTables grid which contains date columns. I have provided two values for the date in my JSON data set, one for display and one specifically designed so th

相关标签:
9条回答
  • 2020-12-08 10:40

    This is an old post, but hopefully this will help someone else that comes here.

    In a more recent version of DataTables, bUseRendered and fnRender are deprecated.

    mRender is the new way of doing this sort of thing and has a slightly different approach.

    You can solve your issue with something along the lines of:

    ...
    { mDataProp: "Date.Sort"
      bSortable: true, 
      sName: "Date", 
      // this will return the Display value for everything
      // except for when it's used for sorting,
      // which then it will use the Sort value
      mRender: function (data, type, full) {
        if(type == 'sort') return data.Sort;
        return data.Display
      }
    }
    ...
    
    0 讨论(0)
  • 2020-12-08 10:40

    Try a little bit different approach:

    Put both columns in the table (I will call them DateDisplay and DateSort), do not use render function and just hide the DateSort column. Then in the aoColumns array for the column DateDisplay put { "iDataSort": 2 }, where 2 is an index of the DateSort column.

    In this case, DateDisplay column will be shown in original data, and filter will be done by this column, but sorting will be done by the values in the DateSort column.

    There are more details about the iDataSort property on the datatables site or in the http://www.codeproject.com/KB/scripting/JQuery-DataTables.aspx section"Configure sorting".

    0 讨论(0)
  • 2020-12-08 10:43

    Umm...instead of going through all of this rigmarole just add a hidden span with your "Sort By" to the front:

    <td><span style="display:none;">20101131133000</span>11/31/2010 1:30 PM</td>
    

    Note: This does mean that they can search by either the hidden or shown value...this may be a consequence you can't live with.

    0 讨论(0)
提交回复
热议问题