如何将整个dojox.grid.DataGrid存储(项目json数据)传递给servlet?

时间:2013-03-27 01:37:10

标签: json datagrid dojo

我在页面上有一个按钮 - 单击时,它会将所有数据传递给可以更新每个行数据的servlet。我的问题是如何将整个商店作为json数据传递给servlet?有什么简单的方法吗?感谢

1 个答案:

答案 0 :(得分:1)

这是我编写的一些代码,用于将商店存储到对象中。然后可以使用dojo.toJson(obj);将其转换为JSON。我最初从dojotoolkit website了解到了这一点。 (在信用到期时给予信贷)。我意识到这段代码是巨大而令人讨厌的。当我在一年前寻找更好的方法时,我找不到一个。

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;
};
相关问题