【问题标题】:Populating the value of a Label填充标签的值
【发布时间】:2012-07-09 15:27:36
【问题描述】:

我正在使用 MVC 架构(我已经阅读了 MVC 上的文档,但我仍然在这里迷路)并且我需要知道如何将记录填充到我的标签上。我知道我必须由商店完成这项工作。

我已从商店加载了值,但由于某种原因无法在我的面板上显示它。这是我的代码;

大多数示例/书籍都演示了如何在网格中显示,而不是在标签中显示(我知道它必须是相同的逻辑,但我迷路了)。它显示了如何写入 DB/JSON 文件而不显示值。

我需要在标签文本中显示 COUNTRYNAME。这是我正在做的一个示例代码来理解这一点,有人可以帮助我吗?

Ext.define ('ProjectDisplayExample.model.Country',{
    extend: 'Ext.data.Model',
    //
    fields:['countryid','countryname']
    //
});

商店

Ext.define('ProjectDisplayExample.store.Country',{
    extend:'Ext.data.Store',
    model:'ProjectDisplayExample.model.Country',
    remoteGroup:true,
    proxy: {
        actionMethods : {
            read   : 'POST',
        },
        type: 'ajax',
        url : '/server/country.php'
    }   
});

查看

Ext.define('ProjectDisplayExample.view.CountryWindow', {
    extend: 'Ext.window.Window',
    alias: 'widget.countrywindow',
    ...........

    initComponent: function() {
        var st = Ext.getStore('Country');
        st.load();
        this.items = [
        {
            items: [
            {
                xtype: 'panel',
                region: 'north',
                items: [{
                    xtype: 'label',
                    // NEED TO DISPLAY COUNTRT NAME FROM THE STORE HERE
                }]
            }]
}

更新

var store = ... // Your store
store.on('load', function() {
    // here store is loaded and you can do anything you want with it
    console.log('store is loaded. number of records = ', store.getCount());
}, this, { single: true });

store.load; // I ADDED THIS LINE.................... <---------

更新 2

this.items = [
            {
                items: [
                {
                    xtype: 'panel',
                    region: 'north',
                    items: [{
                        xtype: 'label',
                        name : f
                    }]
                }]

【问题讨论】:

    标签: extjs extjs4 extjs-mvc extjs4.1


    【解决方案1】:

    我不会发布代码示例来完全解决您的问题,但我会给您几点:

    • Store 包含数组或记录。所以你不能只在商店里说give me country name。您需要先获取一条记录,例如:var r = store.getAt(0),然后才能获取countyname字段var f = r.get('countryname')

    • Load() 方法是异步的,因此您可以在代码中的某处执行它,并假设您的存储已准备好下一行。您需要订阅load 事件,例如:

      var store = ... // Your store
      store.on('load', function() {
          // here store is loaded and you can do anything you want with it
          console.log('store is loaded. number of records = ', store.getCount());
      }, this, { single: true });
      store.load();
      
    • xtype: label 中的标签实际上很少在 ExtJs 中使用。您到底想在该面板中显示什么?但无论如何......从存储中获取数据后,您可以使用update()setValue() 或任何其他方法来更新组件。

    希望这会有所帮助...

    【讨论】:

    • 谢谢,这很有帮助。但是,除非我添加store.load(),否则加载事件(store.on('load', function())不会被执行。我已经更新了上面的帖子,添加了代码store.load();。你能检查我是否做对了吗?
    • 是的,你是对的——你首先订阅load事件,然后你执行load()。我错过了。我会更新我的答案。
    • 一切正常,我能够成功地在Console.log 上显示文本。但是,面板在 store.load() 之前加载,hapence。因此,文本值不会打印在面板上。我认为我不必将 Panel 代码粘贴到 load 事件中。有没有办法刷新面板,所以它会加载文本值?
    • 您需要使用 query() 函数找到该控件。查看docs.sencha.com/ext-js/4-0/#!/api/…这篇文章
    • 根据我的代码,它的`var panelArray = Ext.ComponentQuery.query('#label panel'); `。然后我应该如何添加文本。我在这里迷路了:S
    猜你喜欢
    • 2021-07-25
    • 2020-04-20
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    相关资源
    最近更新 更多