有人可以帮我在同一页面上呈现表格和表格吗?我确信这很容易,但想不出怎么做。
这是hook_menu:
function ncbi_subsites_menu() {
$items = array();
$items['admin/content/ncbi_subsites'] = array(
'title' => 'NCBI Subsites Module',
'description' => 'Informs Drupal about NCBI subsites as defined by the Content Inventory database',
'page callback' => 'ncbi_subsites_show_main_page',
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
这是回调:
function ncbi_subsites_show_main_page() {
$subsites = ncbi_subsites_get_subsites_from_inventory(); // fnc returns associative array from inventory, defined in include
return ncbi_subsites_make_table( $subsites );
}
在回调中,我调用了一些返回主题分页表的辅助函数。
我想要的是桌子上方的一个小表格。我怎么样?
答案 0 :(得分:1)
我知道这是很久以前发布的,但也许有人可以使用一般更新的答案。最明智的方法是使用三个函数:一个用于页面回调,一个用于表单,一个用于表格。
/**
* Implements hook_form().
*/
function my_module_form($form, &$form_state) {
//form fields
return $form;
}
/**
* Function for table.
*/
function my_module_table() {
//Define $header
//Define $rows
return theme('table', array('header' => $header, 'rows' => $rows));
}
/**
* Page callback function for /my_module as defined in hook_menu().
*/
function _my_module() {
$page = drupal_render(drupal_get_form('my_module_form'));
$page .= my_module_table();
return $page;
}
答案 1 :(得分:0)
function ncbi_subsites_show_main_page() {
$subsites = ncbi_subsites_get_subsites_from_inventory();
$page = drupal_get_form('your_form');
$page .= ncbi_subsites_make_table( $subsites );
return $page;
}
答案 2 :(得分:-1)
这是一个完整的工作示例,用于在同一页面中显示表单和表格:
<?php
/*
* Implements hook_mennu()
*/
function test_menu() {
$items['test'] = array(
'title' => t('Test'),
'page callback' => 'test_search_page',
'access callback' => True,
);
return $items;
}
function test_search_page(){
$form = drupal_get_form('test_search_form');
return $form;
}
function test_search_form($form, &$form_state){
$header = array(t('id'), t('name'), t('firstname'));
$rows = Null;
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => True,
'#default_value' => isset($_GET['name']) ? $_GET['name'] : Null
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('submit'),
);
if (isset($_GET['name'])){
$rows = get_data();
}
$form['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('Aucun résultat.')
);
$form['pager'] = array('#markup' => theme('pager'));
/*
if (isset($form_state['table'])) {
$form['table'] = $form_state['table'];
}
$form['pager'] = array('#markup' => theme('pager'));
*/
return $form;
}
function test_search_form_submit($form, &$form_state){
$form_state['redirect'] = array(
// $path
'test',
// $options
array('query' => array('name' => $form_state['values']['name'])),
// $http_response_code
302,
);
}
//$header = array(t('id'), t('name'), t('firstname'));
function get_data(){
$data = array(
0 => array(
'id' => '0',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
1 => array(
'id' => '1',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
2 => array(
'id' => '2',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
3 => array(
'id' => '3',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
4 => array(
'id' => '4',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
5 => array(
'id' => '5',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
6 => array(
'id' => '6',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
7 => array(
'id' => '7',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
8 => array(
'id' => '8',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
9 => array(
'id' => '9',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
10 => array(
'id' => '10',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
11 => array(
'id' => '11',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
)
);
$paging = pager_array_splice($data, 2);
return $paging;
}
/*
$header = array(t('id'), t('name'), t('firstname'));
$form_state['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $paging,
'#empty' => t('Aucun r?sultat.')
);
$form_state['rebuild'] = True;*/
function pager_array_splice($data, $limit = 9, $element = 0) {
global $pager_page_array, $pager_total, $pager_total_items;
$page = isset($_GET['page']) ? $_GET['page'] : '';
// Convert comma-separated $page to an array, used by other functions.
$pager_page_array = explode(',', $page);
// We calculate the total of pages as ceil(items / limit).
$pager_total_items[$element] = count($data);
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
return array_slice($data, $pager_page_array[$element] * $limit, $limit, TRUE);
}