2014年7月10日星期四

Drupal theme_table order by field 不能正常使用

Drupal version 6.30


以下代码API地址
https://api.drupal.org/api/drupal/includes!tablesort.inc/function/tablesort_header/6
/**
 * Format a column header.
 *
 * If the cell in question is the column header for the current sort criterion,
 * it gets special formatting. All possible sort criteria become links.
 *
 * @param $cell
 *   The cell to format.
 * @param $header
 *   An array of column headers in the format described in theme_table().
 * @param $ts
 *   The current table sort context as returned from tablesort_init().
 * @return
 *   A properly formatted cell, ready for _theme_table_cell().
 */
function tablesort_header($cell, $header, $ts) {
  // Special formatting for the currently sorted column header.
  if (is_array($cell) && isset($cell['field'])) {
    $title = t('sort by @s', array('@s' => $cell['data']));
    if ($cell['data'] == $ts['name']) {
      $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
      if (isset($cell['class'])) {
        $cell['class'] .= ' active';
      }
      else {
        $cell['class'] = 'active';
      }
      $image = theme('tablesort_indicator', $ts['sort']);
    }
    else {
      // If the user clicks a different header, we want to sort ascending initially.
      $ts['sort'] = 'asc';
      $image = '';
    }

    if (!empty($ts['query_string'])) {

      $ts['query_string'] = '&'. $ts['query_string'];
    }
    $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], 'html' => TRUE));

    unset($cell['field'], $cell['sort']);

  }
  return $cell;
}


这里的   
$cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], 'html' => TRUE));

'&order='. urlencode($cell['data']) . $ts['query_string']

$cell['data']是header里面的data,而不是field。


不改核心代码的情况下解决办法
$order = trim($_GET['order']) ? trim($_GET['order']) : 'n.a';
$sort = trim($_GET['sort']) ? trim($_GET['sort']) : 'asc';

$header = array(
    array('data' => t('A1'), 'field' => 'n.a', 'sort' => 'asc'),
    array('data' => t('A2'), 'field' => 'n.b', 'sort' => 'asc'),
    array('data' => t('A3'), 'field' => 'n.c', 'sort' => 'asc'),
    array('data' => t('A4'), 'field' => 'n.d', 'sort' => 'asc'),
  );
  foreach ($header as $item) {
    if ($order == $item['data'] || $order == $item['field']) {
      $order = $item['field'];
    }
  }
  $sql .= " ORDER BY %s %s  ";
  $args[] = $order;
  $args[] = $sort;





没有评论:

发表评论

上海松善实业有限公司

    上海松善实业有限公司是一家集多品牌销售于一体的电线电缆骨干企业,公司成立于2016年。 公司拥有国内各大品牌:起帆、远东、上上、江南、胜华等。     主要产品有:高低压电力电缆、橡套电缆、控制电缆、架空绝缘电缆、塑胶电缆、电子计算机电缆、通讯电缆、...