1

ページにボタンがあります。クリックすると、すべてのデータがサーブレットに渡され、各行のデータが更新されます。私の質問は、ストア全体をjsonデータとしてサーブレットに渡す方法です。簡単な方法はありますか?ありがとう

4

1 に答える 1

1

これは、ストアをオブジェクトに移動するために作成したコードです。次に、を使用してJSONに変換できますdojo.toJson(obj);。私はもともとdojotoolkitのWebサイトからこれについて学びました。(クレジットが必要な場合はクレジットを付与します)。このコードは巨大で厄介だと思います。約1年前にもっと良い方法を探したとき、私はそれを見つけることができませんでした。

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;
};
于 2013-03-27T01:54:07.897 に答える