简体   繁体   中英

Sencha Touch XML List

Hei i have a Question about XML Lists with Sencha Touch.

I load some XML in Sencha Touch and i would like to display them as a List:

Here's my Code: Sencha:

Ext.setup({  
onReady: function() {   
Ext.regModel('navModel', {
  fields: [
       {name:'id'},
       {name:'title'}
       ]
});

var navi = new Ext.data.Store({
    autoLoad:true,
    model: 'navModel',
    method: 'POST',
    proxy: {
    type: 'ajax',
    url : 'navi.xml',
    reader: {
    type : 'xml',
root: 'menu',
record:'navigation'
 }
}  
});
   var something = new Ext.List({
        store: 'navi',
        title: 'asdfasdf',
        emptyText: 'No data',
        fulscreen: true,
        itemTpl: '{id} - {title}'
    });


       var rootPanel = new Ext.TabPanel({
            fullscreen:true,
            items:[something]
        }); 

    }

});

XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <menu>
    <navigation>
    <id>1</id>
    <title>Hello</title>
    </navigation>
    <navigation>
    <id>2</id>
    <title>Test</title>
    </navigation>
   </menu>

I can read the XML but it doesn't display it in the List.. what's wrong??

Thx for Answers

尝试将TabPanel更改为Container,这可能会对您有所帮助!

You have to write the name of the store without the quote sign.

And for all who got problems with mapping...

you have to add this workaround on the end of sencha-touch.js:

Ext.override( Ext.data.XmlReader, {
    createAccessor: function() {
        var selectValue = function(key, root, defaultValue){
            if( key == '#' ){
                return root.tagName;
            }
            if( key.indexOf( '@' ) != -1 ){
                var property = key.split( '@' )[ 1 ];
                key = key.split( '@' )[ 0 ];
            }
            var val;
            if( key.length ){
                var node = Ext.DomQuery.selectNode(key, root);
                if( node && node.firstChild ){
                    node = node.firstChild;
                }
            }
            else{
                var node = root;
            }
            if(node){
                if( typeof( node.getAttribute ) != 'undefined' && typeof( property ) != 'undefined' ){
                    val = node.getAttribute( property );
                }
                else{
                    val = node.nodeValue;
                }
            }
            return Ext.isEmpty(val) ? defaultValue : val;
        };

        return function(key) {
            var fn;

            if (key == this.totalProperty) {
                fn = function(root, defaultValue) {
                    var value = selectValue(key, root, defaultValue);
                    return parseFloat(value);
                };
            }

            else if (key == this.successProperty) {
                fn = function(root, defaultValue) {
                    var value = selectValue(key, root, true);
                    return (value !== false && value !== 'false');
                };
            }

            else {
                fn = function(root, defaultValue) {
                    return selectValue(key, root, defaultValue);
                };
            }

            return fn;
        };
    }(),
});

so you can access the attribute of your xml with '@attributename'

and if you want to have access to the child attributes you write 'childname@attributename'

I hope i helped you.. searched some hours for this.. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM