thene_tableselect 表格式選取表單(form)

1 篇文章 / 0 new
author
thene_tableselect 表格式選取表單(form)
  一般常有表格顯示要樣式, 然後最前頭有勾選項目供點選的表單處理, theme_tableselect 就是提供這樣一個模式, 其參數用法與theme_table 幾乎是一樣的.
  第一欄的 checkbox 是系統自動加入, 但沒有提供變數供樣式設定, 因此該欄位寬度常會過長不美觀, 若要變更寬度則需修改 form.inctheme_tableselect()[3296] 方式來調整, 主要是增加 class 然後透過 css 來指定
array_unshift($header, array('class' => array('select-all')));
變成 array_unshift($header, array('class' => array('select-all checkbox-width')));
array_unshift($header, '');
變成 array_unshift($header, array('class' => array('checkbox-width')));

範例1
function selectForm($form, $form_state) {
    drupal_add_css('th.checkbox-width { width:15px; }', array('type' => 'inline') );
    //drupal_add_css(drupal_get_path('module', 'themetable') . '/tableselect.css');
    $header = array(
        'c1' => array('data' => ('Title'), 'field'=>'n.title' ), //使用 TableSort 必須指定 field 資料
        'c2' => array('data' => ('Update'), 'style'=>'width:120px'),
        'c3' => array('data' => ('Status'), 'style'=>'width:40px' ),
    );
    $query = db_select('node', 'n')
            ->fields('n', array('nid', 'title', 'changed','status'))
            ->extend('PagerDefault')->limit(4)
            ->extend('TableSort')->orderByHeader($header)
            ->execute()->fetchAll();
    $rows = array();
    foreach ($query as $record) {// dataSet
        $rows[$record->nid] = array(// celles       
            'c1' => array( 'data' => l($record->title, 'node/' . $record->nid), ),
            'c2' => array( 'data' => format_date($record->changed, 'short') ),//date('Y-m-d h:i:s', $record->changed), ),
            'c3' => array( 'data' => $record->status, ),
            //'#attributes' => array('style' => array('width: 70px')),//  這屬性為 tr 非 td 屬性
        );
    }    
    $form['table'] = array(
        '#type' => 'tableselect',
        '#header' => $header,
        '#options' => $rows,//$options,
        //'#multiple' => FALSE, //變成 RadioBox
        //'#js_select' => FALSE, //取消全選功能
        '#empty' => ('No content available.'),
    );
    $form['pager'] = array('#markup' => theme('pager'));
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => ('Submit'),
    );
    return $form;
}
範例2
function selectForm($form, $form_state) {
    $header = array(
        'nid' => t('nid'),
        'title' => t('title'),
        'type' => t('type'),
        'created' => t('created'),
        'status' => t('status'),
    );
    $results = db_select('node', 'n')
            ->extend('PagerDefault')->limit(5)  //分頁, 每頁兩筆
            ->extend('TableSort')->orderByHeader($header)  //欄位排序啟動
            ->fields('n', array('nid','title','type','created','status',) )
            ->execute();
    $rows = array();
    foreach ($results as $node) {
        $rows[$node->nid] = array(
            'title' => l($node->title, 'node/' . $node->nid),
            'nid' => $node->nid,
            'type' => $node->type,
            'created' => format_date($node->created),
            'status' => $node->status
        );
    }
    $form['registration_table'] = array(
        '#type' => 'tableselect',
        '#header' => $header,
        '#options' => $rows,
        '#js_select' => FALSE,
        '#multiple' => TRUE,
        '#prefix' => '<table><tr><td>',
        '#suffix' => '</td></tr>',
    );
    $form['submit'] = array('#type' => 'submit',
        '#value' => t('Register'),
        '#disabled' => FALSE,
        '#prefix' => '<tr><td>',
        '#suffix' => '</td></tr></table>');
    $form['pager'] = array('#markup' => theme('pager')); // 分頁選單
    return $form;
}
Free Web Hosting