baaGrid User Guide

Index

   
Basic methods  
  baaGrid Create new datagrid object
  display Echo the datagrid to the page
  printGrid Echo the datagrid to the page ( alias for display() )
  showErrors Turn error reporting on
Grid appearance  
  setTableAttr Set table attributes
  setHeadings Set grid column headings
  setHeaderClass Set class name(s) for heading style(s)
  setColClass Set class name(s) for column style(s)
  setTotalClass Set class name(s) for total row style(s)
  setSubTotalClass Set class name(s) for total row style(s)
  setRowClass Set class name(s) for row style(s)
  setWidth Set column width
  setDateFormat Set date format
  setAlign Set column alignment
  setDecPlaces Set number of decimal places
  showColNumbers Show column numbers in header cells (programming aid)
Advanced features  
  setTotal Set a column to be totalled
  setCount Set row count column
  setOnChange Specify a column contents displayed only when value changes
  setCondition Set conditional hilighting for a cell
  hideColumn Hide a selected column
  setLink Set link to URL on a cell
  setXTab Prepares a cross-tab (pivot table) instead of a list
  setGPCol General Purpose - Parse column contents and substitute field values
  setSpan Set colspan and rowspan values
Code samples and tutorial  
Download class  
Download guide with tutorial  

Method Reference



|index|
baaGrid object baaGrid (query [, dbtype[,dbConnection]])
  query a valid SQL query
  dbtype [optional] the type of database you are using. Default is MySQL
  dbConnection [required for ODBC only] the link resource returned by odbc_connect()
   


Pre-defined constants for dbtype are

DB_MYSQL
MySql database (default)
DB_POSTGRESQL
PostgreSQL database
DB_ODBC
Any database via odbc connection
DB_MSSQL
MS SQL database
DB_SYBASE
Sybase database


|index|
display() or printGrid()
  No parameters --
   

Outputs the query results in the formatted table



|index|
showErrors([show])
  show [optional]Boolean 1 or True to turn on error display
   


Error display is off by default

showErrors() is equivalent to showErrors(True)



|index|
setTableAttr(attributes)
  attributes string containing the table attributes
   


Use to set table attributes exactly as they would appear in the <TABLE> tag

eg

setTableAttr("WIDTH=\"600\"")



|index|
setHeadings(headings)
  headings array of column headings or a comma-delimited string
   


By default, the column headings are the table field names. Use this method to override those names. If you specify less headings than there are columns, the remaining headings will be the table field names

eg

setHeadings("First\nName,Last\nName,Email address")



|index|
setHeaderClass(classnames)
  classnames array or comma-delinited list of style class names
   


Apply styles to your table heading cells

The class names must have been defined in your page <STYLE> section

    <style>
    .cl1 {background : #666666}
    .cl2 {background : #C0C0C0}
    </style>
    

The last style specified in the list is applied to the remaining columns

So

setHeaderClass('cl2')
will apply a light-grey background to all header cells and
setHeaderClass('cl1,cl2')
sets the first cell as mid-grey and the rest light-grey



|index|
setColClass(classnames)
  classnames array or comma-delimited list of style class names
   


Apply styles to your table data columns

The class usage is the same as setHeaderCells



|index|
setTotalClass(classnames)
  classnames array or comma-delimited list of style class names
   


Apply styles to your table total row

The class usage is the same as setHeaderCells

If totals are specified and no total class defined then the header classes are used for the totals



|index|
setSubTotalClass(classnames)
  classnames array or comma-delimited list of style class names
   


Apply styles to your table subtotal ros

The class usage is the same as setHeaderCells

If subtotals are specified and no subtotal class defined then the total classes are used for the subtotals, or header classes if no total class defined



|index|
setRowClass(classnames)
  classnames array or comma-delimited list of style class names
   


Apply styles to your table data rows

The class definition is the same as setHeaderCells, however, with row classes, the pattern of styles is repeated throughout the data rows, so

<style>
    .cl1 {background : #666666}
    .cl2 {background : #C0C0C0}
    </style>

    with
    setRowClass('cl1, cl1, cl2, cl2')
gives alternating double grey bands



|index|
setWidth(column, width)
  column the column number to be set (first column id 0)
  width the column width
   


You can set an absolute width

setWidth(2,150)
or a percentage width
setWidth(2,'25%')



|index|
setDateFormat(format)
  format date format string as used in date() function
   


All fields of type date or datetime in the table will be formatted thus.

Default is 'd M y'. ie 01 Jan 03



|index|
setAlign(column, alignment)
  column column number
  alignment text alignment string ('L', 'C' or 'R')
   


L = left
C = centre
R = right



|index|
setDecPlaces(column, places [,alignment])
  column column number
  places number of decimal places
  alignment [optional]text alignment
   


Setting the number of decimal places causes the number to be formated with number_format(). By default, the column is also set as right-aligned unless the align parameter is set to another value.



|index|
showColNumbers([show])
  show optional, default is 'true'
   


Use to show the column numbers in the headings. Useful while programming, especially if you have quite a few columns



|index|
setTotal(column [, places])
  column column number
  places [optional] number of decimal places for column
   


Set totalling for a column. Totals are output at the end of the table. The column will also be right-aligned



|index|
setCount(column)
  column column number
   

Cause a record count to be output in the total row of that column.



|index|
setOnChange(column [, subtotals [, blankrow]])
  column column number
  subtotals 1 if subtotals required on change of value in specified column, 0 if not
  blankrow 1 if blank row required between changes of value in specified column, 0 if not
2 if you want blank row and headings repeated
   


Causes the value in the specified column to be shown only when it changes to a new value. Usually will be called for column 0. Optionally you can request subtotals in which case any columns set by setTotal() will be subtotalled on change of value in specified column.

Record count totals specified for the OnChange column will show the number of unique values in the column rather than the record count. Subtotals on the OnChange column are suppressed.



|index|
setCondition(column, test, value, backgound, foreground [, legend])
  column column number of cell to be hilighted if comparison is true
  test the comparison operator to be applied
  value value that the field value is to be compared with
  background the cell background colour to be used
  foreground text colour to be used
  legend [optional] explanatory text to appear in legend below the grid
   


Comparison operators in the 'test' parameter are the normal php operatore eg '<', '>=' etc.

In addition, for text fields, you can use

    '?' field contains value
    '!' field does not contain value
    

You can define more than one condition for a column. In this case the tests are applied in the order they are specified and the test function returns as soon as a true test result is found. You have to be careful, therefore of the order in which you specify the conditions.

All the above comparisons compare the value in the specified column with the 'value' parameter using the simple comparison.

You can also highlight a cell in one column based on the contents of one or more of the other columns in a row using the final 'test' value '#'

When you use a '#' test, the 'value' parameter should be a string containing a boolean expression. Field values in the expression are specified using #0, #1 etc where 0 and 1 are the column numbers. Functions in the expression can be a php or user functions. If a field value is a string or date, place the #0 in quotes. (As in simple comparisons above, dates are passed unformatted)

The legend heading is a DEFINE at the beginning of the class code for ease of change, particularly for other languages

Examples

Hightlight in blue background a sales value in column 4 if the quantity in column 3 is less than 10

    setCondition(4,'#',"#3 < 10", "#CCCCFF","")
    

Hightlight in red text a sales value in column 4 if the product code in column 1 begins with 'X' and create explanatory legend below the grid.

    setCondition(4,'#',"substr('#1',0,1)=='X'", "","#FF0000","Experimental product sales")
    



|index|
setLink(column, url)
  column column number
  url Specifies the href parameter for the link
   


Enables you to set the cell contents in the column to become links to a url. Use #0, #1 etc to place field values in the url's query string. eg

    setLink(2,"$_SERVER[PHP_SELF]?id=#5&action=delete")
    



|index|
hideColumn(column)
  column column number
   


Stops a columnn from being displayed. Most useful in conjunction with setLink() or setGPCol() where you want to specify an 'id' but not display it. (Recommended that hidden columns are at the end of the selection so the displayed column numbers still remain consecutive.)



|index|
setXTab(row, column, aggregate [, analysis])
  row the data column number whose values are to be the row headings
  column the data column number whose values are to be the column headings
  aggregate the data column number to be aggregated
  analysis [optional]type of analysis. Default is total. Can also be average or count
  • T = Totals
  • A = Averages
  • C = Counts
   


The following methods can be used with setXTab. The effects of other methods are ignored. (See tutorial, section 5)

  • setTableAttr()
  • setDateFormat()
  • setHeadings()
  • setHeaderClass()
  • setColClass()
  • setDecPlaces() on aggregated column



|index|
setGPCol(column[,content])
  column column number
  content optional column contents (added version 1.3a)
   


Causes the contents of the cell to be parsed for #0, #1 etc and the corresponding field values are substituted (as in setLink())


For example, you want to be able to put a checkbox on each row to mark records for block deletion

	$sql = "select id, name, department, 
	'<input type=\"checkbox\" name=\"del[]\" value=\"#0\">' as Del
	from employee order by name";
		   
	and then setGPCol(3);
	

Alternatively, (version 1.3a) ensure query has an extra column as a placeholder
	$sql = "select id, name, department, '' as Del
	from employee order by name";
		   
	and then setGPCol(3, '<input type="checkbox" name="del[]" value="#0">');
	

(version 1.7) Content can be a function call and the result of the function is output
    $sql = "select id, name, department, '' as Result
    from employee order by name";

    eg setGPCol(3, 'myfunction(#1, #2)');
    


|index|
setSpan(column, startnewrow [,colspan [, rowspan ]])
  column column number
  startnewrow 1 to start a new row, zero for same row
  colspan [optional] number of columns to be spanned (values of 0 or 1 are ignored)
  rowspan [optional] number of rows to be spanned (values of 0 or 1 are ignored)
   


Allows you to have multiple rows in the grid for each record in the dataset. Cells can have individual colspan and rowspan values.

Headings and total rows use the same cell pattern

Example : SELECT a, b, c, d, e FROM tablename

a b c
d e
    $grid->setSpan (0,0,0,2);  // field a (col 0) rowspan = 2
    $grid->setSpan (1,0,2);    // field b (col 1) colspan = 2
    $grid->setSpan(2,0,0,2);   // field c (col 2) rowspan = 2
    $grid->setSpan (3,1);      // field d (col 3) starts second row

	

|index|
© Barry Andrew 2003