How to sort a column date strings as per time

How to sort a column date strings as per time

hrushi53hrushi53 Posts: 3Questions: 0Answers: 0
edited January 2014 in General
Hi,

I am using datatables plugin in my rails application. I have a column in my datatable which displays time returned by 'distance_of_time_in_words_to_now()" ruby method. The values displayed in this cell are strings such as 'about 21 hours', 'about 1 month', '6 days' , '1 day'. So now if we try to sort this column it sorts as '1 day', '6 days', 'about 1 month', 'about 21 hours' or vice versa; which is definitely not what I want. I want it to sort as per time. I know that datatables in built sort functionality doesn't know that it is time and considers it as strings and hence sorts them as strings.

So could some one tell me how to sort this as per time and still display it as above (value returned by the distance_of_time_in_words_to_now() ruby method) ??

Thanks,
Hrushi

Replies

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    That seems an unrealistic expectation of a sort method. Why not have your application return "normal" time as well, and use that as a hidden sort field?
  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin
    edited January 2014
    It is possible to do, but imho the overhead would be significant to process on every sort evaluation in pure Javascript (which I realise isn't what you are asking since you want to use a Ruby function).

    What I would suggest you do is use orthogonal data - a numeric representation which can be sorted for sort data and the string for filtering / display. See: http://datatables.net/blog/Orthogonal_Data

    Allan
  • hrushi53hrushi53 Posts: 3Questions: 0Answers: 0
    I would like to go with tangerine's comment it sounds easier. So I can have a hidden column which are the actual date durations (time) and a column which will display their user-friendly strings such as 1 day, about 1 month. Now how do I tell the datatable to sort the hidden column when the user clicks on the user-friendly date column. I have read about iDataSort but dont know how to fit that in my codebase.

    Let me explain here... My rails applications has a lot of pages with different datatables on each page. So we have this common datatable partial view which is used to display datatables on different pages. Now if I am planning to use this http://datatables.net/usage/columns#iDataSort, then where does this go? I suppose this javascript should go in my view file which is displaying this datatable that I am talking about. Am I right? This is how my view file looks like:

    [code]
    %h1.content-title
    = t(:recruiting_coordinator_queue)

    = select("choose", "work_step", @step_count_items.collect{|step_count_item| [translate_work_step_type(step_count_item.work_step_type) + " ("+ step_count_item.count.to_s + ")", step_count_item.work_step_type, selected_work_step_options(params[ART_CONFIG['rc_groups']['work_step_type_param']], step_count_item.work_step_type)]})

    = select("choose", "rc_group", @rc_groups.collect{|rc_group| [rc_group.description, rc_group.recruiting_coordinator_group_id.id, selected_group_options(params[ART_CONFIG['rc_groups']['rc_group_param']], rc_group.recruiting_coordinator_group_id.id)]})

    = t(:only_mine)
    = (params['all_users'] == 'true') ? check_box_tag('only_mine', 'true') : check_box_tag('only_mine', 'true', :checked=>"true")


    = datatable(Datatables::RecruitingCoordinatorQueue)

    - content_for :partial_script_includes do
    = javascript_include_tag 'query_string_lib'

    - content_for :partial_scripts do
    :plain
    $(document).ready(function() {
    // These three constants are the keys we use to insert query params into the url, representing the currently
    // selected work step and rc group for their respective drop downs and the all users checkbox.
    var WORK_STEP_TYPE_PARAM = '#{ART_CONFIG['rc_groups']['work_step_type_param']}';
    var RC_GROUP_PARAM = '#{ART_CONFIG['rc_groups']['rc_group_param']}';
    var ALL_USERS_PARAM = 'all_users';

    // Attach a listener to the work step drop down that will update the url with the currently selected work step
    // and trigger a page refresh with the new url query params.
    $("#choose_work_step").change(function(){
    var current_url_without_query_string = [location.protocol, '//', location.host, location.pathname].join('');
    window.location.href = current_url_without_query_string + update_url_query_string(WORK_STEP_TYPE_PARAM, this.value);
    });

    // Attach a listener to the all users checkbox that will update the url and trigger a page refresh with the new url query params.
    $("#only_mine").change(function() {
    var current_url_without_query_string = [location.protocol, '//', location.host, location.pathname].join('');
    window.location.href = current_url_without_query_string + update_url_query_string(ALL_USERS_PARAM, !this.checked);
    });

    // Attach a listener to the rc group drop down that will update the url with the currently selected rc group
    // and trigger a page refresh with the new url query params.
    $("#choose_rc_group").change(function() {
    var current_url_without_query_string = [location.protocol, '//', location.host, location.pathname].join('');
    window.location.href = current_url_without_query_string + update_url_query_string(RC_GROUP_PARAM, this.value);
    });
    });
    [/code]
  • hrushi53hrushi53 Posts: 3Questions: 0Answers: 0
    I have added the following below line 12,

    [code]
    - content_for :partial_scripts do
    :plain
    $(document).ready(function() {
    $('#datatables_recruiting_coordinator_queue_2830723327177820168').dataTable( {
    "aoColumns": [
    null,
    { "iDataSort": 2 },
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null
    ]
    });
    });
    [/code]

    But it doesnt work, it displays "DataTables warning (table id = 'datatables_recruiting_coordinator_queue_2830723327177820168'): Cannot reinitialise DataTable.

    To retrieve the DataTables object for this table, pass no arguments or see the docs for bRetrieve and bDestroy"

    I also tried adding the following below line 12 but it displays same error message

    [code]
    $(document).ready( function() {
    $('#datatables_recruiting_coordinator_queue_2830723327177820168').dataTable( {
    "aoColumnDefs": [
    { "iDataSort": 1, "aTargets": [ 0 ] }
    ]
    } );
    } );
    [/code]

    Could some one help me what's wrong with the above if I am doing it the right way. If not then how to do this?
  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin
    Where are you initialising your DataTable the first time? Your aoColumnDefs should go in there.

    Allan
This discussion has been closed.