grid.php
<?php
function activegrid($gridnumber, $width, $height, $background, $border, $headers, $dataarray, $DblClickJS){
if ($gridnumber) {
if ($DblClickJS == '')
{
$DblClickJS = '\'\'';
}
?>
<style>
#grid<?php print($gridnumber)?> {height: <?php print($height)?>px; width: <?php print($width)?>px; border: <?php print($border)?>; background: <?php print($background)?>}
#grid<?php print($gridnumber)?>
.active-scroll-data {left:0px; top: 0px}
.active-controls-grid {height: 100%; font: menu;}
.active-grid-row {border-bottom: 1px solid threedlightshadow;}
.active-grid-column {border-right: 1px solid threedlightshadow;}
.active-controls-grid {height: 100%; font: menu; border: 3px double black; }
<?php
$str = "";
foreach($headers as $header)
{
if(strtoupper($header["Display"]) == "NONE")
{
$str .= "\n.active-column-" . $header["ID"] . " {display: none;}";
} else {
$str .= "\n.active-column-" . $header["ID"] . " {
width:" . $header["Width"] . "px;
text-align: " . $header["Align"] . ";
background-color: " . $header["BGColor"] . ";
}
";
}
}
print $str;
?>
.active-grid-column {border-right: 1px solid threedshadow;}
.active-grid-row {border-bottom: 1px solid threedlightshadow;}
.active-template-input-<?php print($gridnumber)?>
{ text-align: top; border-style: none; font-family: Arial; font-size: 8pt; }
.grid-input-1 { text-align: top; height: 30px; border-style: none; font-family: Arial; font-size: 8pt; }
.grid-select-1 { text-align: top; height: 15px; border-style: none; font-family: Arial; font-size: 8pt; }
</style>
<script>
var datalist<?php print($gridnumber)?> = [
<?php
$str = "";
if (!is_array($dataarray))
{
if ($dataarray == "EMPTY")
{
$strcol = "";
foreach($headers as $header)
{
if (!$strcol == "") {
$strcol .= ",";
} else {
$strcol .= "[";
}
$strcol .= "\"";
$strcol .= "";
$strcol .= "\"";
}
$str .= $strcol;
}
} else {
foreach($dataarray as $data)
{
$strcol = "";
if (!$str == "")
{
$str .= "],";
}
foreach($headers as $header)
{
if (!$strcol == "")
{
$strcol .= ",";
} else {
$strcol .= "[";
}
if (strchr($header["Columns"], ","))
{
$ColArr = split(",", $header["Columns"]);
$stradd = "";
foreach ($ColArr As $ColHeader)
{
if ($stradd == "")
{
$stradd .= "\"";
} else {
$stradd .= " ";
}
$stradd .= $data[$ColHeader];
}
$stradd .= "\"";
$strcol .= $stradd;
} else {
$strcol .= "\"";
$strcol .= $data[$header["Columns"]];
$strcol .= "\"";
}
}
$str .= $strcol;
}
}
print $str;
?>
]];
var headlist<?php print($gridnumber)?> = [<?php
$str = "";
foreach($headers as $header)
{
if (!$str == "") {
$str .= ",\n";
}
$str .= "\"" . $header["Name"] . "\"";
}
print $str;
?>
];
var G<?php print($gridnumber)?> = new Object;
G<?php print($gridnumber)?>.Templates = new Object;
G<?php print($gridnumber)?>.Templates.Input = Active.System.Template.subclass();
G<?php print($gridnumber)?>.Templates.Input.create = function(){
/****************************************************************
Input Cell template.
*****************************************************************/
var obj = this.prototype;
var _super = this.superclass.prototype;
obj.getName = function() {
var r = this.$owner.$index;
var c = this.getColumnProperty("index");
//return "item_"+r+"_"+c;
<?php
foreach($headers as $header)
{
if (isset($header["Edit"]) && isset($header["EditType"])) {
if (strtolower($header["EditType"]) == "input") {
print "if (c == " . $header["ID"] . ") { ";
print "return '" . $header["Columns"] . "_'+r;";
print "}\n";
}
}
}
?>
}
obj.setTag("input");
obj.setClass("template","input-<?php print($gridnumber)?>");
obj.setClass("input",function(){return this.getColumnProperty("index")});
obj.setAttribute("type","text");
obj.setAttribute("value",function(){return this.getItemProperty("text");});
obj.setAttribute("name", function(){return obj.getName.call(this);})
obj.setEvent( "onblur", function(event, src) { this.onBlurAction( event, src ); } );
obj.onBlurAction = function( event ) {
var name = this.element().name;
var row = this.getRowProperty("index");
var col = this.getColumnProperty("index");
var originalVal = this.getItemProperty("text");
var currentVal = this.element().value;
if (currentVal!=originalVal) {
this.onChangeAction( currentVal, name, row, col );
}
}
/**
* This function can be overridden to receive change events from the
* rendered grid input elements
*/
obj.onChangeAction = function( newVal, name, row, col ) {
//alert("Changed "+name+" ("+row+":"+col+") to "+newVal );
datalist<?php print($gridnumber)?>[row][col]=newVal;
}
};
G<?php print($gridnumber)?>.Templates.Input.create();
var grid<?php print($gridnumber)?> = new Active.Controls.Grid;
grid<?php print($gridnumber)?>.setId("grid<?php print($gridnumber)?>");
grid<?php print($gridnumber)?>.setRowCount(<?php print(count($dataarray)); ?>);
grid<?php print($gridnumber)?>.setColumnCount(<?php print(count($headers)); ?>);
grid<?php print($gridnumber)?>.setDataText(function(i, j){return datalist<?php print($gridnumber)?>[i][j]});
grid<?php print($gridnumber)?>.setColumnText(function(i){return headlist<?php print($gridnumber)?>[i]});
// set headers width/height
grid<?php print($gridnumber)?>.setRowHeaderWidth("0px");
grid<?php print($gridnumber)?>.setColumnHeaderHeight("20px");
// Double Click Functionality
var row = new Active.Templates.Row;
row.setEvent("ondblclick", function(){this.action("DblClick")});
grid<?php print($gridnumber)?>.setTemplate("row", row);
grid<?php print($gridnumber)?>.setAction("DblClick", <?php print($DblClickJS)?>);
// Input Box Test
//var input = new Active.HTML.INPUT;
//input.setClass("box", "input");
//input.setAttribute("value", function(){return this.getItemProperty("text")});
<?php
foreach($headers as $header)
{
if (isset($header["Edit"]) && isset($header["EditType"])) {
if (strtolower($header["EditType"]) == "input") {
print "grid" . $gridnumber . ".setTemplate('column', new G" . $gridnumber . ".Templates.Input, " . $header["ID"] . ");";
}
}
}
?>
document.write(grid<?php print($gridnumber)?>);
function addRow<?php print($gridnumber)?>(rowData){
//var rowData = ["GGL", "Google, Inc", "9,999.999", "765.432", "10000"];
datalist<?php print($gridnumber)?>.unshift(rowData);
grid<?php print($gridnumber)?>.setRowProperty("count", datalist<?php print($gridnumber)?>.length);
grid<?php print($gridnumber)?>.refresh();
//alert(grid<?php print($gridnumber)?>.getProperty("row/count") + '-' + datalist<?php print($gridnumber)?>.length);
}
function remRow<?php print($gridnumber)?>() {
robj = grid<?php print($gridnumber)?>;
var srow = robj.getProperty("selection/index");
var nrow = robj.getProperty("row/count");
var thisRowB = robj.getProperty("row/values",nrow);
var thisRow=[];
var tmpArr=[];
var ind,k=0;
robj.setProperty("selection/index",-1);
if (nrow==1) thisRow='';
if (srow!=-1 && nrow>0)
{
for (i=0;i<nrow-1;i++)
{
if (thisRowB[i]==srow) {
k++;ind=i;
alert(thisRowB[i]);
datalist<?php print($gridnumber)?>.splice(thisRowB[i],1);
}
}
if (ind<nrow) robj.setProperty("selection/index",thisRow[ind]);
grid<?php print($gridnumber)?>.setRowProperty("count", datalist<?php print($gridnumber)?>.length);
grid<?php print($gridnumber)?>.refresh();
}
//alert(grid<?php print($gridnumber)?>.getProperty("row/count") + '-' + datalist<?php print($gridnumber)?>.length);
}
</script>
<?php
$colstr = "";
$inpstr = "";
foreach($headers as $header)
{
if (isset($header["Edit"]) && isset($header["EditType"])) {
if (strtolower($header["EditType"]) == "input") {
if ($colstr != "") {
$colstr .= ",";
}
$colstr .= $header["Columns"];
$inpstr .= "<input type='hidden' name='D_Grid_Count_" . $header["Columns"] . "' value='" . count($dataarray) . "'>";
}
}
}
if ($colstr != "") {
print "<input type='hidden' name='D_Grid_Columns' value='" . $colstr . "'>";
print $inpstr;
}
?>
<?php
}
}
?>
usage:
<?php
$headers = array(
array(
"ID" => "0",
"Columns" => "CustomerID",
"Name" => "CustomerID",
"Width" => "0",
"Align" => "left",
"BGColor" => "",
"Display" => "none"
),
array(
"ID" => "1",
"Columns" => "Company",
"Name" => "Company",
"Width" => "150",
"Align" => "left",
"BGColor" => "threedlightshadow",
"Display" => ""
)
);
$DblJS = 'function(src){var i = src.getProperty("item/index");document.location.href = "./index.php?A=SetCust&CrID="+this.getDataText(i, 0)}';
activegrid('1', '1000', '475', 'grey', 'none', $headers, $recordset, $DblJS);
activegrid('2', '1000', '475', 'grey', 'none', $headers, 'EMPTY', $DblJS);
?>
and for editing:
$headers = array(
array(
"ID" => "0",
"Columns" => "CPE_Desc",
"Name" => "Description",
"Width" => "150",
"Align" => "left",
"BGColor" => "",
"Display" => ""
,"Edit" => "1","EditType" => "input"
),
array(
"ID" => "1",
"Columns" => "CPE_ExtDesc",
"Name" => "Extended Desc",
"Width" => "175",
"Align" => "left",
"BGColor" => "",
"Display" => ""
,"Edit" => "1","EditType" => "input"
)
);
<a href="#" onclick="remRow1();">Remove Selected Row</a>
<a href="#" onclick="remRow2();">Remove Selected Row</a>
<script language="javascript">
function inAdd(data, rec) {
if (rec == 1) {
addRow1(data);
} else {
addRow2(data);
}
}
function postme(numb, rec) {
var x = parent.eval("inventoryAdd(["+numb+"], "+rec+");");
}
</script>
<input onclick="var x='\'asdf1\', \'desc\', \'2\', \'99\', \'\', \'\', \'1\', \'MP1\', \'Me1\', \'99\'';postme(x,1);" type="checkbox" name="inv_1" value="1">
thx gbeg!
not sure if any of that is of any use to anyone.. but it has 'add' and 'remove' and 'edit cells' ability... a pits!
hmm.. enjoy.. ;)
-m