http://localhost/drupal6/draggabledemo 可看到效果。(如果你的term_data表无数据,请随便添加几行进去以便测试)
draggabledemo.module 代码如下:
-----------------------------------------------------------------------------------
<?php
/**
* Implementation of hook_menu
*/
function draggabledemo_menu() {
$items = array();
$items['draggabledemo'] = array(
'title' => '可拖动排序的表格',
'page callback' => 'drupal_get_form',
'page arguments' => array('draggabledemo_sort_form'),
'access arguments' => array('access content'),
);
return $items;
}
/**
* Form for re-ordering draggabledemo
*/
function draggabledemo_sort_form() {
//以drupal自带的term_data表为例
$result = db_query("SELECT tid, name, weight FROM {term_data} ORDER BY weight ASC");
$form = array();
$form['list']['#tree'] = TRUE;
$form['list']['#theme'] = 'draggabledemo_list_sort_form';
while ($row = db_fetch_object($result)){
$tid = $row->tid;
$name = $row->name;
$weight = $row->weight;
$form['list'][$tid]['title'] = array('#value' => $name);
$form['list'][$tid]['sort'] = array('#type' => 'weight', '#delta' => 5, '#default_value' => $weight);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Theme the re-ordering form
*/
function theme_draggabledemo_list_sort_form($form) {
drupal_add_tabledrag('draggabledemo-sort', 'order', 'sibling', 'sort');
$header = array('', 'title', 'sort');
foreach (element_children($form) as $key) {
// Add class to group weight fields for drag and drop.
$form[$key]['sort']['#attributes']['class'] = 'sort';
$row = array(''); //This is important. We need to start with an empty element for the drag handle.
$row[] = drupal_render($form[$key]['title']);
$row[] = drupal_render($form[$key]['sort']);
$rows[] = array('data' => $row, 'class' => 'draggable'); //note the difference between $row and $rows
}
$output = theme('table', $header, $rows, array('id' => 'draggabledemo-sort'));
$output .= drupal_render($form);
return $output;
}
/**
* Implementation of hook_theme
*/
function draggabledemo_theme() {
return array(
'draggabledemo_list_sort_form' => array(
'arguments' => array('form' => NULL),
),
);
}
function draggabledemo_sort_form_submit($form, &$form_state) {
foreach($form_state['values']['list'] as $id => $weight){
//echo $id . ': '. $weight['sort'] .'<br />';
db_query("UPDATE {term_data} SET weight = %d WHERE tid = %d", $weight['sort'], $id);
}
drupal_set_message('排序已保存');
}