如何将整个dojox.grid.DataGrid存储(项目json数据)传递到servlet? [英] How to pass a whole dojox.grid.DataGrid store(items json data) to servlet?

查看:210
本文介绍了如何将整个dojox.grid.DataGrid存储(项目json数据)传递到servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

这是我写的一个代码,以将商店存储到一个对象。然后可以使用 dojo.toJson(obj); 将其转换为JSON。我最初从 dojotoolkit网站了解到这一点。 (信用额度到期时)。我意识到这个代码是巨大和讨厌的。当我在一年前找到更好的方法时,找不到一个。

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

JsonHelper.itemToObject = function(store,item){
// store:
//项目来自的数据存储区。
// item:
//有问题的项目。
var obj = {};
if(item&& store){
//确定需要处理的属性。
var attributes = store.getAttributes(item);
if(attributes&& attributes.length> 0){
var i; (i = 0; i< attributes.length; i ++){
var values = store.getValues(item,attributes [i]);

if(values){
//处理多值和单值属性。
if(values.length> 1){
var j;
obj [attributes [i]] = [];
for(j = 0; j var value = values [j];
//检查该值不是另一个项目。如果
//它是,将其处理为一个项目。
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,
值[0]);
} else {
obj [attributes [i]] = values [0];
}
}
}
}
}
}
return obj;
};


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

解决方案

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;
};

这篇关于如何将整个dojox.grid.DataGrid存储(项目json数据)传递到servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆