updateFromJS() method

There is an updateFromXML, I do not think it’s sister function for json exists – updateFromJS, so I created it and called it mergeJs().

This accepts the Native JSON data object and then:

  • deletes rows which are missing
  • add rows which need to be added
  • when the 2nd parameter is “true”, updates each cell if needed
dhtmlXGridObject.prototype.mergeJs = function(js, columnProcess){	
  if (columnProcess == undefined) columnProcess = false;
  var i, r, c, a, found
  var rIds = this.getAllRowIds().split(',');

  for (i=0; i<rIds.length; i++){
    found = false;
    for (r=0; r < js.total_count; r++){
       if (rIds[i] == js.data[r].id) {
        found = true;
        continue;
      }
    }
    if (!found){
      this.deleteRow(rIds[i]);
    }
  }

  for (r=0; r < js.total_count; r++){
    if (this.getRowIndex(js.data[r].id) == -1) {
      a = [];  
      for (c=0; c<this.getColumnsNum(); c++){
        a.push( js.data[r][this.getColumnId(c)])
      }
      this.addRow(js.data[r].id, a)
    }
    else if (columnProcess) {
      this.mergeRowJs(js.data[r])
    }
    
  }
}

dhtmlXGridObject.prototype.mergeRowJs = function(js){	
  var c, colVal, colName;
  
  for (c=0; c<this.getColumnsNum(); c++) {
    colVal  = this.cells(js.id, c).getValue()
    colName = this.getColumnId(c)
    if (colVal != js[colName] ) {    
      this.cells(js.id, c).setValue(js[colName])
    }
  }  
}