2

私はdrupal7でモジュールを書いていますが、コンテンツ管理でテーブルのようなテーブルを作成したいと思います。このように並べ替えられたテーブルを作成できますが、ヘッダーと各行にチェックボックスを追加するにはどうすればよいですか?

これが私のコードです:

 $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

You can use #type 'tableselect' to add a checkbox in each row including header. Your code with following few changes may work out fine,

 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 に答える