📄 sortabletable.class.php
字号:
<?php/*============================================================================== Dokeos - elearning and course management software Copyright (c) 2005-2008 Dokeos S.A. Copyright (c) Bart Mollet (bart.mollet@hogent.be) For a full list of contributors, see "credits.txt". The full license can be read in "license.txt". This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the GNU General Public License for more details. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium Mail: info@dokeos.com==============================================================================*/require_once "HTML/Table.php"; //See http://pear.php.net/package/HTML_Tablerequire_once "Pager/Pager.php"; //See http://pear.php.net/package/Pagerrequire_once 'tablesort.lib.php';/** * This class allows you to display a sortable data-table. It is possible to * split the data in several pages. * Using this class you can: * - automatically create checkboxes of the first table column * - a "select all" and "deselect all" link is added * - only if you provide a list of actions for the selected items * - click on the table header to sort the data * - choose how many items you see per page * - navigate through all data-pages */class SortableTable extends HTML_Table{ /** * A name for this table */ var $table_name; /** * The page to display */ var $page_nr; /** * The column to sort the data */ var $column; /** * The sorting direction (ASC or DESC) */ var $direction; /** * Number of items to display per page */ var $per_page; /** * The default number of items to display per page */ var $default_items_per_page; /** * A prefix for the URL-parameters, can be used on pages with multiple * SortableTables */ var $param_prefix; /** * The pager object to split the data in several pages */ var $pager; /** * The total number of items in the table */ var $total_number_of_items; /** * The function to get the total number of items */ var $get_total_number_function; /** * The function to the the data to display */ var $get_data_function; /** * An array with defined column-filters */ var $column_filters; /** * A list of actions which will be available through a select list */ var $form_actions; /** * Additional parameters to pass in the URL */ var $additional_parameters; /** * Additional attributes for the th-tags */ var $th_attributes; /** * Additional attributes for the td-tags */ var $td_attributes; /** * Array with names of the other tables defined on the same page of this * table */ var $other_tables; /** * Create a new SortableTable * @param string $table_name A name for the table (default = 'table') * @param string $get_total_number_function A user defined function to get * the total number of items in the table * @param string $get_data_function A function to get the data to display on * the current page * @param int $default_column The default column on which the data should be * sorted * @param int $default_items_per_page The default number of items to show * on one page * @param string $default_order_direction The default order direction; * either the constant 'ASC' or 'DESC' */ function SortableTable($table_name = 'table', $get_total_number_function = null, $get_data_function = null, $default_column = 1, $default_items_per_page = 20, $default_order_direction = 'ASC') { parent :: HTML_Table(array ('class' => 'data_table')); $this->table_name = $table_name; $this->additional_parameters = array (); $this->param_prefix = $table_name.'_'; $this->page_nr = isset ($_SESSION[$this->param_prefix.'page_nr']) ? $_SESSION[$this->param_prefix.'page_nr'] : 1; $this->page_nr = isset ($_GET[$this->param_prefix.'page_nr']) ? $_GET[$this->param_prefix.'page_nr'] : $this->page_nr; $this->column = isset ($_SESSION[$this->param_prefix.'column']) ? $_SESSION[$this->param_prefix.'column'] : $default_column; $this->column = isset ($_GET[$this->param_prefix.'column']) ? $_GET[$this->param_prefix.'column'] : $this->column; $this->direction = isset ($_SESSION[$this->param_prefix.'direction']) ? $_SESSION[$this->param_prefix.'direction'] : $default_order_direction; $this->direction = isset ($_GET[$this->param_prefix.'direction']) ? $_GET[$this->param_prefix.'direction'] : $this->direction; $this->per_page = isset ($_SESSION[$this->param_prefix.'per_page']) ? $_SESSION[$this->param_prefix.'per_page'] : $default_items_per_page; $this->per_page = isset ($_GET[$this->param_prefix.'per_page']) ? $_GET[$this->param_prefix.'per_page'] : $this->per_page; $_SESSION[$this->param_prefix.'per_page'] = $this->per_page; $_SESSION[$this->param_prefix.'direction'] = $this->direction ; $_SESSION[$this->param_prefix.'page_nr'] = $this->page_nr; $_SESSION[$this->param_prefix.'column'] = $this->column; $this->pager = null; $this->default_items_per_page = $default_items_per_page; $this->total_number_of_items = -1; $this->get_total_number_function = $get_total_number_function; $this->get_data_function = $get_data_function; $this->column_filters = array (); $this->form_actions = array (); $this->checkbox_name = null; $this->td_attributes = array (); $this->th_attributes = array (); $this->other_tables = array(); } /** * Get the Pager object to split the showed data in several pages */ function get_pager() { if (is_null($this->pager)) { $total_number_of_items = $this->get_total_number_of_items(); $params['mode'] = 'Sliding'; $params['perPage'] = $this->per_page; $params['totalItems'] = $total_number_of_items; $params['urlVar'] = $this->param_prefix.'page_nr'; $params['currentPage'] = $this->page_nr; $params['prevImg'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/prev.png" style="vertical-align: middle;"/>'; $params['nextImg'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/next.png" style="vertical-align: middle;"/>'; $params['firstPageText'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/first.png" style="vertical-align: middle;"/>'; $params['lastPageText'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/last.png" style="vertical-align: middle;"/>'; $params['firstPagePre'] = ''; $params['lastPagePre'] = ''; $params['firstPagePost'] = ''; $params['lastPagePost'] = ''; $params['spacesBeforeSeparator'] = ''; $params['spacesAfterSeparator'] = ''; $query_vars = array_keys($_GET); $query_vars_needed = array ($this->param_prefix.'column', $this->param_prefix.'direction', $this->param_prefix.'per_page'); if (count($this->additional_parameters) > 0) { $query_vars_needed = array_merge($query_vars_needed, array_keys($this->additional_parameters)); } $query_vars_exclude = array_diff($query_vars, $query_vars_needed); $params['excludeVars'] = $query_vars_exclude; $this->pager = & Pager :: factory($params); } return $this->pager; } /** * Displays the table, complete with navigation buttons to browse through * the data-pages. */ function display() { global $charset; $empty_table = false; if ($this->get_total_number_of_items() == 0) { $cols = $this->getColCount(); $this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols); $this->setCellContents(1, 0, get_lang('TheListIsEmpty')); $empty_table = true; } if (!$empty_table) { $form = $this->get_page_select_form(); $nav = $this->get_navigation_html(); $html = '<table style="width:100%;">'; $html .= '<tr>'; $html .= '<td style="width:25%;">'; $html .= $form; $html .= '</td>'; $html .= '<td style="text-align:center;">'; $html .= $this->get_table_title(); $html .= '</td>'; $html .= '<td style="text-align:right;width:25%;">'; $html .= $nav; $html .= '</td>'; $html .= '</tr>'; $html .= '</table>'; if (count($this->form_actions) > 0) { $html .= '<script language="JavaScript" type="text/javascript"> /*<![CDATA[*/ function setCheckbox(value) { d = document.form_'.$this->table_name.'; for (i = 0; i < d.elements.length; i++) { if (d.elements[i].type == "checkbox") { d.elements[i].checked = value; } } } /*]]>*/ </script>'; $params = $this->get_sortable_table_param_string().'&'.$this->get_additional_url_paramstring(); $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">'; } } $html .= $this->get_table_html(); if (!$empty_table) { $html .= '<table style="width:100%;">'; $html .= '<tr>'; $html .= '<td colspan="2">'; if (count($this->form_actions) > 0) { $html .= '<a href="?'.$params.'&'.$this->param_prefix.'selectall=1" onclick="javascript:setCheckbox(true);return false;">'.get_lang('SelectAll').'</a> - '; $html .= '<a href="?'.$params.'" onclick="javascript:setCheckbox(false);return false;">'.get_lang('UnSelectAll').'</a> '; $html .= '<select name="action">'; foreach ($this->form_actions as $action => $label) { $html .= '<option value="'.$action.'">'.$label.'</option>'; } $html .= '</select>'; $html .= '<input type="submit" value="'.get_lang('Ok').'" onclick="javascript:if(!confirm('."'".addslashes(htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."'".')) return false;"/>'; } else { $html .= $form; } $html .= '</td>'; $html .= '<td style="text-align:right;">'; $html .= $nav; $html .= '</td>'; $html .= '</tr>'; $html .= '</table>'; if (count($this->form_actions) > 0) { $html .= '</form>'; } } echo $html; } /** * Get the HTML-code with the navigational buttons to browse through the * data-pages. */ function get_navigation_html() { $pager = $this->get_pager(); $pager_links = $pager->getLinks(); $showed_items = $pager->getOffsetByPageId(); $nav = $pager_links['first'].' '.$pager_links['back']; $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' '; $nav .= $pager_links['next'].' '.$pager_links['last']; return $nav; } /** * Get the HTML-code with the data-table. */ function get_table_html() { $pager = $this->get_pager(); $offset = $pager->getOffsetByPageId(); $from = $offset[0] - 1; $table_data = $this->get_table_data($from); foreach ($table_data as $index => $row) { $row = $this->filter_data($row); $this->addRow($row); } $this->altRowAttributes(0, array ('class' => 'row_odd'), array ('class' => 'row_even'), true); foreach ($this->th_attributes as $column => $attributes) { $this->setCellAttributes(0, $column, $attributes); } foreach ($this->td_attributes as $column => $attributes) { $this->setColAttributes($column, $attributes); } return $this->toHTML(); } /** * Get the HTML-code wich represents a form to select how many items a page * should contain. */ function get_page_select_form() { $total_number_of_items = $this->get_total_number_of_items(); if ($total_number_of_items <= $this->default_items_per_page) { return ''; } $result[] = '<form method="get" action="'.api_get_self().'" style="display:inline;">'; $param[$this->param_prefix.'direction'] = $this->direction; $param[$this->param_prefix.'page_nr'] = $this->page_nr; $param[$this->param_prefix.'column'] = $this->column; $param = array_merge($param, $this->additional_parameters); foreach ($param as $key => $value) { $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>'; } $result[] = '<select name="'.$this->param_prefix.'per_page" onchange="javascript:this.form.submit();">'; for ($nr = 10; $nr <= min(50, $total_number_of_items); $nr += 10) { $result[] = '<option value="'.$nr.'" '. ($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>'; } if ($total_number_of_items < 500) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -