Developer's Guide :: Using DBGrid

From RadPHP XE2 Documentation
Jump to: navigation, search

DBGrid uses a Data Dictionary to get several properties for the fields to show. Data Dictionaries can live in a database table or in an array, here is sample code to show how to set Data Dictionary properties using an array:

 
function Unit306BeforeShow($sender, $params)
{
  $prop=array();
  $prop['products']['products_id']=array('displaylabel'=>array('ID'));
  $prop['products']['products_quantity']=array('displaylabel'=>array('Quantity'));
  $this->dboscommerce1->DictionaryProperties=$prop;
}

Frequently used JavaScript methods:

var row = dbgrid.getFocusedRow();
var column = dbgrid.getFocusedColumn(); 
var value=dbgrid.getTableModel().getValue(col,row);
dbgrid.getTableModel().setValue(col,row,value);
dbgrid.sortByColumn (col, true);
dbgrid.setFocusedCell(col, row, true);
dbgrid.getSelectionModel().setSelectionInterval(row, row);

For an example: In your dbgrids javascript onClick event put

              function DBGrid1JSClick($sender, $params)
               {
 
               ?>
var row = dbgrid1.getFocusedRow();
var col = dbgrid1.getFocusedColumn();
var value=dbgrid1.getTableModel().getValue(col,row);
alert('col = '+col+' row = '+row+' cell = '+value);
               <?php
 
               }

And then click around on the different cells of your grid.


A routine to total some columns in a grid

                var totcases=1*0 ,totsale=1*0
                var model = dbgrid.getTableModel();
                var rowCount = model.getRowCount();
                for(var row = 0; row < rowCount; row ++ )
                   {
                   var order=parseInt(model.getValue(5,row));
                   if (order != model.getValue(5,row)) order=0;
                   var multi=parseInt(model.getValue(10,row));
                   if (multi != model.getValue(10,row)) multi=1;
                   var price=parseFloat(model.getValue(6,row));
                   if (price != model.getValue(6,row)) price=0;
                   totcases+=order;
                   totsale+=parseInt(order*multi*price*100)/100;
                   }

A routine that does an incremental search through a dbgrids column.

function  jsDBGfindtext(model,thetext,startingRow,column)
  {  //zips thru a dbgrid to find something
     var rowCount = model.getRowCount();
     for(var row = startingRow; row < rowCount; row ++ )
            {
            var desc = model.getValue(column,row);
            if (typeof(desc)=="number") desc=desc.toString();
            if (typeof(thetext)=="number") thetext=thetext.toString();
            desc = desc.toUpperCase();
            thetext = thetext.toUpperCase();
            if ( desc.indexOf(thetext)>=0) return (row);
            }
      return (-1);
   }
//the calling routine from a basic edit
   function EsearchdbgridJSKeyUp($sender, $params)
               {
               ?>
                var lookfor = findObj('Esearchdbgrid').value;
                if (lookfor.length < 2) startRow = 0;
                if (lookfor.indexOf(' ',0)) //use space bar as a look for next occurrence.
                  {
                  var look = lookfor.split(' ');
                  lookfor=look[0];
                  }
                if (typeof(startRow)=='undefined') startRow=-1;
                startRow++;
                startRow=jsDBGfindtext(dbgrid.getTableModel(),lookfor,startRow,4);
                if (startRow>-1)
                     {
                     dbgrid.setFocusedCell(5, startRow, true);
                     dbgrid.getSelectionModel().setSelectionInterval(startRow, startRow);
                     }
               <?php
               }
Personal tools