简体   繁体   English

JavaScript YUI自动完成源更改

[英]JavaScript YUI Autocomplete Source change

I'm using YUI for an auto completion and I need the data for the auto completion to change dynamically. 我正在使用YUI进行自动完成,并且需要自动完成的数据才能动态更改。

For example I have an input box which says "user name" and it will autocomplete according to function(userName) which would return an array accordingly. 例如,我有一个输入框,上面写着“用户名”,它将根据function(userName)自动完成,该函数将相应地返回一个数组。

How do I implement that? 我该如何实现?

One way to do this is to define the source of the AutoComplete as a function: 一种方法是将AutoCompletesource定义为一个函数:

new Y.AutoComplete({
    // your code here ...
    source: getSource
});

In that function you can return a dynamically modified array of results: 在该函数中,您可以返回动态修改的结果数组:

function getSource() {
    if (something) {
        return [ 'something' ];
    }
    else {
        return [ 'somethingElse' ];
    }
}

Here's a runnable example: (click the button to change the completion array) 这是一个可运行的示例:(单击按钮更改完成数组)

 YUI().use('autocomplete', function(Y) { var reversed = false; function getSource() { if (!reversed) { return ['foo', 'bar', 'baz']; } else { return ['oof', 'rab', 'zab']; } } new Y.AutoComplete({ inputNode: Y.one('#input'), render: true, source: getSource }); Y.one('#button').on('click', function(event) { reversed = !reversed; }); }); 
 <script src="https://cdn.rawgit.com/stiemannkj1/0214fdc4cccaa77d6e504320cf70a571/raw/63d260364730fb067f103c00862c7e65685256df/yui-3.18.1_build_yui_yui-min.js"></script> <input id="input" /> <button id="button">Reverse</button> 

Note: You can use the sendRequest() method to programmatically requery the source. 注意:您可以使用sendRequest()方法以编程方式重新查询源。 This can be useful if you've changed the list and want to show the new list before the user types anything new. 如果您已经更改了列表并希望在用户键入任何新内容之前显示新列表,这将很有用。

For more information on dynamic sources for AutoComplete , check out the "Result Sources" section of the YUI AutoComplete Article/Documentation. 有关AutoComplete动态源的更多信息,请查看YUI 自动完成文章/文档的“结果源”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM