ExtJS的XmlReader字段映射不起作用

时间:2012-01-23 23:48:37

标签: xml extjs

我更新了这个,因为你建议它似乎没有使用更新的映射运行..doesnt似乎让这个Ext.data.XmlReader的字段映射工作。

Ext.require([
    'Ext.data.*',
    'Ext.grid.*'
]);

    Ext.onReady(function(){
    Ext.define('Book',{
        extend: 'Ext.data.Model',
        fields: [
            // set up the fields mapping into the xml doc
            // The first needs mapping, the others are very basic
            'Time', 'UserID', 'Type', 'Description','val0'
        ]
    });


        // create the Data Store
    var store = Ext.create('Ext.data.Store', {
        model: 'Book',
        autoLoad: true,
        proxy: {
            // load using HTTP
            type: 'ajax',
            url: 'audit-v2.xml',
            // the return will be XML, so lets set up a reader
            reader: {
                type: 'xml',
                // records will have an "Item" tag
                //record: 'rows',
               // idProperty: 'ASIN',
                //totalRecords: '@total'
                 root: 'Root',
                 record: 'AuditTrail'
            }
        }
    });


    // create the grid
    var grid = Ext.create('Ext.grid.Panel', {
        store: store,
        columns: [
            {text: "Time", flex: 1, dataIndex: 'Time', sortable: true},
            {text: "UserId", width: 180, dataIndex: 'UserId', sortable: true},
            {text: "Type", width: 115, dataIndex: 'Type', sortable: true},
            {text: "Description", width: 100, dataIndex: 'Description', sortable: true}
            {text: "Value", width: 100, dataIndex: 'Value', sortable: true}
        ],
        renderTo:'example-grid',
        width: 540,
        height: 200
    });
});

这是xml

<?xml version="1.0" encoding="UTF-8"?>
  <Root>
    <AuditTrail>
      <Time>2012-01-10 09:27:30 (GMT-05:00)</Time>
      <UserID>DX</UserID>
      <Type>FY [REC]</Type>
      <Description>Server: wamp</Description>
      <val0>1</val0>
    </AuditTrail>
    <AuditTrail>
      <Time>2012-01-10 09:27:30 (GMT-05:00)</Time>
      <UserID>DX</UserID>
      <Type>FY [REC]</Type>
      <Description>Server:tomcat</Description>
      <val0>1</val0>
    </AuditTrail>
    <AuditTrail>
      <Time>2012-01-10 09:27:30 (GMT-05:00)</Time>
      <UserID>DX</UserID>
      <Type>FY [REC]</Type>
      <Description>Server: apache</Description>
      <val0>1</val0>
    </AuditTrail>
</Root>

它似乎没有加载xml

由于

1 个答案:

答案 0 :(得分:0)

您将阅读器的record属性设置得过高。在您的情况下,我认为最好将root设置为Root,将record设置为AuditTrail。另外,在Book模型上,您应更改mapping字段的time以不进行相对查询,即您的记录字段为AuditTrail,因此您不需要深入查询查询中的某个级别:

    // ...

    Ext.define('Book',{
        extend: 'Ext.data.Model',
        fields: ['Time', 'UserID', 'Type', 'Description','val0']
    });

    // ...

    proxy: {
        // load using HTTP
        type: 'ajax',
        url: 'audit-v2.xml',
        // the return will be XML, so lets set up a reader
        reader: {
            type: 'xml',
            root: 'Root',
            record: 'AuditTrail'
        }
    }

    // ...

您的商店期望数据索引处于AuditTrail元素的级别,这就是为什么我提到您应该将record设置为AuditTrail