Extjs组合框 - doQuery回调?

时间:2010-06-07 14:48:36

标签: combobox extjs

我正在使用以下组合框:

            var cb = new Ext.form.ComboBox({
            store: someDs,              
            fieldLabel: 'test',
            valueField:'name',
            displayField:'name_id',
            typeAhead: true,
            minChars: 3,
            triggerAction: 'query'
        });

因此,当用户输入3个字符时,会向服务器发出查询,显示正确的结果。

现在我尝试以编程方式使用户输入使用组合框的doQuery()函数。在调用doQuery()方法之后,我想通过setValue()来保存一个Item。

        cb.doQuery('myval');
        cb.setValue('myval');

问题是setValue()无法选择propper值,因为在调用它时,通过doQuery()启动的请求尚未完成。 所以我需要类似回调的东西,我可以使用setValue() - 但doQuery()似乎没有回调函数。

任何想法?谢谢!

2 个答案:

答案 0 :(得分:1)

我正在回答自己,因为评论中没有代码格式化。

Igor:它有效,但感觉就像一个丑陋的黑客,然后是一个干净的解决方案。 为了更好地理解,我解释了这种情况: 我有一个GridPanel。当用户单击此面板上的行时,我想在组合框中预选选定的值。因为有很多数据我想使用组合框的doQuery函数来降低它。 在GridPanel的rowClick-event中,我有以下代码:

var myrec = Ext.getCmp('mygrid').store.getAt(rowIndex);

mycombo.store.on('load', myfn1 = function() {
    // When the store has finisihed loading, select item in the combobox
    mycombo.setValue(myrec.data.customer_id);
    // Remove the function assigend to the load event... 
    // ...(when user types directly to the combobox instead clicking on a row...)
    mycombo.store.un('load', myfn1);
});
// setValue has to be called again, because the load-event doesn't fires
// when doQuery is called with same params
mycombo.setValue(myrec.data.customer_id);
// Call do query to fill the combo only with the relevant values
mycombo.doQuery(myrec.data.customer_id);

答案 1 :(得分:0)

ComboBox有一个beforequery事件,您可以选择拦截查询,改为执行自己的商店加载并取消ComboBox查询。此外,在您的示例中,您将处理程序函数保存到变量,然后从处理程序中调用un。这完全没必要,因为您可以将选项{single: true}作为第四个参数传递给on(第三个参数是scope)。

如果我理解了您的问题,您希望将组合的值设置为某个值,例如客户ID 123。问题是未加载客户123的记录,因此您在商店中没有关于该客户的任何数据。我是对的吗?

你想要做的是(这不一定适合你的情况,只是一个让你入门的方向):

combo.on('beforequery', function(q) {
  q.cancel = true;
  var id = q.query;
  combo.setDisabled(true);
  combo.store.on('load', function() {
    combo.setValue(id);
  }, combo.store, {single: true});
  combo.store.reload({
    customer_id: id
  });

  return q;
});