对于Drupal 7使用jQuery Datatables模块

问题描述:

我是Drupal的新手,并且已经理解如何让我的基本块和菜单变得非常流畅。另外我学习了一些jQuery(它很棒)。对于Drupal 7使用jQuery Datatables模块

我想用户的分页列表,它只是a separate PHP script此刻融入my new Drupal 7 site

enter image description here

我想实现它作为一个Drupal的菜单,让我可以这样称呼它http://preferans.de/tophttp://preferans.de/top/100(已分页的偏移量为0)(显示的用户开始与100用户列表):

function pref_menu() { 
    $items['top'] = array(
    'title' => 'Weekly player rating', 
    'description' => 'Weekly player rating', 
    'page callback' => 'pref_top_callback', 
    'access callback' => TRUE, 
    'file' => 'pref.top.inc', 
    'file path' => drupal_get_path('module', 'pref'), 
    'type' => MENU_CALLBACK, 
); 

    return $items; 
} 

而且我非常SIMPL Ëpref.top.inc文件是:

function pref_top_callback($offset = 0) { 
    return array(
    'pref_players_table' => array(
     '#type' => 'markup', 
     '#markup' => pref_players_table($offset), 
    ), 
); 
} 

function pref_fetch_players($offset) { 
    /* FETCH MAX 20 RECORDS INTO AN ARRAY */ 
    $players = array(); 
    $result = db_query(" 
select u.id, 
     u.first_name, 
     row_number() OVER (order by m.money desc) as pos, 
     u.female, 
     u.city, 
     u.avatar, 
     m.money, 
     u.login > u.logout as online 
from pref_users u, pref_money m where 
     m.yw=to_char(current_timestamp, 'IYYY-IW') and 
     u.id=m.id 
order by m.money desc 
limit 20 offset :offset 
", array(':offset' => array($offset)), 
    array('fetch' => PDO::FETCH_ASSOC) 
); 
    $players = $result->fetchAll(); 

    /* PRINT THE ARRAY AS AN HMTL-TABLE */ 
    $table = '<table>'; 
    foreach ($players as $user) { 
    $table .= '<tr>'; 
    $table .= sprintf('<td>%u</td> 
<td><a href="/user.php?id=%s">%s</a></td> 
<td>%s</td><td>%d $</td>', 
     $user['pos'], 
     $user['id'], 
     $user['first_name'], 
     $user['city'], 
     $user['money']); 
    $table .= '</tr>'; 
    } 

    $table .= '</table>'; 
    return $table; 
} 

这并不工作,我得到一个Drupal页面含有多达20行的HTML表:

enter image description here

但我不知道怎么样使用the Datatables Drupal module。我已经成功下载并安装了它,并正在查看它的源代码,但不知道从哪里开始。

请帮助我,我怎么能从我的菜单功能调用它?

谢谢! Alex

该模块是一个Views插件,您需要将您的表暴露给视图才能使用它。

根据你的需要,你也可以得到一个只有Drupal核心的可排序的多页表格,请参阅https://drupal.stackexchange.com/questions/364/how-do-you-make-sortable-tables-with-a-pager-with-data-from-a-custom-table/367#367了解如何构建查询。

在你的代码的一些其他提示:

“$球员= $ result->使用fetchall();”是不必要的。您可以直接遍历$ result。

此外,你想使用主题('表')。然后,你只需要在这您的foreach钩:

<?php 
$rows = array(); 
$header = array(t('Position'), t('Id'), t('First name'), t('City'), t('Money')); 
foreach ($result as $player) { 
    $rows[] = array($player['pos'], $player['id'], $player['first_name'], $player['city'], $player['money']); 
} 
return theme('table', array('rows' => $rows, 'header' => $header)); 
?> 

此外,您将需要定义$头阵,即在theme_table()描述(参见字段和排序在$标题密钥)。

+0

对不起,但我不明白你的代码:你填充$ rows数组,但你通过/返回它? $ header是什么,可以设置为$ header = array('pos','id','first_name','city','money'); ? – 2011-03-23 14:04:16

+0

我也试过:$ header = array('pos','id','first_name','city','money'); $ rows = array(); foreach($ player为$ player){ $ rows [] = array($ player ['pos'],$ player ['id'],$ player ['first_name'],$ player ['city'], $播放器[ '钱']); } return theme('table',$ header,$ rows); 但得到一个没有桌子的白色屏幕 – 2011-03-23 14:09:16

+0

对不起,我有一个错字。显然,你需要将$行传递给'行'。我修正了它,并为$ header添加了一个示例。请注意,当您要使用TableSort时,您需要扩展$ header,请参阅http://api.drupal.org/api/drupal/modules--dblog--dblog.admin。inc/function/dblog_overview/7为例。 – Berdir 2011-03-23 17:08:55