How to pass a whole dojox.grid.DataGrid store(items json data) to servlet?

孤者浪人 提交于 2019-12-11 13:52:06

问题


I have a button on page - when clicked, it passes all the data to the servlet that could update each row data. My question is how to pass the whole store to the servlet as json data? Is there any easy way? Thanks


回答1:


Here is some code I wrote to get the store to an object. Then it can be converted to JSON using dojo.toJson(obj);. I learned about this from the dojotoolkit website originally. (Give credit where credit is due). I realize this code is huge and nasty. When I looked for a better way about a year back I could not find one.

JsonHelper.storeToObject = function(store) {
    var object = [];
    var index = -1;
    store.fetch({
        onItem : function(item, request) {
            object[++index] = JsonHelper.itemToObject(store, item);
        }
    });
    return object;
};

JsonHelper.itemToObject = function(store, item) {
    // store:
    // The datastore the item came from.
    // item:
    // The item in question.
    var obj = {};
    if (item && store) {
        // Determine the attributes we need to process.
        var attributes = store.getAttributes(item);
        if (attributes && attributes.length > 0) {
            var i;
            for (i = 0; i < attributes.length; i++) {
                var values = store.getValues(item, attributes[i]);
                if (values) {
                    // Handle multivalued and single-valued attributes.
                    if (values.length > 1) {
                        var j;
                        obj[attributes[i]] = [];
                        for (j = 0; j < values.length; j++) {
                            var value = values[j];
                            // Check that the value isn't another item. If
                            // it is, process it as an item.
                            if (store.isItem(value)) {
                                obj[attributes[i]].push(itemToObject(store,
                                        value));
                            } else {
                                obj[attributes[i]].push(value);
                            }
                        }
                    } else {
                        if (store.isItem(values[0])) {
                            obj[attributes[i]] = itemToObject(store,
                                    values[0]);
                        } else {
                            obj[attributes[i]] = values[0];
                        }
                    }
                }
            }
        }
    }
    return obj;
};


来源:https://stackoverflow.com/questions/15650541/how-to-pass-a-whole-dojox-grid-datagrid-storeitems-json-data-to-servlet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!