drupal创建拖动排序表格的方法
发布时间:2014-11-03 16:57:21 作者:佚名 我要评论
这篇文章主要为大家介绍了drupal创建拖动排序表格的方法,包括了具体的步骤与实现代码,具有一定的参考借鉴价值,需要的朋友可以参考下
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', ), );
}
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;
}
$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上面定义的表单。这里的关键
① 对每一行中用于存放顺序的weight字段,指定class
复制代码
代码如下:$form['nodes'][$count]['weight']['#attributes']['class'] = array('text-format-order-weight');
② theme table时指定table的ID:
复制代码
代码如下:$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));
③ 加上tabledrag需要的功能:
复制代码
代码如下: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;
}
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处理结果数据。
希望本文所述对大家的drupal二次开发有所帮助。
相关文章
- 真是不看不知道,Drupal 真奇妙。很多使用CMS内容管理系统的人可能都会知道一款国外的CMS系统:Drupal 。在我们传统的想象中CMS除了能做内容文章站外,其他还有图片站,分2010-01-24
- 对页面和静态资源的启用缓存和Gzip压缩传输.2010-01-24
- drupal模块开发分析,方便想要drupal模块开发的朋友2012-12-06
drupal 自定义表单调用autocomplete主标签实现代码
drupal 自定义表单调用autocomplete主标签实现代码,需要的朋友可以参考下2012-12-06- 这篇文章主要为大家介绍了drupal实现输出可点击表头排序表格的方法,包括了表的定义、SQL语句、表内容及生成HTML文件等,需要的朋友可以参考下2014-11-03
- 这篇文章主要为大家介绍了drupal文件系统,讲述了drupal文件系统的分类,重点讲述了钩子函数的用法,需要的朋友可以参考下2014-11-03
drupal模板(page.tpl)中的tabs无用户与密码的解决方法
这篇文章主要为大家介绍了drupal模板(page.tpl)中的tabs无用户与密码的解决方法,在drupal模板开发中具有一定的参考借鉴价值,需要的朋友可以参考下2014-11-03- 这篇文章主要为大家介绍了drupal按分类进行文章排序的实现方法,较为详细的分析了drupal文件组织结构与实现文章排序的方法,非常具有实用价值,需要的朋友可以参考下2014-11-03
drupal之hook_link和hook_link_alter钩子函数解析
这篇文章主要为大家介绍了drupal中hook_link和hook_link_alter钩子函数,实例分析了钩子函数的具体用法,具有一定的参考借鉴价值,需要的朋友可以参考下2014-11-03drupal使用hook_form_alter()修改表单实例
这篇文章主要为大家介绍了drupal使用hook_form_alter()修改表单的实现方法,包括了修改多个表单与修改特定的表单,非常实用,需要的朋友可以参考下2014-11-03
最新评论