如何在drupal 7中的有序表中添加复选框?

时间:2012-09-12 05:13:15

标签: drupal drupal-7 drupal-modules


我正在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;

2 个答案:

答案 0 :(得分: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'),
);

无论如何。非常感谢你!!

答案 1 :(得分: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;
}