Is there an updateFromJson() method equivilent to updateFromXML()? Couldn’t find a doc entry on it. Please direct me to the docs if so.
There is no such method. But you can use:
mygrid.clearAll();//remove all rows
mygrid.load(url,“json”);// load new one
if you don’t want to reload all data, you can use a custom created function :
dhtmlXGridObject.prototype._updateOneRow = function(rId,data){
if (this.doesRowExist(rId))
for(var i=0;i<data.length;i++) this.cells(rId,i).setValue(data[i]);
};
dhtmlXGridObject.prototype.updateFromJSON = function(json,insert_new,del_missed,callback){
//Setting the parameters
if (typeof insert_new == 'function') {
callback=insert_new;insert_new=false;
}
if (typeof del_missed == 'function') {
callback=del_missed;del_missed=false;
}
insert_new=!!insert_new;
del_missed=!!del_missed;
if (typeof json == 'string')
try{
json=window.dhx4.s2j(json)
}
catch (e){
console.log('Error parsing json data:'+e);
return false;
}
var del_rows_ids=[];
if (del_missed)
//If del_missed we get all rows id in grid in a array.We will exclude form deletion the rows that whe have in input json
del_rows_ids=this.getAllRowIds().split(",");
for(var i=0;i<json.rows.length;i++){
var r=json.rows[i];
if (this.doesRowExist(r.id)) {
this._updateOneRow(r.id, r.data); // update row data
if (del_missed)
del_rows_ids.remove(r.id);//remove from deletion array a row that exist in input json
}
else{
if (insert_new) this.addRow(r.id, r.data,null, r.parent, r.image, r.xmlkids); //insert new row (treegrid call format) ... if we have normal grid only first two parameters apply
}
}
if (del_missed)
//delete remaining rows that were not present in input json
for(var i=0;i<del_rows_ids.length;i++)
this.deleteRow(del_rows_ids[i])
if(typeof callback=='function') {
var grid=this;
callback.call(this,grid,json)
}
};
json have to be an valid object or string , keeping the structure of usual load json data
var json='{rows:[{id:"id1",data:["d11","d12","d13"],{id:"id2",data:["d21","d22","d23"]}]}';
//OR
var json={
rows:[
{id:"id1",data:["d11","d12","d13"],
{id:"id2",data:["d21","d22","d23"]}
]};
mygrid.updateFromJSON(json)
//or
mygrid.updateFromJSON(json,function(gid,json){})
//or
mygrid.updateFromJSON(json,true,false,function(grid,json){})
to get data from server you can use window.dhx4.ajax.get
window.dhx4.ajax.get("php/get_my_data.php",function(loader){
mygrid.updateFromJSON(loader.xmlDoc.responseText)
})
you have to define 2 prototypes used on array : indexOf and remove
//if ie8 or older
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
// 1. Let O be the result of calling ToObject passing
// the this value as the argument.
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var O = Object(this);
// 2. Let lenValue be the result of calling the Get
// internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If len is 0, return -1.
if (len === 0) {
return -1;
}
// 5. If argument fromIndex was passed let n be
// ToInteger(fromIndex); else let n be 0.
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
// 6. If n >= len, return -1.
if (n >= len) {
return -1;
}
// 7. If n >= 0, then Let k be n.
// 8. Else, n<0, Let k be len - abs(n).
// If k is less than 0, then let k be 0.
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
// 9. Repeat, while k < len
while (k < len) {
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the
// HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
// i. Let elementK be the result of calling the Get
// internal method of O with the argument ToString(k).
// ii. Let same be the result of applying the
// Strict Equality Comparison Algorithm to
// searchElement and elementK.
// iii. If same is true, return k.
if (k in O && O[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
//remove an element from array by value
if (!Array.prototype.remove){
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
}