【问题标题】:Sencha touch 2 filterby() not updating recordsSencha touch 2 filterby() 不更新记录
【发布时间】:2012-11-19 22:30:08
【问题描述】:

我在从“offices.json”提取数据的选项卡式面板应用程序的一个页面上有一个嵌套列表

我希望能够在用户单击工具栏按钮时过滤此列表。但是,我的 filterBy() 函数不会更新商店和我可以看到的办公室列表,即使我可以在控制台中看到它正在迭代记录并找到匹配项。我究竟做错了什么? (是的,我在 filterBy 之前和之后都尝试过 s.load() 无济于事!)

toolbar:{                        
   items:[{
           text: 'Near you',
           id: 'btnNearYou',
           xtype: 'button',
           handler: function() {
             s = Ext.StoreMgr.get('offices');
             s._proxy._url = 'officesFLAT.json';        
             console.log("trying to filter");
             s.filterBy(function(record) {
              var search = new RegExp("Altrincham", 'i'); 
              if(record.get('text').match(search)){
               console.log("did Match");
               return true;
              }else {
               console.log("didnt Match");
               return false;
             }
           });
           s.load();
          }                            
   }]

为了记录,我这样定义我的商店:

store: {
    type: 'tree',
    model: 'ListItem',
    id: 'offices',
    defaultRootProperty: 'items',
    proxy: {
        type: 'ajax',
        root: {},
        url: 'offices.json',
        reader: {
            type: 'json',
            rootProperty: 'items'
        }
    }
}

【问题讨论】:

    标签: touch sencha-touch-2


    【解决方案1】:
    1. 不需要每次都重新创建正则表达式,把它缓存在外面。

    2. 您可以大大简化代码(见下文)。

    3. 为什么之后直接调用 load?这会将它发送到服务器,它只会检索相同的数据集。

    toolbar: {
        items: [{
            text: 'Near you',
            id: 'btnNearYou',
            xtype: 'button',
            handler: function() {
                s = Ext.StoreMgr.get('offices');
                s._proxy._url = 'officesFLAT.json';
                var search = /Altrincham/i;
                s.filterBy(function(record) {
                    return !!record.get('text').match(search);
                });
            }
        }]
    }
    

    【讨论】:

    • 我将上面的代码添加到按钮的处理函数中,但没有任何反应。该函数触发(我可以通过console.log看到)但nestedList不会重新加载过滤记录。如果我调用 s.load(),我只能得到一些东西(然后正如你所说,你得到了整个数据集)也许我应该在商店级别设置一些东西?我将它添加到上面的主要问题中......
    • 我认为我的问题源于尝试对 NestedList 执行此操作。从网上搜索看来,在 Sencha Touch 中过滤 NestedList 似乎是不可能的。但是,如果这是一个普通的列表,那么上面的代码确实可以工作,所以感谢 Evan!
    猜你喜欢
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多