【问题标题】:Using jQuery Datatables module for Drupal 7使用 Drupal 7 的 jQuery 数据表模块
【发布时间】:2011-03-22 18:21:35
【问题描述】:

我是 Drupal 新手,目前已经了解如何制作我的基本块和菜单。我还学了一些 jQuery(它很棒)。

我正在尝试将当前只是 a separate PHP script 的分页用户列表集成到 my new Drupal 7 site

我正在尝试将其实现为 Drupal 菜单,以便我可以将其称为 http://preferans.de/top(分页偏移量为 0)和 http://preferans.de/top/100(以显示从第 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;
}

我非常简单的 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 页面,其中的 HTML 表格最多包含 20 行:

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

请帮助我,如何从我的菜单功能中调用它?

谢谢! 亚历克斯

【问题讨论】:

    标签: jquery drupal drupal-modules drupal-7 datatables


    【解决方案1】:

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

    根据您的需要,您还可以获得仅使用 Drupal 核心的可排序的多页表,请参阅https://drupal.stackexchange.com/questions/364/how-do-you-make-sortable-tables-with-a-pager-with-data-from-a-custom-table/367#367,了解您需要如何构建查询来执行此操作。

    关于您的代码的其他一些提示:

    "$players = $result->fetchAll();"是不必要的。您可以直接迭代 $result。

    另外,你想使用主题('table')。然后,你只需要在你的 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));
    ?>
    

    此外,您还需要定义 $header 数组,如 theme_table() 中所述(请参阅 $header 中的字段和排序键)。

    【讨论】:

    • 抱歉,我不明白你的代码:你填充了 $rows 数组,但是你在哪里传递/返回它?而$header是什么,可以设置成$header = array('pos', 'id', 'first_name', 'city', 'money'); ?
    • 我也试过:$header = array('pos', 'id', 'first_name', 'city', 'money'); $rows = 数组(); foreach ($players as $player) { $rows[] = array($player['pos'], $player['id'], $player['first_name'], $player['city'], $player ['钱']); } 返回主题('table',$header,$rows);但得到一个没有桌子的白屏
    • 对不起,我有一个错字。显然,您需要将 $rows 传递给“行”。我修复了它并为 $header 添加了一个示例。注意,当你要使用TableSort时,需要扩展$header,例子见api.drupal.org/api/drupal/modules--dblog--dblog.admin.inc/…
    • 您在第二条评论中看到的是 D6 版本,在 D7 中,您将一组参数作为单个参数而不是多个参数传递给主题。
    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多