Breve guida su come utilizzare drupal_add_tabledrag per creare tabelle ordinabili per peso drag and drop

Questo codice lavora perfettamente e con grande soddisfazione lo posto! Grazie a tutti quelli che hanno dato dei consigli perchè il codice riportato altrove è simile ma proprio incompleto e inesatto. Ora potete implementare il vostro sito con delle funzionali tabelle droppabili fino a 200 record. Utilizzate lo script come volete o all'interno dei vostri moduli o create un modulo sulla base di questo codice e soprattutto, per il funzionamento create la tabella con il codice sql mostrato.

<?php
/*
CREATE TABLE dr_foo (
                  id serial NOT NULL,
                  value1 varchar(255) NOT NULL default '',
                  value2 varchar(255) NOT NULL default '',
                  weight smallint NOT NULL DEFAULT 0,
                  PRIMARY KEY (id)
                  );
INSERT INTO dr_foo (id, value1, value2, weight) VALUES (1, 'a', 'a', 1);
INSERT INTO dr_foo (id, value1, value2, weight) VALUES (2, 'b', 'b', 2);
INSERT INTO dr_foo (id, value1, value2, weight) VALUES (3, 'c', 'c', 3);
INSERT INTO dr_foo (id, value1, value2, weight) VALUES (4, 'd', 'd', 4);
INSERT INTO dr_foo (id, value1, value2, weight) VALUES (5, 'e', 'e', 5);
*/
function example_menu() {
  $items = array();
  $items[example] = array(
    'title' => t('Example tabledrag'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('example_form'),
    'access arguments' => array('go example'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}
function example_form(&$form_state){
  $form['#submit'][] = 'example_form_submit';
  $result = db_query("SELECT id, value1, value2, weight FROM {foo} ORDER BY weight");
  while ($row = db_fetch_object($result)){
    $data = array($row->value1, $row->value2);
    $form['rows'][$row->id]['data'] = array('#type' => 'value', '#value' => $data);
    $form['rows'][$row->id]['weight-'.$row->id] = array(
      '#type' => 'weight',
      '#delta' => 200,
      '#default_value' => $row->weight,
      '#attributes' => array('class'=>'weight', 'style'=>'display:none;'),
    );
  }
  $form['submit']=array(
    '#type'=>'submit',
    '#value'=>t('Save changes'),
  );
  $form['#theme'] = 'example_drawform';
  return $form;
}
function example_form_submit($form, &$form_state) {
  foreach($form_state['values'] as $key => $data){
    if (substr($key, 0, 6) == 'weight') {
      if (is_numeric($data)) {
        $id = str_replace('weight-', '', $key);
        db_query("UPDATE {foo} SET weight=%d WHERE id=%d", $data, $id);
      }
    }
  }
}
function theme_example_drawform($form){
  foreach($form['rows'] as $id => $row){
    if (intval($id)){
      $this_row = $row['data']['#value'];
      $this_row[] = drupal_render($form['rows'][$id]['weight-'.$id]);
      $table_rows[] = array('data'=>$this_row, 'class'=>'draggable');
    }
  }
  $header = array("Value1", "Value2", "Weight");
  $output = theme('table', $header, $table_rows, array('id'=>'example-table'));
  $output .= drupal_render($form);
  drupal_add_tabledrag('example-table', 'order', 'sibling', 'weight');
  return $output;
}
function example_theme() {
  return array(
    'example_drawform' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

Non allego file perchè occorrerebbe implementare anche il codice del file .info e del file.install nonchè le traduzioni. Se avete commenti o opinioni non esitate a scrivermi. Ciao da Danzisiweb!

Argomenti: