HomeHelpTrac

Changeset 15685 for trunk/inc


Ignore:
Timestamp:
11/20/11 22:31:38 (6 months ago)
Author:
Alexander Trofimov
Message:

Grid - docs and samples

Location:
trunk/inc
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/inc/classes/BxDolGrid.php

    r15674 r15685  
    1616 
    1717/** 
    18  * Grid for any content 
    19  * TODO: doc 
     18 * Grid for any content.  
     19 * 
     20 * It allows to display some data as grid with ready to use features: 
     21 * - paginate 
     22 * - reordering 
     23 * - sorting 
     24 * - search 
     25 * - actions 
     26 * 
     27 * The advantages of the this system:  
     28 * - Less code to write - so you can concentrate on the main functionality. 
     29 * - Flexibility - you can turn on/off ready features or override it for the custom behavior. 
     30 * 
     31 * Grid is working together with Paginate to look through the data in the grid. @see BxDolPaginate 
     32 *  
     33 * @section grid_create Creating the new Grid object: 
     34 * 
     35 * 1. add record to 'sys_objects_grid' table: 
     36 * 
     37 * - object: name of the grid object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; for example: bx_profiles_admin - to display profiles in admin panel. 
     38 * - source_type: type of the source data: 
     39 *      - Sql: the source is SQL query. 
     40 *      - Array: the source is serialized array. 
     41 * - source: the source data, different for each 'source_type': 
     42 *      - Sql: the SQL query string, without ORDER BY and LIMIT clauses, these clauses are added automatically for sorting, pagination and filtering. 
     43 *      - Array: 2 dimentional serialized array string. 
     44 * - table: table name (if 'source_type' is 'Sql'), to automatically update order field and delete records. 
     45 * - field_id: name of the ID field. 
     46 * - field_order: name of the order field. 
     47 * - paginate_url: URL of the page for the grid, set it empty for AJAX paginate, or specify the url for regular page reloading in paginate, with the following markers: 
     48 *      - {start}: starting record to show data from. 
     49 *      - {per_page}: number of records per one page. 
     50 * - paginate_per_page: number of records per one page. 
     51 * - paginate_simple: show full or simple paginate, the following values are supported: 
     52 *      - NULL: show full(big) paginate. 
     53 *      - ''(empty string): show simple(small) paginate. 
     54 *      - 'some url': show simple(small) paginate with "View All" link to the specified URL. 
     55 * - paginate_get_start: GET variable name for 'start'. 
     56 * - paginate_get_per_page: GET variable name for 'per_page'. 
     57 * - filter_fields: comma separated field names, to search in. 
     58 * - filter_mode: search mode: 
     59 *      - like: use SQL LIKE expression for search, if 'source_type' is not 'Sql' it doesn't matter. 
     60 *      - fulltext: use MATCH ... AGAINST expression for search, if 'source_type' is not 'Sql' it doesn't matter. 
     61 *      - auto: use 'like' or 'fulltext', depending on 'useLikeOperator' setting option. 
     62 * - sorting_fields: comma separated field names, which will be allowed for sorting. 
     63 * - override_class_name: user defined class name which is derived from BxTemplGrid. 
     64 * - override_class_file: the location of the user defined class. 
     65 * 
     66 * 2. Specify field names (columns in the grid) in sys_grid_fields table: 
     67 * 
     68 * - object: name of the Grid object. 
     69 * - name: name of the field, it must refer to the SQL field name in the case of 'Sql' 'source_type' or index of the 2 dimentional array in the case of 'Array' 'source_type'. 
     70 * - title: title of the field, the language key. 
     71 * - width: width of the column in % or px, pt, etc. 
     72 * - params: searialized array of additional params: 
     73 *      - display: display function from BxDolFormCheckerHelper class, for example to convert unix timestamp to the regular date/time string. 
     74 *      - attr_cell: tag attributes for the data cell. 
     75 *      - attr_head: tag attributes for the header cell. 
     76 * - order: order of the field. 
     77 *  
     78 * There are some fields which are always available, additionally to the provided set of fields: 
     79 *  
     80 * - order: display column as dragable handle, it makes sense if you have data ordered by some field  
     81 *          and it is specified in field_order, field_id and table fields; reordering is not correctly  
     82 *          working with paginate, so make sure that paginate_per_page number is big enough to show all records;  
     83 *          reordering is working with Sql source_type. 
     84 * - checkbox: display column with checkboxes, so several records can be selected for bulk action;  
     85 *          you need to specify field_id field, so every checkbox have unique row id;  
     86 *          you need to specify bulk actions separately in sys_grid_actions table. 
     87 * - actions: display column with single actions, displayed as buttons; you need to specify field_id field,  
     88 *          so every action is provided with unique row id; you need to specify single actions separately in sys_grid_actions table. 
     89 *   
     90 * 3. Add actions to sys_grid_actions table: 
     91 *  
     92 * - object: name of the Grid object. 
     93 * - type: action type, one of the following: 
     94 *      - bulk: bulk action, to perform on the set of records, the action is usually displaed below the grid. 
     95 *      - single: simple action, to perform on one record, the action is usually displayed in the grid row. 
     96 *      - independent: independent actionm which is not related to any rowm the action is usually displayed above the grid. 
     97 * - name: action name. 
     98 * - title: title of the action, the language key. 
     99 * - confirm: ask confirmation before performing the action, 0 or 1. 
     100 * - order: order of the action in particular actions set by type. 
     101 *  
     102 * Usually you need to handle actions manually, but there are several actions which are available by default: 
     103 *  
     104 * - delete: delete the record, it works automatically when 'source_type' is 'Sql' and 'field_id', 'table' fields are specified. 
     105 * 
     106 * 
     107 * 
     108 * @section grid_display_custom_cell Displaying custom cell 
     109 *  
     110 * Cell is displayed with default design. It is possible to easily customize its design by specifying custom attributes as 'attr_cell' in params field in sys_grid_fields table.  
     111 * 
     112 * If it is not enough, you can customize it even more by adding the method to your custom class with the following format:  
     113 * _getCell[field name] 
     114 * where [field name] is the name of the field you want to have custom look with the capital first letter.  
     115 * 
     116 * For example: 
     117 * 
     118 * @code 
     119 * protected function _getCellStatus ($mixedValue, $sKey, $aField, $aRow) {         
     120 *  
     121 *     $sAttr = $this->_convertAttrs( 
     122 *         $aField, 'attr_cell', 
     123 *         false,  
     124 *         isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles 
     125 *     ); 
     126 *     return '<td ' . $sAttr . '><span style="background-color:' . ('Active' == $mixedValue ? '#cfc' : '#fcc') . '">' . $mixedValue . '</span></td>'; 
     127 * }  
     128 * @endcode 
     129 *   
     130 * Above example is displaying user's status using different colors depending on the status value. Please note that you need to convert attributes by adding some default classes or styles if you need. 
     131 * 
     132 * 
     133 * 
     134 * @section grid_display_custom_header Displaying custom column header 
     135 *  
     136 * This is working similar to displaying custom cell. It easily customize its design by specifying custom attributes as 'attr_head' in params field in sys_grid_fields table. 
     137 * If it is not enough, you can customize it even more by adding the method to your custom class with the following format:  
     138 * _getCellHeader[field name] 
     139 * where [field name] is the name of the field you want to have custom look with the capital first letter.  
     140 *  
     141 * For example: 
     142 * 
     143 * @code 
     144 * protected function _getCellHeaderStatus ($sKey, $aField) {  
     145 *     $s = parent::_getCellHeaderDefault($sKey, $aField); 
     146 *     return preg_replace ('/<th(.*?)>(.*?)<\/th>/', '<th$1><img src="' . BxDolTemplate::getInstance()->getIconUrl('user.png') . '"></th>', $s); 
     147 * } 
     148 * @endcode 
     149 *  
     150 * The above example replaces column header text with the image.  
     151 * 
     152 * 
     153 * 
     154 * @section grid_display_custom_action Displaying custom action 
     155 *  
     156 * All actions are displayed as buttons. Bulk and independent actions are displaed as big buttons and single actions are displayed as small buttons. 
     157 *  
     158 * It is possible to completely customize it by adding the following method to your custom class: 
     159 * _getAction[action name] 
     160 * where [action name] is the action name with the capital first letter.  
     161 *  
     162 * For example: 
     163 * 
     164 * @code 
     165 * protected function _getActionCustom1 ($sType, $sKey, $a, $isSmall = false) { 
     166 *     $sAttr = $this->_convertAttrs( 
     167 *         $a, 'attr', 
     168 *         'bx-btn bx-def-margin-sec-left' . ($isSmall ? ' bx-btn-small' : '') // add default classes 
     169 *     ); 
     170 *     return '<button ' . $sAttr . ' onclick="$(this).off(); alert(\'default behaviour is overrided, so the action is not performed\');">' . $a['title'] . '</button>'; 
     171 * } 
     172 * @endcode 
     173 *  
     174 * The above example disables default onclick event and just displays an alert. Please note that you need to convert attributes by adding some default classes or styles if you need. 
     175 * 
     176 *  
     177 *  
     178 * @section grid_add_action_handler Add action handler 
     179 * 
     180 * As it was mentioned earlier only several actions can be handled automatically, all other actions must be processed manually.  
     181 * To add action handler you need to add method to your custom class with the following format:  
     182 * performAction[action name] 
     183 * where [action name] is the action name with the capital first letter.  
     184 * 
     185 * For example: 
     186 * 
     187 * @code 
     188 * public function performActionApprove() { 
     189 *  
     190 *     $iAffected = 0; 
     191 *     $aIds = bx_get('ids'); 
     192 *     if (!$aIds || !is_array($aIds)) { 
     193 *         $this->_echoResultJson(array()); 
     194 *         exit; 
     195 *     } 
     196 *  
     197 *     $aIdsAffected = array (); 
     198 *     foreach ($aIds as $mixedId) { 
     199 *         if (!$this->_approve($mixedId)) 
     200 *             continue; 
     201 *         $aIdsAffected[] = $mixedId; 
     202 *         $iAffected++; 
     203 *     } 
     204 *  
     205 *     $this->_echoResultJson(array( 
     206 *         'msg' => $iAffected > 0 ? sprintf("%d profiles successfully activated", $iAffected) : "Profile(s) activation failed",  
     207 *         'grid' => $this->getCode(false), 
     208 *         'blink' => $aIdsAffected, 
     209 *     )); 
     210 * } 
     211 *  
     212 * protected function _approve ($mixedId) { 
     213 *     $oDb = BxDolDb::getInstance(); 
     214 *     $sTable = $this->_aOptions['table']; 
     215 *     $sFieldId = $this->_aOptions['field_id']; 
     216 *     $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `Status` = 'Active' WHERE `{$sFieldId}` = ?", $mixedId); 
     217 *     return $oDb->query($sQuery); 
     218 * } 
     219 * @endcode 
     220 *  
     221 * The action can be used as 'single' or 'bulk', in the case of 'single' action 'ids' array always has one element. 
     222 *  
     223 * As the result, action must outputs JSON array, which is done by _echoResultJson function.  
     224 * The defined indexes in the array determines behavior after action is performed, the following behaviors are supported: 
     225 *  
     226 * - msg: display javascript alert message. 
     227 * - grid: reload grid data with the provided HTML code. 
     228 * - popup: display popup with the provided HTML code. 
     229 * - blink: highlight(blink effect) the specified rows, by the ids. 
     230 *  
    20231 */ 
    21  
    22                 /** 
    23                  * Source, different depending on source_type: 
    24                  * Array - array of arrays. 
    25                  * Sql - SQL query without LIMIT part, it is added automatically. 
    26                  */ 
    27 /* 
    28                 'object' => 'id' . rand(1000000, 9999999), 
    29                 'source' => array(),   
    30                 'source_type' => 'Array', // Array, Sql 
    31                 'fields' => array (), // array of fields to display width some options 
    32                 'field_id' => false, // name of id field 
    33                 'field_order' => false, // name of order field 
    34                 'actions_bulk' => array (), // available bulk actions for the diplayed data, standard action is delete 
    35                 'actions_single' => array (), // available single actions for the diplayed data, standard action is delete 
    36                 'actions_independent' => array (), // available independent actions  
    37                 'form_add' => false, // add data form object 
    38                 'form_edit' => false, // edit data form object 
    39                 'paginate' => false, // array of paginate options 
    40                 'paginate_position' => false, // array of paginate positions(top, bottom) or false to position it by default (bottom) 
    41                 'paginate_simple' => false, // show smaller, simpler paginate; set to non false value to start showing simple paginate 
    42                 'filter' => array(), // filter options: fields and mode (like or fulltext); by default filter is empty array - not shown 
    43  
    44 */ 
    45  
    46  
    47 /* 
    48  
    49         'id' => 'sample', 
    50         'source' => $aAll,   
    51         'source_type' => 'Array', // Array, Sql 
    52         'fields' => array ( // array of fields to display width some options 
    53             'order' => array('title' => '', 'width' => '1%'), 
    54             'checkbox' => array('title' => 'Select', 'width' => '2%'), 
    55             'name' => array('title' => 'Name', 'width' => '25%'), 
    56             'value' => array('title' => 'Value', 'width' => '15%', 'display' => 'DateTime', 'attr_cell' => array('style' => 'text-align:center'), 'attr_head' => array('style' => 'text-align:center')),  
    57             'caption' => array('title' => 'Caption', 'width' => '42%'), 
    58             'actions' => array('title' => 'Actions', 'width' => '15%'), 
    59         ),  
    60         'field_id' => 'id', // name of id field 
    61         'field_order' => 'order', // name of order field 
    62         'actions_bulk' => array ( // available bulk actions for the diplayed data, standard action is delete 
    63             'delete' => array ('title' => _t('_Delete')), 
    64             'pause' => array ('title' => _t('_Approve')), 
    65             'div1' => array (), 
    66             'custom1' => array ('title' => _t('_Custom1')), 
    67             'custom2' => array ('title' => _t('_Custom2')), 
    68         ),  
    69         'actions_single' => array ( // available single actions for the diplayed data, standard action is delete 
    70             'delete' => array ('title' => _t('_Delete')), 
    71             'edit' => array ('title' => _t('_Edit')), 
    72         ), 
    73         'actions_independent' => array ( // available independent actions  
    74             'add' => array ('title' => _t('_Add record')), 
    75         ), 
    76         'form_add' => false, // add data form object 
    77         'form_edit' => false, // edit data form object 
    78         'paginate' => array( // array of paginate options 
    79             'page_url' => BX_DOL_URL_ROOT . 'samples/grid.php?start={start}', 
    80             'per_page' => 10, 
    81         ), 
    82         'paginate_simple' => false, // show smaller, simpler paginate; set to non false value to start showing simple paginate 
    83         'filter' => array( // filter options, by default filter is not shown 
    84             'fields' => array('name', 'value', 'caption'), 
    85             'mode' => 'like', // like or fulltext 
    86         ), 
    87  
    88 */ 
    89232 
    90233class BxDolGrid extends BxDol { 
     
    126269            require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']); 
    127270        $o = new $sClass($aObject); 
    128  
    129         // TODO: check some setting for incompatibility, like: 
    130         // soring + reordering 
    131         // paginate + reordering 
    132271 
    133272        return ($GLOBALS['bxDolClasses']['BxDolGrid!'.$sObject] = $o); 
  • trunk/inc/classes/BxDolGridQuery.php

    r15674 r15685  
    1 <?php 
     1<?php defined('BX_DOL') or die('hack attempt'); 
    22/** 
    3  * @package     Dolphin Core 
    4  * @copyright   Copyright (c) BoonEx Pty Limited - http://www.boonex.com/ 
    5  * @license     CC-BY - http://creativecommons.org/licenses/by/3.0/ 
     3 * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/ 
     4 * CC-BY License - http://creativecommons.org/licenses/by/3.0/ 
     5 * 
     6 * @defgroup    DolphinCore Dolphin Core 
     7 * @{ 
    68 */ 
    7 defined('BX_DOL') or die('hack attempt'); 
    89 
    910bx_import('BxDolDb'); 
    1011 
    1112/** 
    12  * Database queries for File storage class. 
    13  * @see BxDolStorage 
     13 * Database queries for grid. 
     14 * @see BxDolGrid 
    1415 */ 
    1516class BxDolGridQuery extends BxDolDb { 
     
    7475} 
    7576 
     77/** @} */ 
  • trunk/inc/js/jquery.webForms.js

    r15331 r15685  
    99    $(this).addNonWebForms(); 
    1010 
    11     // collapsable headers TODO: 
     11    // collapsable headers TODO: there is other duplicated functionlaity now and this one is not used 
    1212    $('thead.collapsable', this).each(function() { 
    1313        var $thead = $(this); 
Note: See TracChangeset for help on using the changeset viewer.