Location: PHPKode > projects > WCL - Web Control Library for PHP > DataGrid/DataGrid.php
<?php
/*
 * 	(C) Copyright by Christian Möller
 * 	All Rights reserved
 *
 * 	This file is part of the WCL (Web Control Library).
 *
 *  WCL 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with WCL.  If not, see <http://www.gnu.org/licenses/>.
 */

class wuiDataGrid extends wuiPanel  {
	
	var $datasource,
		$header_class,
		$cell_class,
		$hover_color = "#E4F3FA",
		$cell_color = "#FFFFFF",
		$alternating_color = "#FFFFD7",
		$showSelection = false,
		$rowSelect = false,
		$showFilter = true,
		$readOnly = false,
		$maxSize = 120;
	
	function wuiDataGrid($name) {
		parent::IControl($name);
		parent::setBaseTag("table");
		parent::setProp("cellspacing", "1");
	}
	
	function setDataSource($source) 	{ 
		if (!_eventmode()) $this->datasource = $source; 
	}
	
	function setHeaderClass($class) 	{ 
		if (!_eventmode()) $this->header_class = $class; 
		_eventmode_add("class", $class);
	}
	
	function setCellClass($class) 		{ 
		if (!_eventmode()) $this->cell_class = $class; 
	}
	
	function setHoverColor($color) 		{ 
		if (!_eventmode()) $this->hover_color = $color; 
	}

	function setCellColor($color) 		{ 
		$this->cell_color = $color; 
	}
	
	function setShowSelection($state) 	{ 
		$this->showSelection = $state; 
	}
	
	function setRowSelect($state) 		{ 
		$this->rowSelect = $state; 
	}
	
	function setFilterEnabled($state) 	{ 
		if (!_eventmode()) $this->showFilter = $state;
		//_eventmode_add();
	}
	
	function setMaxWidth($width) 		{
		if (!_eventmode()) $this->maxSize = $width;
	}
	
	function setReadOnly($state) 		{
		if (!_eventmode()) $this->readOnly = $state;
	}
	
	function setAlternateColor($color)	{
		if (!_eventmode()) $this->alternating_color = $color;
	}
	
	function render() {
		
		$content = parent::render_partial_to();
		$content .=	"<tr>";
		
		foreach ($this->datasource->columns as $col_name => $col_text) {
			$col_text_render = $col_text;
			if (is_subclass_of($col_text, "IControl"))
				$col_text_render = $col_text->render();
			if ($this->header_class) 
				$content .= "<th class=\"".$this->header_class."\">".$col_text_render."</th>";
			else 
				$content .= "<th>".$col_text_render."</th>";
		}
		
		$content .= "</tr>";
		
		$indent = false;
		
		foreach ($this->datasource->rows as $row) {
		
			$indent = !$indent;
			
			($indent)?$curr_color = $this->cell_color:$curr_color = $this->alternating_color;
			
			if ($this->showSelection && $this->rowSelect)
				$content .= "<tr onMouseOver=\"this.bgColor='".$this->hover_color."';\"
								 onMouseOut=\"this.bgColor='".$curr_color."'\" bgColor=\"".$curr_color."\">";
			else 
				$content .= "<tr bgColor=\"".$curr_color."\">";
				
			foreach ($row as $row_item) {
				$row_item_render = $row_item;
				if (is_subclass_of($row_item, "IControl"))
					$row_item_render = $row_item->render();
					
				if (!$this->readOnly) {
					$row_item_render = '<input style="border: 0px;" type="text" value="'.
										$row_item_render.'" size='.min(strlen($row_item_render)+10,$this->maxSize).'>';
				} 
				
				if ($this->showSelection && !$this->rowSelect)
					$content .= "<td class=\"".$this->cell_class."\" onMouseOver=\"this.bgColor='".$this->hover_color."';\"
								 onMouseOut=\"this.bgColor='".$thid->cell_color."'\">".$row_item_render."</td>";
				else
					$content .= "<td class=\"".$this->cell_class."\">".$row_item_render."</td>";
			}
			$content .= "</tr>";
		}
		
		$content .= parent::render_partial_tc();
		
		if ($this->showFilter) {
			$content .= '<div id="'.$this->name.'_searchdiv">
						<script language="javascript" type="text/javascript">
						 function filter_values(textbox, table) {
						 	if (textbox.value == "") {
						 		for (row = 1; row < table.rows.length; row++) 
						 			table.rows[row].style.display = \'block\';
						 	}
						 	for (row = 1; row < table.rows.length; row++) {
						 		if (table.rows[row].innerText.indexOf(textbox.value) == -1)
						 			table.rows[row].style.display = \'none\';
						 		else
						 			table.rows[row].style.display = \'block\';
						 	}
						 }
						 </script>';
			
			$content .= 'Search: <input type="text" id="'.$this->name.'_filter'.'" onkeyup="filter_values(this, '.$this->get_this().');"></div>';
		}
		
		SessionSetVar($this->name, serialize($this));
		
		return $content;
	}
}

?>
Return current item: WCL - Web Control Library for PHP