extjs中model的HasMany和belongTo讀取xml資料的用法

來源:互聯網
上載者:User

論壇上看到的提問,於是就研究了一下,終於得出store中list嵌套如何用HasMany和BelongTo實現。 先看介面源碼   <html><head> <!-- Put your page Title here --><title> Cache Server Page </title><!-- 載入ExtJS --> www.2cto.com<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css"/><script src="extjs/resources/ext-all.js"></script><script src="extjs/resources/ext-lang-zh_CN.js"></script></head> <body><script type="text/javascript"> Ext.onReady(function(){Ext.QuickTips.init();Ext.define('common.teacher',{     extend:'Ext.data.Model',     fields:['name'],     proxy: {        type: 'ajax',        url:'Test.TestApplication.TestOneToMany.cls',        reader: {            type: 'xml',            //root: 'teacherList'            record:'teacher'        }     },     hasMany:{model:'common.student', name:'students'} });  Ext.define('common.student',{        extend:'Ext.data.Model',   proxy: {        type: 'ajax',        url:'Test.TestApplication.TestOneToMany.cls',        reader: {            type: 'xml',            root: 'students',            record:'name'        }       },       fields:[{name:'name',mapping:'/'}],   belongsTo:'common.teacher' }); var tstore = Ext.create('Ext.data.Store', {    storeId:'simpsonsStore',    model:'common.teacher'});tstore.load({callback:function(records){console.log(records);alert(records.length);alert(records[0].students().count());}});Ext.create('Ext.grid.Panel', {    title: 'Simpsons',    store:tstore,    title:'老師姓名',    columns: [        { header: 'teName',  dataIndex: 'name' },        {            xtype:'actioncolumn',            width:50,            items: [{           icon:'resources/images/confirm.png',                tooltip: '查看學生姓名',                handler: function(grid, rowIndex, colIndex) {                    var rec = grid.getStore().getAt(rowIndex);                    Ext.create('Ext.window.Window', {                     title: "老師姓名:"+rec.get('name'),                    height: 200,                  width: 400,    layout: 'fit',    items: {  // Let's put an empty grid in just to illustrate fit layout        xtype: 'grid',        border: false,        columns: [        { header: '學生姓名',  dataIndex: 'name',flex:1 },           ],              // One header just for show. There's no data,        store: rec.students() // A dummy empty data store    }}).show();                }            }            ]        }    ],    height: 200,    width: 400,    renderTo: Ext.getBody()});   });</script></body></html>後台從檔案讀取xml檔案的內容: Class Test.TestApplication.TestOneToMany Extends %CSP.Page{ ClassMethod OnPage() As %Status{;Write "<?xml version=""1.0"" ?>",!s file=##class(%FileCharacterStream).%New()s file.Filename="C:/person.xml"w file.Read()Quit $$$OK} Parameter CONTENTTYPE = "text/xml"; } //注意:此後台代碼為ensemble開發平台使用的M語言   載入過來的xml檔案為: <?xml version="1.0" encoding="UTF-8"?><teacherList>    <teacher>        <name>teacher1</name>        <students>            <name>student1_1</name>            <name>student1_2</name>        </students>    </teacher>    <teacher>        <name>teacher2</name>        <students>            <name>student2_1</name>            <name>student2_2</name>        </students>    </teacher></teacherList> 看:  點擊後顯示該老師所帶的學生:   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.