欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

drupal創(chuàng)建拖動排序表格的方法

  發(fā)布時間:2014-11-03 16:57:21   作者:佚名   我要評論
這篇文章主要為大家介紹了drupal創(chuàng)建拖動排序表格的方法,包括了具體的步驟與實現(xiàn)代碼,具有一定的參考借鑒價值,需要的朋友可以參考下

Drupal中,有許多用戶界面采用了拖動排序的功能。在排序的界面上,拖動排序是一種比較友好的形式。
下面是一個例子。

1. 在hook_menu里定義一個menu

復(fù)制代碼
代碼如下:
//欄目下,節(jié)點排序界面,可以拖動行來排序 $items['admin/nodeorder_taxonomy/term/%taxonomy_term/nodeorder'] = array ( 'title' => '節(jié)點排序', '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方法

復(fù)制代碼
代碼如下:
function my_module_theme() {
return array( 'nodeorder_taxonomy_term_nodeorder_form' => array( 'render element' => 'form', ), );
}

3.定義表單。表單的定義與一般表單基本一樣。這里用$form['nodes']存儲了表格里需要的數(shù)據(jù),用$form['nodes'][$count]表示一行。但是對于tabledrag來說,這不是必須的。

復(fù)制代碼
代碼如下:
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;
}

4.theme上面定義的表單。這里的關(guān)鍵

① 對每一行中用于存放順序的weight字段,指定class 

復(fù)制代碼
代碼如下:
$form['nodes'][$count]['weight']['#attributes']['class'] = array('text-format-order-weight');

② theme table時指定table的ID:

復(fù)制代碼
代碼如下:
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));

③ 加上tabledrag需要的功能:

復(fù)制代碼
代碼如下:
drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
function theme_nodeorder_taxonomy_term_nodeorder_form($variables) {
$form = $variables['form'];
$rows = array();
if (isset($_GET["page"]))
$page=intval($_GET["page"]);
else
$page=0;
foreach (element_children($form['nodes']) as $count) { $form['nodes'][$count]['weight']['#attributes']['class'] = array('text-format-order-weight');
$rows[] = array( 'data' => array( drupal_render($form['nodes'][$count]['title']), drupal_render($form['nodes'][$count]['created']), drupal_render($form['nodes'][$count]['weight']), ), 'class' => array('draggable'), );
}
$header = array(t('title'), t('created'), t('weight') );
$output = drupal_render($form['term_name']);
$output .= drupal_render($form['foreactions']);
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));
$output .= drupal_render_children($form);
$output .= theme('pager', array('#theme' => 'pager', '#weight' => 5,));
drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
return $output;
}

5. 最后一步,用hook_form_submit處理結(jié)果數(shù)據(jù)。

希望本文所述對大家的drupal二次開發(fā)有所幫助。

相關(guān)文章

最新評論