How to sort a column date strings as per time
How to sort a column date strings as per time
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
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
This discussion has been closed.
Replies
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
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]
[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?
Allan