首页 > 网站 > 建站经验 > 正文

drupal创建拖动排序表格的方法

2024-08-30 19:07:45
字体:
来源:转载
供稿:网友

Drupal中,有许多用户界面采用了拖动排序的功能。在排序的界面上,拖动排序是一种比较友好的形式。
下面是一个例子。

1. 在hook_menu里定义一个menu

//栏目下,节点排序界面,可以拖动行来排序 $items['admin/nodeorder_taxonomy/term/%taxonomy_term/nodeorder'] = array ( 'title' => '节点排序', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodeorder_taxonomy_term_nodeorder_form',3), 'access callback' => '_nt_node_order_right', 'access arguments' => array(3), 'type' => MENU_LOCAL_TASK, );

2.在hook_theme里定义表单的theme方法

function my_module_theme() {
return array( 'nodeorder_taxonomy_term_nodeorder_form' => array( 'render element' => 'form', ), );
}

3.定义表单。表单的定义与一般表单基本一样。这里用$form['nodes']存储了表格里需要的数据,用$form['nodes'][$count]表示一行。但是对于tabledrag来说,这不是必须的。

function nodeorder_taxonomy_term_nodeorder_form(&$form,&$form_state,$term) {
$tid=$term->tid; $query=db_select('node_term_order','nto')->extend('PagerDefault')->limit(20);
$query->join('node','n','nto.nid = n.nid');
$query->fields('n',array('nid','title','created')) ->fields('nto',array('tid','node_order')) ->condition('nto.tid',$tid) ->condition('sticky_order',0,'<=') ->orderBy('sticky_order','desc') ->orderBy('nto.node_order', 'desc');
$result=$query->execute();
$form['term_name']=array("#markup" => $term->name);
$form['nodes']['#tree']=true;
$form['nodes']['#theme'] = 'theme_nodeorder_taxonomy_term_nodeorder_form';
$delta=20*5; $count=0;
$form['foreactions'] = array('#type' => 'actions');
$form['foreactions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
foreach ($result as $row) { $form['nodes'][$count]['title']=array('#markup' => "nid)."">".check_plain($row->title)."" );
$form['nodes'][$count]['created']=array('#markup' => date("Y-m-d H:i:s",$row->created));
$form['nodes'][$count]['nid']=array( '#type'=>'value', '#title_display' => 'invisible', '#value' => $row->nid, );
$form['nodes'][$count]['tid']=array( '#type'=>'value', '#title_display' => 'invisible', '#value' => $row->tid, );
$form['nodes'][$count]['node_order']=array( '#type' => 'value', '#title_display' => 'invisible', '#value' => $row->node_order, );
$form['nodes'][$count]['weight'] = array( '#type' => 'weight', '#delta' => $delta, '#title_display' => 'invisible', '#default_value' => $count, '#title' => t('Weight for @title', array('@title' => $row->title)), );
$count +=1; } $form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
return $form;
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表