普通网友 2015-06-19 21:29
浏览 24

Drupal提交更改tableselect的查询

I'm new on drupal, i've create a module that shows a form with select, button submit and i've got a tableselect that lists some records (from database). the tableselect works and it's listing all records, but i want to "filter" records on this tableselect by selecting what i want to see with select list (and submitting). when i choose something in the select list and i submit, the tableselect dont change but if i execute a dsm($form['tableselect']) it said that tableselect contains what the db_query returns.

here is my form :

 function myBook_form ($form, &$form_state){

  $form = array();
  $form['options_state'] = array(
  '#type' => 'value',
  '#value' => array (
    'all'=>t('All'),
    'Valid'=>t('Valid'),
    'published'=>t('published'),
    'not published'=>t('not published')

    )

  );


 $form['state_book'] = array(
'#type' => 'select',
'#title' => t('state :'),                     
'#options' => $form['options_state']['#value'],        
 );


  // filter submit button

 $form['filter'] = array(
'#type' => 'submit',
'#value' => t('filter')                
);
}


$header  = array(
'book_title' => t('title'),
'book_state' => t('state'),   
  );

$sql = db_select('field_data_field_title','ta');
$sql->join('field_data_field_state','st','st.entity_id = ta.entity_id');


$sql
    ->fields('ta',   array('field_title_value','entity_id'))       
    ->fields('st',array('field_state_value'));
    $result = $sql->execute();


$rows = array();



foreach ($result as $res){ 

  $rows [] = array(
    'book_title' => l($res->field_title_value,  'node/'.$res->entity_id),
    'book_state' => $res->field_state_value           
  );  

 }

 $form['table1'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#empty' => t('empty !'),

  );


 return $form;

here is my submit function :

function book_form_submit($form, &$form_state){



 $cond = $form_state['values']['state_book'];


$header  = array(
'book_title' => t('title'),
'book_state' => t('state'),
 );

   $sql = db_select('field_data_field_title','ta');
   $sql->join('field_data_field_state','st','st.entity_id = ta.entity_id');

  $sql
    ->fields('ta', array('field_title_value'))        
    ->fields('st',array('field_state_value'));


  $sql->condition('st.field_state_value', $cond, '=');       



 $quer = $sql->execute();


$rows = array();

 foreach ($quer as $q){ 

    array_push($rows, array(
    'book_title' => $q->field_title_value,
    'book_state' => $q->field_state_value,

      ));

      }    
    $form['table2'] = array(
   '#type' => 'tableselect',
   '#header' => $header,
   '#options' => $rows,
   '#empty' => t('empty!')
  );   
   }

Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计