Scripts

Fluent jqGrid HTML Helper

Om de mogelijkheden van de jqGrid HTML Helper te demonstreren heb ik een voorbeeld applicatie opgezet. De voorbeeld applicatie is hier http://jqgrid.clockspeed.nl/ te vinden en de broncode is hier https://github.com/daanl/jqGrid-Helper-PHP te downloaden (voor niet git gebruikers, klik op downloads en je krijgt een popup scherm om de broncode te downloaden). Mocht je alleen de Helper willen gebruiken heb je alleen het bestand jqGrid.php nodig. Benodigdheden Om jqGrid te gebruiken heb je minimaal het volgende nodig (dit zit ook allemaal in de voorbeeld applicatie): * jqGrid 3.6.5 of nieuwer, je kan de nieuwste versie downloaden op de jqGrid download pagina, je hebt minimaal Grid Base, formatter en custom nodig (http://www.trirand.com/blog/?page_id=6 * jQuery 1.3 of nieuwer, je kan de laatste versie downloaden op http://jquery.com * jQuery theme, je kan een eigen thema maken op http://jqueryui.com/themeroller * De jqGrid PHP HTML Helper te downloaden via github http://github.com/daanl/jqGrid-Helper-PHP. Installatie & Gebruik Na het downloaden van de HTML Helper kan je direct aan de slag. Onderstaande code geeft een simpel voorbeeld. De methode create wordt altijd als eerste aangeroepen op het grid object, in de parameter wordt het id van de grid meegegeven. Dit grid id wordt in het grid gebruikt om een tabel met het opgegeven id te genereren. Alle andere opties, functies en events zijn vervolgens beschikbaar via “Method Chaining” ook wel bekend als Fluent Interface, de volgorde van de aanroepen heeft geen invloed op het resultaat. Doormiddel van SetUrl('url') kan je de url voor de AJAX request specificeren. Voorbeeld applicatie: 1. Download de voorbeeld applicatie op github (download source https://github.com/daanl/jqGrid-Helper-PHP) en pak deze uit 2. Zet de test database op, met behulp van de twee SQL bestanden: sakila-schema.sql en sakila-data.sql (te vinden in de voorbeeld applicatie). 3. Verander de database connectie gegevens in datafeed.php 4. Ga naar index.php en je hebt een werkend voorbeeld. Om het makkelijk te maken heb ik de helper ook hierbij gevoegd! Mocht je nog vragen of opmerkingen hoor ik het graag. Orginele blog artikel -> http://www.webpirates.nl/webpirates/daan-le-duc/50-fluent-jqgrid-helper-for-php

jqGrid.php
<?php

// Grid class, used to render JqGrid
class Grid
{
	private $_id;
	private $_altClass;
	private $_altRows;
	private $_autoEncode;
	private $_autoWidth;
	private $_caption;
	private $_columns = array();
	private $_dataType = 'json';
	private $_JsonReader = array('repeatitems' => 'false', 'id' => 0);
	private $_emptyRecords;
	private $_footerRow;
	private $_forceFit;
	private $_gridView;
	private $_headerTitles;
	private $_height;
	private $_hiddenGrid;
	private $_hideGrid;
	private $_hoverRows;
	private $_loadOnce;
	private $_loadText;
	private $_loadUi;
	private $_multiKey;
	private $_multiBoxOnly;
	private $_multiSelect;
	private $_multiSelectWidth;
	private $_onAfterInsertRow;
	private $_onBeforeRequest;
	private $_onBeforeSelectRow;
	private $_onGridComplete;
	private $_onLoadBeforeSend;
	private $_onLoadComplete;
	private $_onLoadError;
	private $_onCellSelect;
	private $_onDblClickRow;
	private $_onHeaderClick;
	private $_onPaging;
	private $_onRightClickRow;
	private $_onSelectAll;
	private $_onSelectRow;
	private $_onSortCol;
	private $_onResizeStart;
	private $_onResizeStop;
	private $_onSerializeGridData;
	private $_page;
	private $_pager;
	private $_pagerPos;
	private $_pgButtons;
	private $_pgInput;
	private $_pgText;
	private $_recordPos;
	private $_recordText;
	private $_requestType;
	private $_resizeClass;
	private $_rowList;
	private $_rowNum;
	private $_rowNumbers;
	private $_rowNumWidth;
	private $_scroll;
	private $_scrollInt;
	private $_scrollOffset;
	private $_scrollRows;
	private $_scrollTimeout;
	private $_shrinkToFit;
	private $_sortName;
	private $_sortOrder;
	private $_topPager;
	private $_toolbar;
	private $_toolbarPosition = 'top';
	private $_searchToolbar;
	private $_searchOnEnter;
	private $_searchClearButton;
	private $_searchToggleButton;
	private $_url;
	private $_viewRecords;
	private $_showAllSortIcons;
	private $_sortIconDirection;
	private $_sortOnHeaderClick;
	private $_width;

		/**
		 * Constructor
		 * @param string $id Id of grid
		 */
		private function __construct($id)
		{
		    if (trim($id) === '')
		    {
				throw new Exception("Id must contain a value to identify the grid");
		    }

		    $this->_id = $id;
		}

		/**
		 * creates new instance of grid
		 * @param string $id Id of grid
		 * @return Grid
		 */
		public static function create($id)
		{

			return new Grid($id);
		}

		/**
		 * Adds column to grid
		 * @param Column $colum
		 * @return Grid
		 */
		public function addColumn(Column $column)
		{
		    $this->_columns[] = $column;

		    return $this;
		}

		/**
		 * The class that is used for alternate rows. You can construct your own class and replace this value.
		 * This option is valid only if altRows options is set to true (default: ui-priority-secondary)
		 * @param string $altClass Classname for alternate rows
		 * @return Grid
		 */
		public function setAltClass($altClass)
		{
		    $this->_altClass = $altClass;

		    return $this;
		}

		/**
		 * Set a zebra-striped grid (default: false)
		 * @param boolean $altRows Boolean indicating if zebra-striped grid is used
		 * @return Grid
		 */
		public function setAltRows($altRows)
		{
		    $this->_altRows = $altRows;

		    return $this;
		}

		/**
		 * When set to true encodes (html encode) the incoming (from server) and posted
		 * data (from editing modules). For example < will be converted to &lt (default: false)
		 * @param boolan indicating if autoencode is used
		 * @return Grid
		 */
		public function setAutoEncode($autoEncode)
		{
			if (!is_bool($autoEncode))
			{
				throw new Exception('AutoEncode is not a bool');
			}

		    $this->_autoEncode = ($autoEncode === true) ? 'true' : 'false';

		    return $this;
		}

       /**
		 * When set to true, the grid width is recalculated automatically to the width of the
		 * parent element. This is done only initially when the grid is created. In order to
		 * resize the grid when the parent element changes width you should apply custom code
		 * and use a setGridWidth method for this purpose. (default: false)
		 * @param boolean $autoWidth indicating if autowidth is used
		 * @return Grid
		 */
		public function setAutoWidth($autoWidth)
		{
			if (!is_bool($autoWidth))
			{
				throw new Exception('autoWidth is not a bool');
			}

		    $this->_autoWidth = ($autoWidth === true) ? 'true' : 'false';

		    return $this;
		}

		/**
		 * Defines the caption layer for the grid. This caption appears above the header layer.
		 * If the string is empty the caption does not appear. (default: empty)
		 * @param string $caption Caption of grid
		 * @return Grid
		 */
		public function setCaption($caption)
		{
		    $this->_caption = $caption;

		    return $this;
		}

		/**
		 * Defines what type of information to expect to represent data in the grid. Valid
		 * options are json (default) and xml
		 * @param string $dataType Data type
		 * @return Grid
		 */
		public function setDataType($dataType)
		{
		    $this->_dataType = $dataType;

		    return $this;
		}

		/**
		 * Displayed when the returned (or the current) number of records is zero.
		 * This option is valid only if viewrecords option is set to true. (default value is
		 * set in language file)
		 * @param string $emptyRecords Display string
		 * @return Grid
		 */
		public function setEmptyRecords($emptyRecords)
		{
		    $this->_emptyRecords = $emptyRecords;

		    return $this;
		}

		/**
		 * If set to true this will place a footer table with one row below the grid records
		 * and above the pager. The number of columns equal to the number of columns in the colModel
		 * (default: false)
		 * @param boolean $footerRow indicating whether footerrow is displayed
		 * @return Grid
		 */
		public function setFooterRow($footerRow)
		{
			if (!is_bool($footerRow))
			{
				throw new Exception('footerRow is not a bool');
			}

		    $this->_footerRow = ($footerRow === true) ? 'true' : 'false';

		    return $this;
		}

		       /**
		 * If set to true, when resizing the width of a column, the adjacent column (to the right)
		 * will resize so that the overall grid width is maintained (e.g., reducing the width of
		 * column 2 by 30px will increase the size of column 3 by 30px).
		 * In this case there is no horizontal scrolbar.
		 * Note: this option is not compatible with shrinkToFit option - i.e if
		 * shrinkToFit is set to false, forceFit is ignored.
		 * @param boolean $forceFit indicating if forcefit is enforced
		 * @return Grid
		 */
		public function setForceFit($forceFit)
		{
			if (!is_bool($forceFit))
			{
				throw new Exception('forceFit is not a bool');
			}

		    $this->_forceFit = ($forceFit === true) ? 'true' : 'false';

		    return $this;
		}

		  /**
		 * In the previous versions of jqGrid including 3.4.X,reading relatively big data sets
		 * (Rows >=100 ) caused speed problems. The reason for this was that as every cell was
		 * inserted into the grid we applied about 5-6 jQuery calls to it. Now this problem has
		 * been resolved; we now insert the entry row at once with a jQuery append. The result is
		 * impressive - about 3-5 times faster. What will be the result if we insert all the
		 * data at once? Yes, this can be done with help of the gridview option. When set to true,
		 * the result is a grid that is 5 to 10 times faster. Of course when this option is set
		 * to true we have some limitations. If set to true we can not use treeGrid, subGrid,
		 * or afterInsertRow event. If you do not use these three options in the grid you can
		 * set this option to true and enjoy the speed. (default: false)
		 * @param boolean $gridView
		 * @return Grid
		 */
		public function setGridView($gridView)
		{
			if (!is_bool($gridView))
			{
				throw new Exception('gridView is not a bool');
			}

		    $this->_gridView = ($gridView === true) ? 'true' : 'false';

		    return $this;
		}

		/**
		 * If the option is set to true the title attribute is added to the column headers (default: false)
		 * @param boolean $headerTitles indicating if headertitles are enabled
		 * @return Grid
		 */
		public function setHeaderTitles($headerTitles)
		{
			if (!is_bool($headerTitles))
			{
				throw new Exception('headerTitles is not a bool');
			}

		    $this->_headerTitles = ($headerTitles === true) ? 'true' : 'false';

		    return $this;
		}

		/**
		 * The height of the grid in pixels (default: 100%, which is the only acceptable percentage for jqGrid)
		 * @param in $height Height in pixels
		 * @return Grid
		 */
		public function setHeight($height)
		{
			if (!ctype_digit((string) $height))
			{
				throw new Exception('Height is not an number');
			}

		    $this->_height = $height;

		    return $this;
		}

		/**
		 * If set to true the grid initially is hidden. The data is not loaded (no request is sent) and only the
		 * caption layer is shown. When the show/hide button is clicked for the first time to show the grid, the request
		 * is sent to the server, the data is loaded, and the grid is shown. From this point on we have a regular grid.
		 * This option has effect only if the caption property is not empty. (default: false)
		 * @param boolean $hiddenGrid indicating if hiddengrid is enforced
		 * @return Grid
		 */
		public function setHiddenGrid($hiddenGrid)
		{
			if (!is_bool($hiddenGrid))
			{
				throw new Exception('hiddenGrid is not a bool');
			}

		    $this->_hiddenGrid = ($hiddenGrid === true) ? 'true' : 'false';

		    return $this;
		}

		/**
		 * Enables or disables the show/hide grid button, which appears on the right side of the caption layer.
		 * Takes effect only if the caption property is not an empty string. (default: true)
		 * @param boolean indicating if show/hide button is enabled
		 * @return Grid
		 */
		public function setHideGrid($hideGrid)
		{
			if (!is_bool($hideGrid))
			{
				throw new Exception('hideGrid is not a bool');
			}

		    $this->_hideGrid = ($hideGrid === true) ? 'true' : 'false';

		    return $this;
		}

		/**
		 * When set to false the mouse hovering is disabled in the grid data rows. (default: true)
		 * @param boolean $hoverRows Indicates whether hoverrows is enabled
		 * @return Grid
		 */
		public function setHoverRows($hoverRows)
		{
			if (!is_bool($hoverRows))
			{
				throw new Exception('hoverRows is not a bool');
			}

		    $this->_hoverRows = ($hoverRows === true) ? 'true' : 'false';

		    return $this;
		}

		/**
		 * If this flag is set to true, the grid loads the data from the server only once (using the
		 * appropriate datatype). After the first request the datatype parameter is automatically
		 * changed to local and all further manipulations are done on the client side. The functions
		 * of the pager (if present) are disabled. (default: false)
		 * @param boolean $loadOnce indicating if loadonce is enforced
		 * @return Grid
		 */
		public function setLoadOnce($loadOnce)
		{
			if (!is_bool($loadOnce))
			{
				throw new Exception('loadOnce is not a bool');
			}

		    $this->_loadOnce = ($loadOnce === true) ? 'true' : 'false';

		    return $this;
		}

		/**
		 * The text which appears when requesting and sorting data. This parameter override the value located in the language file
		 * @param string Loadtext
		 * @return Grid
		 */
		public function setLoadText($loadText)
		{
		    $this->_loadText = $loadText;

		    return $this;
		}

		/**
		 * This option controls what to do when an ajax operation is in progress.
		 * 'disable' - disables the jqGrid progress indicator. This way you can use your own indicator.
		 * 'enable' (default) - enables 

Reacties

0
Nog geen reacties.