2012-09-12 2 views
2

저는 드루팔 (Drupal 7)에 모듈을 작성하고 있습니다. 콘텐츠 관리에서 테이블과 같은 테이블을 만들고 싶습니다. 이렇게 정렬 된 표를 만들 수 있지만 헤더와 각 행에 확인란을 어떻게 추가 할 수 있습니까? 문제는 테이블 형태처럼 초기화 할 수 있어야합니다,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; 

답변

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'), 
); 

어떤 방법. 정말 고맙습니다!!

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; 
} 
관련 문제