0

I have a tableselect and I have been looking all over the internet for a tutorial on how to make a tableselect have sorting/drag-and-drop functionality. I have found tutorials for making normal lists have click and drag, but not a single mention of tableselect.

I would like to know if making a tableselect draggable is possible. I'm not asking for a tutorial. At this time I just want to know if it is possible.

This the form element I am using.

$form['mod_setttings']['settings'] = array(
  '#type' => 'tableselect',
  '#header' => $header,
  '#options' => $options,
  '#empty' => t('No settings available')
);

This tableselect is inside a vertical_tabs element.

0

3 Answers 3

0

i think you are looking for tabledrag option.If so please refer drupal_add_tabledrag

1
  • The documentation page has just an example for a table, while the OP is expressly asking for a tableselect form element.
    – apaderno
    Mar 23, 2019 at 11:57
0

check these examples :

Drupal: Drag and Drop Tables in Custom Modules

CREATING A FORM WITH DRAGGABLE ELEMENTS

1
  • Neither links are for a tableselect form element.
    – apaderno
    Mar 23, 2019 at 11:44
0

Without writing your code, you cannot. To make a table (which isn't a form element) draggable, you first call theme('table') and then drupal_add_tabledrag(). With a tableselect form element, you don't call theme('table'), so you cannot call drupal_add_tabledrag() either.

What you could do is implementing a new form element and, in the theme function associated to that form element, call theme('table') and then drupal_add_tabledrag(). theme_tableselect() code can be used as guideline.

function theme_tableselect($variables) {
  $element = $variables['element'];
  $rows = array();
  $header = $element['#header'];
  if (!empty($element['#options'])) {

    // Generate a table row for each selectable item in #options.
    foreach (element_children($element) as $key) {
      $row = array();
      $row['data'] = array();
      if (isset($element['#options'][$key]['#attributes'])) {
        $row += $element['#options'][$key]['#attributes'];
      }

      // Render the checkbox / radio element.
      $row['data'][] = drupal_render($element[$key]);

      // As theme_table only maps header and row columns by order, create the
      // correct order by iterating over the header fields.
      foreach ($element['#header'] as $fieldname => $title) {
        $row['data'][] = $element['#options'][$key][$fieldname];
      }
      $rows[] = $row;
    }

    // Add an empty header or a "Select all" checkbox to provide room for the
    // checkboxes/radios in the first table column.
    if ($element['#js_select']) {

      // Add a "Select all" checkbox.
      drupal_add_js('misc/tableselect.js');
      array_unshift($header, array(
        'class' => array(
          'select-all',
        ),
      ));
    }
    else {

      // Add an empty header when radio buttons are displayed or a "Select all"
      // checkbox is not desired.
      array_unshift($header, '');
    }
  }
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'empty' => $element['#empty'],
    'attributes' => $element['#attributes'],
  ));
}

Keep in mind the conditions reported in drupal_add_tabledrag() for the dragging to work, including that the table must have an ID attribute which must be equal to the first argument passed to drupal_add_tabledrag(), and each table row needs a weight value (which is used to sort the rows).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.