Problem/Motivation

Due to the code for responsive tables that adds the header content as an attribute to all cells, sortable tables break in the front-end.
This is due to the fact that sortable tables have Link objects as their header content and the Attribute rendering class can't handle them.

Steps to reproduce

Create a sortable table in the front-end.

Proposed resolution

Add a check that converts Link objects to strings when adding them as attributes.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

L_VanDamme created an issue. See original summary.

rembrandx’s picture

@L_VanDamme: I'm not entirely sure what you mean with converting the Link objects to strings. If you've already fixed it somewhere, I would appreciate a patch, MR or a code sample.

rembrandx’s picture

Or at least point me to the file where you think this code is located.

rembrandx’s picture

I'm guessing you mean that this bit of code in the .theme file should check the header 'content' for links and extract the text instead?
Will keep this issue open until we've properly reproduced & tested this. At the moment, I'm not finding any use cases (or implementations) of this kind of sortable tables in the front-end, so I'm not sure how/where to test this.

foreach($row['cells'] as $key => $cell) {
      if (isset($variables['header'][$key]['content'])) {
        $cell['attributes']['data-title'] = $variables['header'][$key]['content'];
      }
      $cells[] = $cell;
    }
rembrandx’s picture

Status: Active » Needs review

After some deliberation and testing with a table-formatted view, I believe the best solution for this use case (sortable tables or any links on th's), is NOT to use the 'reformatted' type of responsive table but 'scroll'. From a UI point of view, reformatting the table makes no sense if your because the behavior relies on the th's always being there on top.

There are 2 types available: scrolling and reformatted. You can pick either one to force onto tables (if generated outside of CKE) by applying the 'responsive' attribute and passing a class via a preprocess hook.

This is an example of how to set it for a view table.

function THEME_preprocess_views_view_table(&$variables) {

  // We want tables from views to be using scrolling
  // this usually works better for most purposes (eg. for sortable tables)
  $variables['responsive']= TRUE;
  $variables['attributes']['class'][] = 'table--scroll';
}

If you're targetting a table directly, it's possible you need to use $variables['table']['#responsive'] and $variables['table']['#attributes'] for getting to the classes.
(eg. the preprocess_office_hours_table function in the .theme file can be used as a starting point)

Regardless of this:

nielsaers made their first commit to this issue’s fork.

rembrandx’s picture

Some of my thoughts on the use cases of 'reformatted' vs 'scrolling' tables:

Picking one of the 2 depends on what kind of data the table needs to handle and its overall purpose for the user/visitor. Most likely, as soon as its purpose is ‘display a set of data’, scrolling tables is the sensible way to display it.

Reformatted tables only make sense (in my opinion) for very specific use cases, such as a product basket in a webshop, or perhaps a small table with product properties.

It’s just that a case which requires reformatted tables will not likely overlap with a case where the table needs actions in its header (eg. those links for sorting).

rembrandx’s picture

Status: Needs review » Fixed

Got the example (a custom table in a form), added the fix

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.