Adding multiple rows to subgrid and getSubGrid() is not a function

Two issues:

  1. If the subgrid does not exist and I try to check for it with
    var mysubgrid = maingrid.cells(row,0).getSubGrid();

It returns “not an object”. How do I check if the subgrid exists?

Does it matter if I use cells(row,0) vs cellById(row,0)?

  1. Subgrid exists with rows and I want to add multiple additional rows to it.
    I want to overwrite what is in subgrid with all the new rows. So, 2 old rows, 3 new rows, I write all 5 rows to the subgrid.

maingrid.cellById(row,0).setValue(data); Works 99 out of 100 times and NO errors.
Should I be doing something different?

bbyDRgrid.attachEvent(“onSubGridCreated”,function(subgrid,row,col,value){
subgrid.setHeader(“Item Sku,Item Description,UPC,Units,In Process/Applied,DMQRow”);
subgrid.setColTypes(“ro,ro,ro,ron,ro,ro”);
subgrid.setInitWidths(“100,250,100,100,100,0”);
subgrid.setColAlign(“left,center,left,center,center,right”);
subgrid.setColSorting(“str,str,str,ron,str,str”)
subgrid.csv.row = “^”;
subgrid.csv.cell = “|”;
subgrid.init();
subgrid.setColumnHidden(5,true);
subgrid.clearAll();
subgrid.loadCSVString(value);
// subgrid.callEvent(“onGridReconstructed”,[]); Do I need this???

return false;  // block default behavior

});

Thanks,

Rich

  1. If the subgrid does not exist and I try to check for it with
    var mysubgrid = maingrid.cells(row,0).getSubGrid();

You may try to use:

 if (typeof maingrid.cells(row,0).getSubGrid==="function")
var mysubgrid = maingrid.cells(row,0).getSubGrid();

Does it matter if I use cells(row,0) vs cellById(row,0)?

No, these methods have the similar functionality.

  1. Subgrid exists with rows and I want to add multiple additional rows to it.
    I want to overwrite what is in subgrid with all the new rows. So, 2 old rows, 3 new rows, I write all 5 rows to the subgrid.

Please, try to use the addRow() method for your subgrid.
https://docs.dhtmlx.com/api__dhtmlxgrid_addrow.html
Like:

var mysubgrid = bbyDRgrid.cells(row,0).getSubGrid();
var newId = (new Date()).valueOf();
mysubgrid.addRow(newId,"test,test,test")

// subgrid.callEvent(“onGridReconstructed”,[]); Do I need this???

Yes, it recommended to trigger this event for the correct grid sizes update.

Thanks for your help. I’ve modified my code to check if the function exists. The first time through, I add the subgrid and when the second time through, I check if the subgrid exists. The code goes into the branch where the subgrid exists but hits the .clearAll() and throws an error saying the subgrid is null.

There is data(the row number is correct) in the subgrid and typeof agrees it exists, how could it be null?

var selectedDRDisputeRow = null;
var vSubGridData = "";
var vDRSubGrid;
var vSplitSubGridData = "";
selectedDRDisputeRow = bbyDRgrid.getSelectedRowId();


                alert('hi there data : ' + vSubGridData + " " + vSubGridData.length + " " + selectedDRDisputeRow );

                if (vSubGridData != '') // create subgrid 
                {
                  // does subgrid exist?
                  if (typeof bbyDRgrid.cells(selectedDRDisputeRow,0).getSubGrid==="function") 
                  {
                     alert("subgrid exists ") ;
                     vDRSubGrid = bbyDRgrid.cells(selectedDRDisputeRow,0).getSubGrid(); 
                     
                     
                     vDRSubGrid.clearAll();   <=========== Fails with subgrid is Null
                     alert("after clear all");
                     
                     vDRSubGrid.loadCSVString(vSubGridData);
                  }   
                  else // first time through, create subgrid
                  {
                     bbyDRgrid.setCellExcellType(selectedDRDisputeRow,0,"sub_row_grid"); 
                     bbyDRgrid.cellById(selectedDRDisputeRow,0).setValue(vSubGridData);
                  } 
                } // vSubgrid has data

Regards,

Rich

Reading through the forum, someone had a problem adding a row to a subgrid if the the subgrid was NOT open. I added the open call and the subgrid object is no longer null and my code works.

                  if (typeof bbyDRgrid.cells(selectedDRDisputeRow,0).getSubGrid==="function") 
                  {

                     bbyDRgrid.cells(selectedDRDisputeRow,0).open();
                     
                     vDRSubGrid = bbyDRgrid.cells(selectedDRDisputeRow,0).getSubGrid(); 
                     vDRSubGrid.clearAll();
                     vDRSubGrid.loadCSVString(vSubGridData);
                     
                     bbyDRgrid.cells(selectedDRDisputeRow,0).close();
                     
                  } // first cell contain function?
1 Like