突发奇想 写了个类似.net中的DataGrid(还很不完善)
请各位帮帮忙,一起来完善它。多谢了
<?php
//require_once("DB.php");
class DataGrid
{
var $dsn = "";
var $DataSource = array();
var $AllowPaging = false;
var $PageSize = 0; //integer
var $PageCount;
var $CurrentPageIndex = 0;
var $AllowSorting = false; //true | false
var $AutoGenerateColumns = true; //true | false
var $BackImageUrl = false;
var $CellPadding = 0;
var $CellSpacing = 0;
var $GridLines = "None"; //None | Horizontal | Vertical | Both
var $HorizontalAlign = "NotSet"; //Center | Justify | Left | NotSet | Right"
var $ShowHeader = true;
var $ShowFooter = true;
var $HeaderStyle = null;
var $ItemStyle = null;
var $FooterStyle = null;
var $PagerStyle = null;
var $_headerStyle = "";
var $_itemStyle = "";
var $_footerStyle = "";
var $_pagerStyle = "";
var $AlternatingItemStyle = null;
var $_alternatingItemStyle = "";
var $Style = null;
var $_style = "";
var $Columns = null;
var $_rows = array();
function DataGrid()
{
$this->Columns = new ColumnCollection;
$this->HeaderStyle =& new Style();
$this->ItemStyle =& new Style();
$this->FooterStyle =& new Style();
$this->AlternatingItemStyle =& new Style();
$this->PagerStyle = new PagerStyle();
$this->Style =& new Style();
}
function _buildStyle()
{
$this->_headerStyle = $this->HeaderStyle->build();
$_columns = $this->Columns->Item;
switch (strtolower($this->GridLines)) { //None | Horizontal | Vertical | Both
case "horizontal":
break;
case "vertical":
break;
case "both":
while (list(, $_c) = each($_columns)) {
echo $_c->ColumnName;
$_c->ItemStyle->AddAttributes('border', '1px solid #000000');
}
break;
case "none":
default:
break;
}
switch (strtolower($this->HorizontalAlign)) { //Center | Justify | Left | NotSet | Right"
case "center":
break;
case "justify":
break;
case "left":
break;
case "right":
break;
case "noset":
default:
break;
}
$this->_itemStyle = $this->ItemStyle->build();
$this->_footerStyle = $this->FooterStyle->build();
$this->_alternatingItemStyle = $this->AlternatingItemStyle->build();
$this->_pagerStyle = $this->PagerStyle->build();
if ($this->BackImageUrl) $this->Style->AddAttributes('background-image', 'url(' . $this->BackImageUrl . ')');
//if (is_int($this->CellPadding)) $this->Style->AddAttributes('padding', $this->CellPadding);
//if (is_int($this->CellSpacing)) $this->Style->AddAttributes('cell-spacing', $this->CellSpacing);
$this->_style = $this->Style->build();
}
function _buildRows()
{
if ($this->AutoGenerateColumns) {
$c = $this->DataSource[0];
while (list($k, $v) = each($c)) {
$bc = new BoundColumn;
$bc->HeaderText = $k;
$bc->DataField = $k;
$this->Columns->Add($bc);
}
}
}
function _paging()
{
$this->_buildRows();
$this->_buildStyle();
$this->_rows = $this->Columns->build();
if ($this->AllowPaging) {
$NewPageIndex = isset($_GET[$this->PagerStyle->PageId]) ? $_GET[$this->PagerStyle->PageId] : 1;
$this->PagerStyle->CurrentPageIndex = $this->CurrentPageIndex = $NewPageIndex;
$this->PagerStyle->PageCount = $this->PageCount = ceil(count($this->DataSource) / $this->PageSize);
return array_slice($this->DataSource, ($this->CurrentPageIndex - 1) * $this->PageSize, $this->PageSize);
} else {
return $this->DataSource;
}
}
function get()
{
$ds = $this->_paging();
echo $this->PagerStyle->buildTextBar();
$html = "<TABLE " . $this->_style;
if (is_int($this->CellPadding)) $html .= " CELLPADDING=\"{$this->CellPadding}\"";
if (is_int($this->CellSpacing)) $html .= " CELLSPACING=\"{$this->CellSpacing}\"";
$html .= ">\n";
if ($this->ShowHeader) {
$html .= "<TR {$this->_headerStyle}>{$this->_rows['header']}</TR>\n";
}
$i = 0;
while (list(, $data) = each($ds)) {
$i++;
$tmp = $this->_rows['item'];
while (list($key, $value) = each($data)) {
$tmp = preg_replace("/\{$key\}/", $value, $tmp);
}
if ($this->_alternatingItemStyle && ($i % 2 == 0)) {
$html .= "<TR {$this->_alternatingItemStyle}>" . $tmp . "</TR>\n";
continue;
}
$html .= "<TR {$this->_itemStyle}>" . $tmp . "</TR>\n";
}
if ($this->ShowFooter) {
$html .= "<TR {$this->_footerStyle}>{$this->_rows['footer']}</TR>\n";
}
$html .= "</TABLE>\n";
echo $this->PagerStyle->buildNumericBar();
return $html;
}
function show()
{
print $this->get();
}
}