0

I am creating a view with datatables in drupal 7. It has a column date in format 'd/m/Y'. For making it sort correctly, I am planning to add attribute 'data-order' to these table cells as referred from here.

I am facing issues while trying to rewrite the <td> containing date with templates or preprocess functions. I want to add data-order attribute to date cell in every row so I can use it later for front-end sorting.

Can someone here help me?

Community
  • 1
  • 1
meen
  • 2,287
  • 3
  • 23
  • 42
  • Please describe what "issues" you are facing, for example the code where you try to rewrite th ``s. `data-order` is merely for columns where you have difference between the view and how you want to order the column. It seems to me you are better off with a sorting plugin. – davidkonrad May 09 '17 at 12:55
  • @davidkonrad I am finding ways to add data-order attribute to existing column... I tried with template preprocess, but it allows me to innerhtml of td and not the attribute. – meen May 09 '17 at 14:37
  • 1
    what do you mean by 'sort correctly'?? drupal view will sort dates just fine... – Bart Hofma May 10 '17 at 09:26
  • If you want to add some attribute to every row so you could use it later for front-end sorting (JS/jQuery) you should mention that. If that the case you should use some format like 'Ymd', so year is first, then month and the day and then you can sort it as any string or number - it should work. – MilanG May 10 '17 at 09:46
  • @BartHofma, views alone works perfect. I am having issues with views and datatables combined. – meen May 10 '17 at 11:17
  • @MilanG, thanks, updated the question. The requirement is to keep date in d/m/Y format only. so can't change format of date – meen May 10 '17 at 11:17
  • Can you keep that attribute in needed format and add another one, just for sorting purpose in the format I suggested? – MilanG May 10 '17 at 14:23

1 Answers1

1

Instead of adding 'data-order' attribute, problem got solved by adding a hidden span in date column. The same thing I had tried before but with template preprocess, which did not work.

/* This did not work */
function template_preprocess_views_view_field__viewname__date_field(&$vars) {
  $vars['output'] = "<span class='hidden'>".$vars['row']->date."</span>".$vars['row']->date;
}


/* THIS WORKED */
/* Field: Content: date */
$handler->display->display_options['fields']['date_h']['id'] = 'date_h';
$handler->display->display_options['fields']['date_h']['table'] = 'node';
$handler->display->display_options['fields']['date_h']['field'] = 'created';
$handler->display->display_options['fields']['date_h']['exclude'] = TRUE;
$handler->display->display_options['fields']['date_h']['date_format'] = 'custom';
$handler->display->display_options['fields']['date_h']['custom_date_format'] = 'U';
$handler->display->display_options['fields']['date_h']['second_date_format'] = 'long';
/* Field: Content: Post date */
$handler->display->display_options['fields']['date']['id'] = 'date';
$handler->display->display_options['fields']['date']['table'] = 'node';
$handler->display->display_options['fields']['date']['field'] = 'created';
$handler->display->display_options['fields']['date']['label'] = 'Date';
$handler->display->display_options['fields']['date']['date_format'] = 'short_date_only';
$handler->display->display_options['fields']['date']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['date']['alter']['text'] = '<span class="hidden">[date_h]</span>[date]';
meen
  • 2,287
  • 3
  • 23
  • 42