Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Alim Hassam on Aug 07, 2008 07:24
open dhtmlx forum
Programatically create a block selection

In the dhtmlx grid version 1.6 build 80319.
I would like to programatically create a block selection (in javascript code).
I tried the code given in this other question, but it doesn't work.

http://dhtmlx.com/docs/products/kb/index.php?s=normal&q=4439&a=6354

I'm using the following code and my grid doesn't show anymore.

// Grid initialisation
var mygrid = new dhtmlXGridObject();
mygrid.enableBlockSelection(true);
mygrid.enableDragAndDrop(false);
mygrid.attachEvent("onBlockSelected",function(){
  // Disable selection of first column
  if ( mygrid._selectionArea.LeftTopCol <= 0 || mygrid._selectionArea.RightBottomCol <= 0) {
    return false;
  }
  // This sends the selection to the server.
  gridSelection(mygrid._selectionArea.LeftTopRow,
    mygrid._selectionArea.LeftTopCol - 1,
    mygrid._selectionArea.RightBottomRow,
    mygrid._selectionArea.RightBottomCol - 1 );
  return true;
});
refreshGrid();

//the "refreshGrid" function is called when the grid changes. Sometimes new columns are added and the selection doesn't get redrawn.
function refreshGrid()
{
            mygrid.clearAll();
            mygrid.loadXML(url);
            mygrid.init();

            mygrid._CreateSelection();
            mygrid_ShowSelection();
            mygrid._selectionArea = mygrid._RedrawSelectionPos(
                mygrid.cellByIndex(#{selectedRegion.leftTopRow},#{selectedRegion.leftTopCol}).cell,
                mygrid.cellByIndex(#{selectedRegion.rightBottomRow}, #{selectedRegion.rightBottomCol})).cell;
}

Thank you,
-Alim
Answer posted by Support on Aug 08, 2008 02:16
Code which you are using is not exactly the same as was suggested. 
Please check attached sample
Attachments (1)
Answer posted on Aug 08, 2008 07:58
It works!
Thank you,
I didn't put the code inside a function as a parameter to loadXML.

-Alim
Answer posted by Support on Aug 08, 2008 09:37
The data loading is async, so data related commands need to be loaded only after XML loading, which can be catched by using second parameter of loadXML command
Answer posted by Alim Hassam on Aug 11, 2008 12:07
Would it be possible to do this selection from the xml file as an <afterInit><call/> parameter?

-Alim

Answer posted by Support on Aug 12, 2008 01:46
You can call any method of grid from afterInit section , so it technically possible to call your custom code from afterInit, but it will not work for selection creation , because afterInit section processed when hea section parsed, but data not loaded in grid yet. There is no row in grid - so selection can't be created.