这是同一页面上的页面和列表的完整工作示例
<?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);
}