在 Drupal 7 中使用 jQuery 数据表模块

发布于 2024-10-24 15:36:50 字数 2633 浏览 1 评论 0原文

我是 Drupal 新手,到目前为止已经了解如何制作基本块和菜单。我还学习了一些 jQuery(非常棒)。

我正在尝试将分页的用户列表集成到其中,该列表目前只是 一个单独的 PHP 脚本 我的新 Drupal 7 网站

在此处输入图像描述

我正在尝试将其实现为 Drupal 菜单,以便我可以像 http 那样调用它://preferans.de/top(分页偏移量为 0)和 http:// Preferred.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;
}

这确实有效,我得到一个带有 HTML 表格的 Drupal 页面最多包含 20 行:

在此处输入图像描述

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

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

谢谢你! 亚历克斯

I'm a Drupal newbie and have understood how to make my basic blocks and menus sofar. Also I've learned some jQuery (and it is awesome).

I'm trying to integrate a paginated list of users which is just a separate PHP script at the moment into my new Drupal 7 site:

enter image description here

I'm trying to implement it as a Drupal menu, so that I can call it like http://preferans.de/top (to have the pagination offset of 0) and http://preferans.de/top/100 (to display the list of users starting with the 100th user):

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;
}

And my very simple pref.top.inc file is:

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 
lt;/td>',
      $user['pos'],
      $user['id'],
      $user['first_name'],
      $user['city'],
      $user['money']);
    $table .= '</tr>';
  }

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

This does work, I get a Drupal page with an HTML table containing up to 20 rows:

enter image description here

But I have no idea how to use the Datatables Drupal module. I've downloaded and installed it successfully and am looking at its source code, but don't event know where to start.

Please help me, how can I call it from my menu function?

Thank you!
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-10-31 15:36:50

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

根据您的需要,您还可以获得仅包含 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 中的字段和排序键)。

That module is a Views plugin, you will need to expose your table to views to be able to use it.

Depending what you want, you can also get a sortable, multi-page table with just Drupal core, see https://drupal.stackexchange.com/questions/364/how-do-you-make-sortable-tables-with-a-pager-with-data-from-a-custom-table/367#367 on how you need to build your query to do that.

Some other hints on your code:

"$players = $result->fetchAll();" is unecessary. You can directly iterate over $result.

Also, you want to use theme('table'). Then, you just need this in your foreach hook:

<?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));
?>

Additionally, you will need to define the $header array, that is described in theme_table() (see field and sort keys in $header).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文