ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-如何定义将组件添加到DataItem的顺序

2019-11-27 17:35:46  阅读:211  来源: 互联网

标签:sencha-touch-2 javascript


我已经阅读了tutorial,其中涉及为Store中的每个元素创建具有自定义组件的DataView.它谈到了将记录字段映射到DataItem中的组件,但是我的问题是如何定义将组件添加到DataItem中的顺序.

例如,我有这样的DataItem:

Ext.define('demo.view.CottageItem', {
    extend: 'Ext.dataview.component.DataItem',
    requires: ['Ext.Label', 'Ext.Array', 'Ext.Img'],
    xtype: 'listitem',

    config: {
        layout: {
            type: 'hbox',
            align: 'center',
            pack: 'start',
        },
        title: {
            flex: 1
        },
        photo: {
            width: '140px',
            height: '140px'
        },

        dataMap: {
            getTitle: {
                setHtml: 'title'
            },
            getPhoto: {
                setSrc: 'photo'
            }
        },
        styleHtmlContent: true
    },

    applyTitle: function(config) {
        return Ext.factory(config, Ext.Label, this.getTitle());
    },

    updateTitle: function(newTitle, oldTitle) {
        if (oldTitle) {
            this.remove(oldTitle);
        }

        if (newTitle) {
            this.add(newTitle);
        }
    },

    applyPhoto: function(config) {
        return Ext.factory(config, Ext.Img, this.getPhoto());
    },

    updatePhoto: function(newImage, oldImage) {
        if (oldImage) {
            this.remove(oldImage);
        }

        if (newImage) {
            this.add(newImage);
        }
    },
});

它的布局类型为hbox,因此我希望先添加照片,然后再添加标题.这样标题就在照片的右边.我该如何实现?

另一个问题是,是否有办法在此DataItem中添加另一个容器,可以在其中插入我的Label和Image?

更新:
现在我的布局看起来像这样:

|标签(标题)| |图片(照片)|

我希望它看起来像这样:

|图片(照片)| |标签(标题)|

解决方法:

最后,我了解了如何解决我的问题,这是我的代码:

Ext.define('demo.view.CottageItem', {
    extend: 'Ext.dataview.component.DataItem',
    requires: ['Ext.Label', 'Ext.Array', 'Ext.Img'],
    xtype: 'listitem',

    config: {
        layout: {
            type: 'hbox',
            align: 'center',
            pack: 'start',
        },
        title: {
            style: {
                'font-weight': 'bold'
            }
        },
        photo: {
            width: '140px',
            height: '140px'
        },
        desc: {},

        dataMap: {
            getTitle: {
                setHtml: 'title'
            },
            getPhoto: {
                setSrc: 'photo'
            },
            getDesc: {
                setHtml: 'desc'
            },
        },
        styleHtmlContent: true,
        items: [
            {
                xtype: 'panel',
                itemId: 'photoContainer',
            },
            {
                xtype: 'panel',
                layout: {
                    type: 'vbox'
                },
                itemId: 'textContainer',
                flex: 1,
            }
        ],
    },

    applyTitle: function(config) {
        return Ext.factory(config, Ext.Label, this.getTitle());
    },

    updateTitle: function(newTitle, oldTitle) {
        if (oldTitle) {
            this.getComponent('textContainer').remove(oldTitle);
        }

        if (newTitle) {
            this.getComponent('textContainer').add(newTitle);
        }
    },

    applyPhoto: function(config) {
        return Ext.factory(config, Ext.Img, this.getPhoto());
    },

    updatePhoto: function(newImage, oldImage) {
        if (oldImage) {
            this.getComponent('photoContainer').remove(oldImage);
        }

        if (newImage) {
            this.getComponent('photoContainer').add(newImage);
        }
    },

    applyDesc: function(config) {
        return Ext.factory(config, Ext.Label, this.getDesc());
    },

    updateDesc: function(newDesc, oldDesc) {
        if (oldDesc) {
            this.getComponent('textContainer').remove(oldDesc);
        }

        if (newDesc) {
            this.getComponent('textContainer').add(newDesc);
        }
    },
});

我没有在根目录中添加组件,而是在DataItem中定义了两个面板(占位符).我使用getComponent方法访问它们,并将我的组件添加到所需的位置.唯一的问题是,添加到textContainer的项目顺序是不确定的,但是在我的情况下,我得到了所需的顺序.

标签:sencha-touch-2,javascript
来源: https://codeday.me/bug/20191127/2076127.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有