突发奇想 写了个类似.net中的DataGrid(还很不完善)

blueaurora 2003-08-20 03:12:42
请各位帮帮忙,一起来完善它。多谢了
<?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();
}

}
...全文
162 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueaurora 2003-08-22
  • 打赏
  • 举报
回复
又改进了一点 :)

示例
cxz.cxc.cc/DataGrid/test.php
hover_online 2003-08-21
  • 打赏
  • 举报
回复
好!
dgnApo 2003-08-21
  • 打赏
  • 举报
回复
偶要~~
DFlyingchen 2003-08-20
  • 打赏
  • 举报
回复
路过,看来不错,先收藏
开学了和你一起做
「已注销」 2003-08-20
  • 打赏
  • 举报
回复
路过
bluemeteor 2003-08-20
  • 打赏
  • 举报
回复
嗯,留个名,一起做
blueaurora 2003-08-20
  • 打赏
  • 举报
回复
楼上的,多谢!
晚上回去加些注释上去 :)
brat365 2003-08-20
  • 打赏
  • 举报
回复
很支持你,虽然还没看,但知道一定挺不错的
我下下来帮你测试
QQ:68497170
交个朋友先
blueaurora 2003-08-20
  • 打赏
  • 举报
回复
我想给测试列的,但超过三次不能回复了!

<?php

require('DB.php');
require('DataGrid.php');

$db = DB::connect('mysql://root:root@localhost/site');
$sql = "SELECT * FROM province order by id";
$ds = $db->getAll($sql, null, DB_FETCHMODE_ASSOC);

$datagrid = new DataGrid();
$datagrid->DataSource = $ds;
$datagrid->AllowPaging = true;
$datagrid->PageSize = 5;
$datagrid->GridLines = "Both";
$datagrid->AlternatingItemStyle->BackColor = "#ffffcc";

$datagrid->show();



?>
xuzuning 2003-08-20
  • 打赏
  • 举报
回复
既不加说明有不给测试例。
真可以
blueaurora 2003-08-20
  • 打赏
  • 举报
回复
/**
class Style
*/

class Style
{
var $Font_Bold = false;
var $Font_Italic = false;
var $Font_Overline = false;
var $Font_Strikeout = false;
var $Font_Underline = false;
var $Font_Name = false;
var $Font_Size = false;

var $BackColor = false;
var $BorderColor = false;
var $BorderStyle = false; // NotSet 不设置边框样式。
// None 无边框
// Dotted 虚线边框。
// Dashed 点划线边框。
// Solid 实线边框。
// Double 双实线边框。
// Groove 用于凹陷边框外观的凹槽状边框。
// Ridge 用于凸起边框外观的突起边框。
// Inset 用于凹陷控件外观的内嵌边框。
// Outset
var $BorderWidth = false;
var $CssClass = false;
var $ForeColor = false;

var $HorizontalAlign = false;//水平对齐
var $VerticalAlign = false;//垂直对齐

var $Height = false;
var $Width = false;

var $_extraProperty = array();

function Style()
{

}

function AddAttributes($property, $value)
{
$this->_extraProperty[$property] = $value;
}

function Reset()
{
$Font_Bold = false;
$Font_Italic = false;
$Font_Overline = false;
$Font_Strikeout = false;
$Font_Underline = false;
$Font_Name = false;
$Font_Size = false;

$BackColor = false;
$BorderColor = false;
$BorderStyle = false;
$BorderWidth = false;
$CssClass = false;
$ForeColor = false;
$Height = false;
$Width = false;
$HorizontalAlign = false;
$VerticalAlign = false;

$_extraProperty = array();
}

function build()
{
$style = "";
if ($this->Font_Name) $style .= " font-family: '{$this->Font_Name}';";
if ($this->Font_Size) $style .= " font-size: {$this->Font_Size}px;";
if ($this->Font_Bold) $style .= " font-weight: bold;";
if ($this->Font_Italic) $style .= " font-style: italic;";
if ($this->Font_Overline || $this->Font_Strikeout || $this->Font_Underline) {
$style .= " text-decoration:";
if ($this->Font_Overline) $style .= " overline";
if ($this->Font_Strikeout) $style .= " line-through";
if ($this->Font_Underline) $style .= " underline";
$style .= ";";
}
if ($this->BackColor) $style .= " background-color: {$this->BackColor};";
if ($this->BorderColor || $this->BorderStyle || $this->BorderWidth) {
$style .= " border:";
if ($this->BorderWidth) $style .= " {$this->BorderWidth}px";
if ($this->BorderStyle) $style .= " {$this->BorderStyle}";
if ($this->BorderColor) $style .= " {$this->BorderColor}";
$style .= ";";
}
//if ($this->HorizontalAlign) $style .= " align:";
if ($this->VerticalAlign) $style .= " vertical-align: {$this->VerticalAlign}";

if ($this->Width !== false) $style .= " width: {$this->Width};";
if ($this->Height !== false) $style .= " height: {$this->Heigth};";

while (list($property, $value) = each($this->_extraProperty)) {
$style .= " $property: $value;";
}

if ($style) $style = "STYLE=\"" . $style . "\"";
if ($this->CssClass) $style = " class=\"{$this->CssClass}\" $style";


return $style;
}

}

class PagerStyle extends Style
{
var $Mode = "Text"; //Text Numeric
var $PageId = "PID";
var $NextPageText;
var $PrevPageText;

var $CurrentPageIndex;
var $PageCount;

var $PageButtonCount = 3;
var $Wrap;

var $_style;
var $_styleTR;

var $_qs;
var $_uri;

function PagerStyle()
{
$querystring = array();
$qs = array();
if (!empty($_SERVER['QUERY_STRING'])) {
$qs = explode('&', $_SERVER['QUERY_STRING']);
for ($i = 0; $i < count($qs); $i++) {
list($name, $value) = explode('=', $qs[$i]);
if ($name != $this->PageId) {
$qs[$name] = $value;
}
unset($qs[$i]);
}
}

foreach ($qs as $name => $value) {
$querystring[] = $name . '=' . $value;
}
$this->_qs = implode('&', $querystring);
}

function buildTextBar()
{
$this->_buildStyle();

if ($this->_qs) {
$url = $this->_uri . "?" . $this->_qs . "&" . $this->PageId . "=";
} else {
$url = $this->_uri . "?" . $this->PageId . "=";
}

if ($this->CurrentPageIndex > 1) {
$url_first = "<A HREF=\"" . $url . "1\">首页</A>";
$url_priv = "<A HREF=\"" . $url . ($this->CurrentPageIndex - 1) . "\">上一页</A>";
} else {
$url_first = "首页";
$url_priv = "上一页";
}
if ($this->CurrentPageIndex < $this->PageCount) {
$url_next = "<A HREF=\"" . $url . ($this->CurrentPageIndex + 1) . "\">下一页</A>";
$url_last = "<A HREF=\"" . $url . $this->PageCount . "\">未页</A>";
} else {
$url_next = "下一页";
$url_last = "未页";
}

$html = "";
$html .= "<TABLE {$this->_style}>\n";
$html .= "<TR><TD {$this->_styleTR}>\n";
$html .= $url_first . $url_priv . $url_next . $url_last;
$html .= "</TD></TR>\n";
$html .= "</TABLE>\n";

return $html;
}

function buildNumericBar()
{
$this->_buildStyle();

if ($this->_qs) {
$url = $this->_uri . "?" . $this->_qs . "&" . $this->PageId . "=";
} else {
$url = $this->_uri . "?" . $this->PageId . "=";
}

$hp = floor($this->PageButtonCount / 2);
$pb = (($this->CurrentPageIndex - $hp) > 1) ? ($this->CurrentPageIndex - $hp) : 1;
$pe = (($pb + $this->PageButtonCount) > $this->PageCount) ? $this->PageCount : ($pb + $this->PageButtonCount);

$u = "";
for ($i = $pb; $i < $pe; $i++) {
if ($i == $this->CurrentPageIndex) {
$u .= " $i ";
} else {
$u .= " <A HREF=\"" . $url . $i . "\">" . $i . "</A> ";
}
}

if ($pb > 1) {
$u = "<A HREF=\"" . $url . ($pb - 1) ."\"><<</A> " . $u;
}
if ($pe < $this->PageCount) {
$u .= " <A HREF=\"" . $url . ($i + 1) . "\">>></A>";
}

$html = "";
$html .= "<TABLE {$this->_style}>\n";
$html .= "<TR><TD {$this->_styleTR}>\n";
$html .= $u;
$html .= "</TD></TR>\n";
$html .= "</TABLE>\n";

return $html;
}

function _buildStyle()
{
$this->_style = $this->build();
$this->_styleTR = "";
}
/*
function build($mode = "Text")
{
$mode = strtolower($mode);
if ($mode == "text")
return $this->buildTextBar();
else
return $this->buildNumeric();
}
*/
}


function StringFormat()
{
$args = func_get_args();
$string = $args[0];

for ($i = 1; $i < sizeof($args); $i++) {
$string = preg_replace("/{" . ($i - 1) . "}/", $args[$i], $string);
}

return $string;
}


?>
blueaurora 2003-08-20
  • 打赏
  • 举报
回复
/**
class BoundColumn
*/
class BoundColumn extends Column
{
var $ColumnName = "BoundColumn";

function BoundColumn()
{
$this->ColumnName = "BoundColumn";

$this->HeaderStyle =& new Style();
$this->ItemStyle =& new Style();
$this->FooterStyle =& new Style();
}

function _buildHeader()
{
if (!$this->Visible) {
$this->HeaderStyle->AddAttributes('display', 'none');
}
$style = $this->HeaderStyle->build();

$content = ($this->HeaderImageUrl) ? ('<IMG SRC="' . $this->HeaderImageUrl . '" Border="0">') : $this->HeaderText;
return ('<TD ' . $style . '>' . $content . '</TD>');
}

function _buildItem()
{
if (!$this->Visible) {
$this->ItemStyle->AddAttributes('display', 'none');
}
$style = $this->ItemStyle->build();

return ('<TD ' . $style . '>{' . $this->DataField . '}</TD>');
}

function _buildFooter()
{
if (!$this->Visible) {
$this->FooterStyle->AddAttributes('display', 'none');
}
$style = $this->FooterStyle->build();

return ('<TD ' . $style . '>' . $this->FooterText . '</TD>');
}

}
/**
class HyperLinkColumn
*/
class HyperLinkColumn extends Column
{
var $ColumnName = "HyperLinkColumn";

var $DataNavigateUrlField;
var $DataNavigateUrlFormatString;

var $NavigateUrl;
var $Target = false;

var $Text;

function HyperLinkColumn()
{
$this->ColumnName = "HyperLinkColumn";

$this->HeaderStyle =& new Style();
$this->ItemStyle =& new Style();
$this->FooterStyle =& new Style();
}

function _buildHeader()
{
if (!$this->Visible) {
$this->HeaderStyle->AddAttributes('display', 'none');
}
$style = $this->HeaderStyle->build();

$content = ($this->HeaderImageUrl) ? ('<IMG SRC="' . $this->HeaderImageUrl . '" Border="0">') : $this->HeaderText;
return ('<TD ' . $style . '>' . $content . '</TD>');
}

function _buildItem()
{
if (!$this->Visible) {
$this->ItemStyle->AddAttributes('display', 'none');
}
$style = $this->ItemStyle->build();

$html = '<TD ' . $style . '>';
$html .= '<A HREF="' . StringFormat($this->DataNavigateUrlFormatString, '{' . $this->DataField . '}');
if ($this->Target) $html .= '" TARGET="' . $this->Target;
$html .= '">{' . $this->DataField . '}</A>';
$html .= '</TD>';
return $html;
}

function _buildFooter()
{
if (!$this->Visible) {
$this->FooterStyle->AddAttributes('display', 'none');
}
$style = $this->FooterStyle->build();

return ('<TD ' . $style . '>' . $this->FooterText . '</TD>');
}

}
/**
class TemplateColumn
*/
class TemplateColumn extends Column
{
var $ColumnName = "TemplateColumn";

function TemplateColumn()
{
$this->ColumnName = "TemplateColumn";
}


}
blueaurora 2003-08-20
  • 打赏
  • 举报
回复
/**
class ColumnCollection
*/
class ColumnCollection
{

var $Count = 0;
var $Item = array();


function ColumnCollection()
{
$this->Count = 0;
$this->Item = array();
}

function Add($column)
{

$this->Item[$this->Count] = $column;
$this->Count++;
}

function AddAt($offset, $column)
{
if ($offset < 0) {
array_unshift($this->Item, $column);
} elseif ($offset > $this->Count) {
array_push($this->Item, $column);
} else {
$tmp1 = array_slice($this->Item, 0, $offset + 1);
$tmp2 = array_slice($this->Item, $offset + 1);
array_push($tmp1, $column);
$this->Item = array_merge($tmp1, $tmp2);
}

$this->Count = sizeof($this->Item);
}

function Remove($column)
{
for ($i = 0; $i < $this->Count; $i++) {
if ($this->Item[$i] == $column) {
break;
}
}
$this->RemoveAt($i);
}

function RemoveAt($offset)
{
if ($offset < 0) {
array_shift($this->Item);
} elseif ($offset > $this->Count) {
array_pop($this->Item);
} else {
$tmp1 = array_slice($this->Item, 0, $offset + 1);
$tmp2 = array_slice($this->Item, $offset + 1);
array_pop($tmp1, $column);
$this->Item = array_merge($tmp1, $tmp2);
}

$this->Count = sizeof($this->Item);
}

function Clear()
{
$this->Item = array();
$this->Count = 0;
}


function build()
{
$header = "";
$item = "";
$footer = "";

while (list(, $column) = each($this->Item)) {
$html = $column->build();
$header .= $html['header'];
$item .= $html['item'];
$footer .= $html['footer'];
}

return array(
'header' => $header,
'item' => $item,
'footer' => $footer
);
}

}
/**
class Column

*/

class Column
{
var $ColumnName = "";

var $DataField = "";
var $DataFormatString = "";

var $HeaderStyle = null;
var $HeaderText = "";
var $HeaderImageUrl = "";

var $ItemStyle = null;

var $FooterStyle = null;
var $FooterText = "";

var $Visible = true;

function Column()
{

}

function build()
{

$header = $this->_buildHeader();
$item = $this->_buildItem();
$footer = $this->_buildFooter();

return array(
'header' => $header,
'item' => $item,
'footer' => $footer
);
}

}
大家好,又见面了!EasyUI又更新了,这次更新内容还是不少的,具体内容请参考下面的更新说明,官方的更新说明还少了1条,我给补上了。 jQuery EasyUI 1.3.5版本更新内容: Bug(修复) searchbox:修复“searcher”函数提供的“name”参数值错误的问题; combo:修复“isValid”方法无法返回布尔值的问题; combo:修复点击页面某一个combo组件的下拉列表时触发的“onHidePanel”事件会导致页面上其他combo组件的下拉列表被关闭的问题; combogrid:修复某些从combo组件继承来的方法无法使用的问题。 Improvement(改进) datagrid:改进检查行时候的性能; menu:允许追加菜单分隔符; menu:新增“hideOnUnHover”属性用于在鼠标离开菜单的时候指示是否需要隐藏菜单; slider:新增“clear”和“reset”方法; tabs:新增“unselect”方法、“onUnselect”事件; tabs:新增“selected”属性,用于指定的默认打开的面板; tabs:Tab Panel(Tab页)新增“collapsible”属性,用于设置是否允许摺叠面板; tabs:新增“showHeader”属性、“showHeader”方法和“hideHeader”方法; combobox:允许“disabled”属性禁用下拉列表选项; tree:改进数据加载时候的性能; pagination:新增“layout”属性,用于自定义控件的样式布局; accordion:新增“unselect”方法、“onUnselect”事件; accordion:新增“select”和“multiple”属性; accordion:新增“getSelections”方法; datebox:新增“sharedCalendar”属性,允许多个datebox控件共享使用同一个calendar控件。 datebox:新增“buttons”属性,用于自定义日历下方的按钮。 (译者注:该点更新内容官方更新公告上没有注明,具体内容和用法请看datebox的API。) 历史版本: - jQuery EasyUI 1.3.4 离线API简体文版 http://download.csdn.net/detail/richie696/6302785 - jQuery EasyUI 1.3.4 离线API简体文版 http://download.csdn.net/detail/richie696/5363933

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧