Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Steve Schreiner on Sep 17, 2008 05:55
open dhtmlx forum
serialize modified grid rows into json

In a grid, I would like to serialize modified rows (or any rows I choose) into your json format. Do you have a javascript example on how to accomplish this?

Thanks!
Answer posted by Support on Sep 17, 2008 09:53
Unfortunately, grid can be serialized only into xml and csv srting. Also there are some internal methods which allow to serialize selected rows to xml or csv. If you need, we can provide you this information.
Answer posted by Steve Schreiner on Sep 17, 2008 09:56
I was able to write some javascript to create the json I needed:

function getAllRowsAsJson() {
   
    var json = "{rows:[";
   
    for (var rowIndex=0; rowIndex<mygrid.getRowsNum(); rowIndex++) {
        json = json + "{id:" + mygrid.getRowId(rowIndex) + ",data:[";
        for(var cellIndex = 0; cellIndex < mygrid.getColumnsNum(); cellIndex++){
            if (cellIndex==0){
                json = json + '"' + mygrid.cells2(rowIndex,cellIndex).getValue() + '"';
            }
            else {
                json = json + "," + '"' + mygrid.cells2(rowIndex,cellIndex).getValue() + '"';
            }
        }
        if (rowIndex<(mygrid.getRowsNum()-1)){
            json = json + "]},";
        }
        else {
            json = json + "]}";
        }      
    }
    json = json + "]}";
   
    return json.toString();

}
Answer posted by Support on Sep 18, 2008 02:37
This approach is absolutely correct and can be used in order to serialize to json string. Thank you for provided  method.