【问题标题】:Find certain words in tagfield values在标签字段值中查找某些单词
【发布时间】:2015-04-23 17:43:59
【问题描述】:

Extjs 5.

我的应用有一个带有标签字段的表单,其中包含 200 多个项目的列表。

其中大约 30 个项目包含“另一件事”(例如)。 每次我选择其中一项时,都应该将一个文本区域动态添加到表单中。

我尝试了多种解决方案但均未成功,包括使用 match()、indexOf() 和 search() javascript 方法。

onSelect : function (combo, records, eOpts) {
    var records = combo.getValue();
    for (var i = 0, count = records.length; i < count; i++) {
            while( records[i] == '%another thing%'){ //I know this is not the right way; Just to show what I'm looking for
                console.log('OK'); //logic...
                return;
            }
        }
  },

我将不胜感激。

提前致谢。

【问题讨论】:

  • 你能在Sencha Fiddle上发布一个例子吗?另外,我不太确定 while 循环的目的是什么……这看起来很奇怪。
  • 感谢您帮助 incutonez。

标签: extjs5


【解决方案1】:

试试这个代码:

onSelect: function (field, records, opts) {
    // records parameter already contains all selected tags
    var found = Ext.Array.filter(records, function(r) {
        // conditions go here
        return r.get('text') === 'aardvark' || 
            r.get('text') === 'aardwolf';
    });
    // check if we found anything
    if (found.length > 0) {
        console.log(found);
    }
}

小提琴:http://jsfiddle.net/wsm6an0n/2/

如果你想使用通配符搜索,使用indexOf而不是比较:

onSelect: function (field, records, opts) {
    // records parameter already contains all selected tags
    var found = Ext.Array.filter(records, function(r) {
        // conditions go here
        return r.get('text').indexOf('aa') !== -1; // LIKE '%aa%'
    });
    // check if we found anything
    if (found.length > 0) {
        console.log(found);
    }
}

小提琴:http://jsfiddle.net/rq90eLh4/1/

【讨论】:

  • 感谢 Krzysztof。这正是我想要完成的。它帮助很大。
  • 嗨,Krzysztof。更多细节:当我删除标签字段中选择的项目之一时,如何删除动态添加的字段?再次感谢。小提琴:fiddle.sencha.com/#fiddle/lpb
  • 我想最简单的方法是使用beforedeselect 事件。从select 切换到beforeselect 也使代码更简单:jsfiddle.net/rq90eLh4/3
  • 再次感谢 Krzysztof。很棒。
猜你喜欢
  • 1970-01-01
  • 2013-09-15
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
相关资源
最近更新 更多