Drupal 6 : Table creation with Sorting and Pagination
$header = array(array('data'=>'Name','field'=>'name','style'=>'width:20%','sort'=>'desc'),
array('data'=>'Address','field'=>'addr','style'=>'width:30%'),
array('data'=>'Type Of Company','field'=>'typeofcompany','style'=>'width:10%'),
array('data'=>'District','field'=>'district','style'=>'width:10%'),
array('data'=>'Shift','field'=>'shift','style'=>'width:10%'),
array('data'=>'Location','field'=>'location','style'=>'width:20%'),
);
array('data'=>'Address','field'=>'addr','style'=>'width:30%'),
array('data'=>'Type Of Company','field'=>'typeofcompany','style'=>'width:10%'),
array('data'=>'District','field'=>'district','style'=>'width:10%'),
array('data'=>'Shift','field'=>'shift','style'=>'width:10%'),
array('data'=>'Location','field'=>'location','style'=>'width:20%'),
);
//$header is header for the table and (Name,Address are column name)(name,addr are table column names) //width is used to allocate the table column width for each column
$list_query = db_query("SELECT * FROM tbl_company".tablesort_sql($header));
//tbl_company is a table name and tblsort_sql for sorting the headers
while($list_data = db_fetch_object($list_query)){
$rows[] = array(array('data'=>$list_data->name),
array('data'=>$list_data->addr),
array('data'=>$list_data->typeofcompany),
array('data'=>$list_data->district),
array('data'=>$list_data->shift),
array('data'=>$list_data->location)
);
}
//We may have one or more values in table,so we can use while loop to fetch all the rows
$output .= theme(table,$header,$rows);
//theme(table,$header,$rows) is also written as table_theme($header,$rows)
$form['output'] = array('#value'=>$output);
//We will assign the output as form value
return $form;
For Pagination:
$list_query = db_query("SELECT * FROM tbl_company".tablesort_sql($header));
$count_query = "SELECT COUNT(*) FROM (" . $list_query . ") ";
$limit = 25;
$list_query_val = pager_query($query , $limit, 0, $count_query);
$count_query = "SELECT COUNT(*) FROM (" . $list_query . ") ";
$limit = 25;
$list_query_val = pager_query($query , $limit, 0, $count_query);
then we need to add the pager theme with the output as,
$output .= theme(table,$header,$rows);
$output .= theme('pager', NULL, $limit, 0);
//here our pager theme also rendered with the $output
$form['output'] = array('#value'=>$output);
$form['output'] = array('#value'=>$output);
Drupal 7 : Table creation with Sorting and Pagination
Header:
$header = array( array('data' => 'Title', 'field' => 'title', 'sort' => 'asc'), array('data' => 'Node ID', 'field' => 'nid'), array('data' => 'Type', 'field' => 'type'), array('data' => 'Created', 'field' => 'created'), array('data' => 'Published'), );
Sql Query:
$query = db_select('node', 'n')
->condition('status', 1) //Only published nodes,
//change condition as it suits you ->extend('PagerDefault') //Pager Extender ->limit(10) //10 results per page ->extend('TableSort') //Sorting Extender ->orderByHeader($header) //Field to sort on is picked from $header ->fields ('n', array ( 'nid', 'title', 'type', 'created', 'status', ));
Execute the Query:
$results = $query->execute();
Rows:
$rows = array();
foreach ($results as $node) {
$rows[] = array('data' => array(
l($node->title, 'node/'. $node->nid .'/edit'),
$node->nid,
$node->type,
format_date($node->created),
$node->status
)
); }
Table Theme:
$html = theme('table',
array('header' => $header, 'rows'=>$rows, 'caption' => 'Creating Drupal 7 style tables',
//Optional Caption for the table
'sticky' => TRUE,
//Optional to indicate whether the table headers should be sticky
'empty' => 'No nodes created...',
//Optional empty text for the table if resultset is empty
)
);
Pager:
$html .= theme('pager',array( 'tags' => array() ) );
return($html);
Some minor changes from drupal 6.
Other than that its same like drupal 6 table theming.
Thats it....
This comment has been removed by the author.
ReplyDeleteI am following the example I have a problem when running generates an error
ReplyDeletewhen I use return, but if you use print if it delivers results, any ideas, thanks
okay,,thanks,,
ReplyDeletecome again clearly,,,you are using drupal 6 or 7??
ReplyDelete