2

我正在drupal 7中编写一个模块,我想在内容管理中制作一个类似表格的表格。我可以制作这样的排序表,但是如何在标题和每一行中添加复选框?

这是我的代码:

 $header = array(
    array('data' => 'Title', 'field' => 'title'),
    array('data' => 'Created', 'field' => 'created','sort' => 'desc'),
    array('data' => 'Published', 'field' => 'status'),
    array('data' => 'Action'),
);

$result = db_select('news','n')->extend('PagerDefault')
    ->fields('n')
    ->limit(10) //This is we can change the number of rows
    ->extend('TableSort') //    Sorting Extender
    ->orderByHeader($header)//  Field to sort on is picked from $header
    ->execute()->fetchAll();

$path = drupal_get_path("module","tuan_nguyen");

foreach($result as $row){
    $img = $path."/del.png";
    if($row->status == 1){
        $img = $path."/check.png";
    }
    $date = format_date($row->created,'medium','','Asia/Ho_Chi_Minh');
    $rows[$row->id] = array(
        l($row->title,'admin/tuan_nguyen/news/edit/'.$row->id),
        $date,
        "<img width='30px' height='30px' src='".$img."'/>",
        l('Edit','admin/tuan_nguyen/news/edit/'.$row->id).' / '.l('Del','admin/tuan_nguyen/news/del/'.$row->id),
    );
}

//Create a render array ($build) which will be themed as a table with a pager
$build['tuan_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' =>t('Table is empty'),
);
//attach the pager theme
$build['tuan_pager'] = array('#theme' => 'pager');
return $build;
4

2 回答 2

0

您可以使用#type 'tableselect' 在每一行中添加一个复选框,包括标题。您的代码进行了以下几处更改可能会正常工作,

 function your_form($form,$form_state){
    $header = array(
        array('data' => 'Title', 'field' => 'title'),
        array('data' => 'Created', 'field' => 'created','sort' => 'desc'),
        array('data' => 'Published', 'field' => 'status'),
        array('data' => 'Action'),
    );

    $result = db_select('news','n')->extend('PagerDefault')
        ->fields('n')
        ->limit(10) //This is we can change the number of rows
        ->extend('TableSort') //    Sorting Extender
        ->orderByHeader($header)//  Field to sort on is picked from $header
        ->execute()->fetchAll();

    $path = drupal_get_path("module","tuan_nguyen");
    $options = array();
    foreach($result as $row){
        $img = $path."/del.png";
        if($row->status == 1){
            $img = $path."/check.png";
        }
        $date = format_date($row->created,'medium','','Asia/Ho_Chi_Minh');
        $rows = array();
        $rows[] = array(
              l($row->title,'admin/tuan_nguyen/news/edit/'.$row->id),
              $date,
              "<img width='30px' height='30px' src='".$img."'/>",              l('Edit','admin/tuan_nguyen/news/edit/'.$row->id).'  / '.l('Del','admin/tuan_nguyen/news/del/'.$row->id),

        );
        $options[$row->id] = $rows;
    }

    //Create a render array ($build) which will be themed as a table with a pager
    $form['tuan_table'] = array(
        '#type' => 'tableselect',
        '#header' => $header,
        '#options' => $options,
        '#empty' =>t('Table is empty'),
    );
    //attach the pager theme
    $build['tuan_pager'] = array('#theme' => 'pager');
    return $form;
}
于 2013-11-22T11:42:17.813 回答
0

我做到了,问题是表必须像表单一样初始化。hook_menu() 中的代码如下:

$items['admin/tuan_nguyen/news'] = array(
    'title' => 'Management news',
    'page callback' => 'drupal_get_form', // <----I missed here
    'page arguments' => array("tuan_nguyen_manage_news"),
);

函数“tuan_nguyen_namage_news”中的表格如下:

$form['tuan_table'] = array(
    '#type' => 'tableselect', // <--'#type', not '#theme'
    '#header' => $header,
    '#options' => $options, // <-- '#options' not '#rows'
    '#empty' =>t('Table is empty'),
);

反正。太感谢了!!

于 2012-09-13T02:39:37.120 回答