[Drupal] Drupal7 - How to theme a table with pager.

本文提供了一个使用 Drupal 7 构建带有分页功能的表格的示例代码。该示例展示了如何通过 SQL 查询从数据库中获取数据,并使用 Drupal 的内置组件进行排序和分页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As we know, Drupal7 has changed a lot, and we have to be familiar with it.

 

Here is an example to theme a table with pager.

 

  $output = '';
  
$header = array(
    
array(
      
'data'=>t('Title'),
      
'field'=>'n.title',
    )
,
    
array(
      
'data' => '',
    )
,
  );

  
$query = db_select('node', 'n')->extend('PagerDefault')->extend('TableSort');
  
$query->fields('n', array('nid', 'title'));
  
$ua_alias = $query->leftJoin('users_act' ,'ua', '%alias.nid = n.nid');
  
$query->addField($ua_alias, 'uid', 'ua_uid');
  
$query->addField($ua_alias, 'status', 'ua_status');
  
$query->condition('n.type', 'activity');
  
$objects = $query->limit(50)
    
->orderByHeader($header)
    
->execute()
    
->fetchAll();
  
$rows = array();
  
foreach ($objects as $key=>$object) {
    
$row = array();
    
$nid = $object->nid;
    
$row[] = l(check_plain($object->title), 'node/'.$object->nid, array('attributes'=>array('target'=>'_blank')));
    
$row[] = '<a href="?nid='.$nid.'" title='.$object->title.' target="_blank">my link</a>';
    
$rows[] = $row;
  }
  
$output .= theme('table', array('header'=>$header, 'rows'=>$rows, 'empty'=>t('No result found.')));
  
$output .= theme('pager');

  return $output; 

 

It is just an example, any details, please see in drupal.org

 

Have fun with Drupal7! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值