0

下面是 Sencha.com Ext JS 4 示例中“cell-editing.js”文件的片段。我想使用 json.get 将此 xml 数据存储转换为 json 数据存储。并获取一个示例 json 文件,以便我可以将其转换为动态 json。请注意,请参阅“// 创建数据存储”下的代码块。现在,它显示的是 xml 格式。如何才能做到这一点?

例子:

http://dev.sencha.com/deploy/ext-4.1.0-gpl/examples/grid/cell-editing.html

(4.1.0 的版本将来可能会改变)

我对他们的 treegrid.js 文件做了类似的事情,但是因为单元格编辑示例的结构包含多种字段类型(字符串、组合框/选择、布尔值等),我不知道json文件需要。这是 treegrid.js 代码:

var store = Ext.create('Ext.data.TreeStore', {
    model: 'Task',
    proxy: {
        type: 'ajax',
        url: 'http://localhost:8888/TreeGrid.ashx'  // OR set to static file 'treegrid.json'
    },
    folderSort: true
});

“cell-editing.js”文件:

Ext.Loader.setConfig({
    enabled: true
});
Ext.Loader.setPath('Ext.ux', '../ux');

Ext.require([
    'Ext.selection.CellModel',
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.state.*',
    'Ext.form.*',
    'Ext.ux.CheckColumn'
]);

Ext.onReady(function(){

    function formatDate(value){
        return value ? Ext.Date.dateFormat(value, 'M d, Y') : '';
    }

    Ext.define('Plant', {
        extend: 'Ext.data.Model',
        fields: [
            // the 'name' below matches the tag name to read, except 'availDate'
            // which is mapped to the tag 'availability'
            {name: 'common', type: 'string'},
            {name: 'botanical', type: 'string'},
            {name: 'light'},
            {name: 'price', type: 'float'},
            // dates can be automatically converted by specifying dateFormat
            {name: 'availDate', mapping: 'availability', type: 'date', dateFormat: 'm/d/Y'},
            {name: 'indoor', type: 'bool'}
        ]
    });


    // create the Data Store
    var store = Ext.create('Ext.data.Store', {
        // destroy the store if the grid is destroyed
        autoDestroy: true,
        model: 'Plant',
        proxy: {
            type: 'ajax',
            // load remote data using HTTP
            url: 'plants.xml',
            // specify a XmlReader (coincides with the XML format of the returned data)
            reader: {
                type: 'xml',
                // records will have a 'plant' tag
                record: 'plant'
            }
        },
        sorters: [{
            property: 'common',
            direction:'ASC'
        }]
    });

    var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
        clicksToEdit: 1
    });

    // create the grid and specify what field you want
    // to use for the editor at each header.
    var grid = Ext.create('Ext.grid.Panel', {
        store: store,
        columns: [{
            id: 'common',
            header: 'Common Name',
            dataIndex: 'common',
            flex: 1,
            editor: {
                allowBlank: false
            }
        }, {
            header: 'Light',
            dataIndex: 'light',
            width: 130,
            editor: {
                xtype: 'combobox',
                typeAhead: true,
                triggerAction: 'all',
                selectOnTab: true,
                store: [
                    ['Shade','Shade'],
                    ['Mostly Shady','Mostly Shady'],
                    ['Sun or Shade','Sun or Shade'],
                    ['Mostly Sunny','Mostly Sunny'],
                    ['Sunny','Sunny']
                ],
                lazyRender: true,
                listClass: 'x-combo-list-small'
            }
        }, {
            header: 'Price',
            dataIndex: 'price',
            width: 70,
            align: 'right',
            renderer: 'usMoney',
            editor: {
                xtype: 'numberfield',
                allowBlank: false,
                minValue: 0,
                maxValue: 100000
            }
        }, {
            header: 'Available',
            dataIndex: 'availDate',
            width: 95,
            renderer: formatDate,
            editor: {
                xtype: 'datefield',
                format: 'm/d/y',
                minValue: '01/01/06',
                disabledDays: [0, 6],
                disabledDaysText: 'Plants are not available on the weekends'
            }
        }, {
            xtype: 'checkcolumn',
            header: 'Indoor?',
            dataIndex: 'indoor',
            width: 55
        }],
        selModel: {
            selType: 'cellmodel'
        },
        renderTo: 'editor-grid',
        width: 600,
        height: 300,
        title: 'Edit Plants?',
        frame: true,
        tbar: [{
            text: 'Add Plant',
            handler : function(){
                // Create a model instance
                var r = Ext.create('Plant', {
                    common: 'New Plant 1',
                    light: 'Mostly Shady',
                    price: 0,
                    availDate: Ext.Date.clearTime(new Date()),
                    indoor: false
                });
                store.insert(0, r);
                cellEditing.startEditByPosition({row: 0, column: 0});
            }
        }],
        plugins: [cellEditing]
    });

    // manually trigger the data store load
    store.load({
        // store loading is asynchronous, use a load listener or callback to handle results
        callback: function(){
            Ext.Msg.show({
                title: 'Store Load Callback',
                msg: 'store was loaded, data available for processing',
                modal: false,
                icon: Ext.Msg.INFO,
                buttons: Ext.Msg.OK
            });
        }
    });
});

“植物.xml”文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <plant>
    <common>Bloodroot</common>
    <botanical>Sanguinaria canadensis</botanical>
    <zone>4</zone>
    <light>Mostly Shady</light>
    <price>2.44</price>
    <availability>03/15/2006</availability>
    <indoor>true</indoor>
  </plant>
  <plant>
    <common>Columbine</common>
    <botanical>Aquilegia canadensis</botanical>
    <zone>3</zone>
    <light>Mostly Shady</light>
    <price>9.37</price>
    <availability>03/06/2006</availability>
    <indoor>true</indoor>
  </plant>
</catalog>
4

2 回答 2

1

将代理阅读器类型更改为“json”

于 2012-04-27T18:39:58.967 回答
1

从第 82 页开始,我能够在“Ext JS 4 First Look”一书中找到有关 Ext-JS 数据存储主题的一些文档。请注意,我必须在阅读器匿名对象上添加“root”属性。我注释掉了 XML 格式的行,并用 JSON 实现替换了这些行。我还包含了一个示例 JSON 文件。“light”字段是一个 ComboBox(选择标签或下拉列表的 Ext-JS 版本)。确保这些值与实际对象中定义的值一致。

“cell-editing.js”文件:

// create the Data Store
var store = Ext.create('Ext.data.Store', {
    // destroy the store if the grid is destroyed
    autoDestroy: true,
    model: 'Event',
    proxy: {
        type: 'ajax',
        // load remote data using HTTP
        //url: 'plants.xml',
        url: 'plants.json',
        // specify a XmlReader (coincides with the XML format of the returned data)
        reader: {
            //type: 'xml',
            type: 'json',
            // records will have a 'plant' tag
            //record: 'plant',
            root: 'plants'
        }
    },
    sorters: [{
        property: 'common',
        direction:'ASC'
    }]
});

“植物.json”文件:

{
    "plants": [
        {
            "common": 'Bloodroot',
            "botanical": 'Sanguinaria canadensis',
            "zone": 4,
            "light": 'Mostly Shady',
            "price": 2.44,
            "availability": '03/15/2006',
            "indoor": 'true'
        },
        {
            "common": 'Bloodroot',
            "botanical": 'Sanguinaria canadensis',
            "zone": 4,
            "light": 'Mostly Shady',
            "price": 2.44,
            "availability": '03/15/2006',
            "indoor": 'true'
        }
    ]
}
于 2012-04-27T19:23:52.060 回答