/**
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
             );
}}

解决方案 »

  1.   

    /**
    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";
    }
    }
      

  2.   

    /**
    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 .= "&nbsp;$i&nbsp;";
    } else {
    $u .= "&nbsp;<A HREF=\"" . $url . $i . "\">" . $i . "</A>&nbsp;";
    }
    } if ($pb > 1) {
    $u = "<A HREF=\"" . $url . ($pb - 1) ."\">&lt;&lt;</A>&nbsp;" . $u;
    }
    if ($pe < $this->PageCount) {
    $u .= "&nbsp;<A HREF=\"" . $url . ($i + 1) . "\">&gt;&gt;</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;
    }
    ?>
      

  3.   

    我想给测试列的,但超过三次不能回复了!<?phprequire('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();?>
      

  4.   

    又改进了一点 :)示例
    cxz.cxc.cc/DataGrid/test.php