Rails 中的动态自动完成,使用 jQuery TokenInput 插件

发布于 2024-11-05 18:37:06 字数 443 浏览 8 评论 0原文

我正在使用很棒的 TokenInput 插件 (http://loopj.com/jquery-tokeninput/)对于 Rails 来说,它可以进行一些自动补全,并且对于模型中的一组普通数据来说,它的工作效果非常好。但我的问题是,就我而言,我实际上有两个文本框我想使用它,第二个文本框的建议应该取决于第一个文本框的内容。 更具体地说:我需要选择一个足球俱乐部(我可以从数据库中的所有现有俱乐部中自动完成),然后选择参加比赛的球队。因此,团队的自动补全应该只使用与已选择的俱乐部相关的团队,我完全不知道如何或即使我可以在 Rails 和这个插件中做到这一点(但插件本身不应该真正成为障碍) )。 我想我的主要问题是缺乏 javascript 的经验,尤其是与 Rails 结合的经验。 有人在他/她的申请中做了类似的事情并且可以帮助我吗?

I'm using the great TokenInput plugin (http://loopj.com/jquery-tokeninput/) for rails to do some autocompletion and for a normal set of data in a model it works just wonderfully. But my problem is, that in my case I have actually 2 textboxes I want to use it on, and the suggestions for the second one should depend on the content of the first.
More specifically: I need to select a football club (which I can just autocomplete out of all exisiting clubs in my database) and then the team that played. So the autocompletion for the team should only use the teams associated with the already selected club and I'm completely clueless about how or even if I can do that in rails and with this plugin (but the plugin itself shouldn't really be a hindrance).
I guess my main problem here is my lack of experience with javascript, especially in combination with rails.
Did anyone do something like that in his/her application and could help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2024-11-12 18:37:06

您的问题可能有许多不同的边缘情况。看一下 tokenInput 方法“onResult”。

也许您可以从第一个输入字段(input-1)获取当前值,并根据 input-1 评估 input-2 返回的 json 并过滤掉您不想要的值。
您显然必须在 Rails 创建的 json 数组中包含用于过滤所需的 id 元素。

<script type="text/javascript">
        $(document).ready(function() {
            $("#input-1").tokenInput("http://railsapp.com/your/search/action");

            $("#input-2").tokenInput("http://railsapp.com/your/search/action", {
                onResult: function (results) {
                    var input-1 = $("#input-1").val();
                    $.each(results, function (index, value) {
                        // some JS to keep only results which match input-1 values
                    });

                    return results;
                }
            });
        });
        </script>

免责声明:这只是一个想法。

希望这有帮助。

Your question could have many different edge cases. Have a look at the tokenInput method 'onResult'.

Maybe you could get the current values from the first input field(input-1) and assess the returned json for input-2 against input-1 and filter out the ones you don't want.
You would obliviously have to include the needed id elements for filtering in the json array that rails creates.

<script type="text/javascript">
        $(document).ready(function() {
            $("#input-1").tokenInput("http://railsapp.com/your/search/action");

            $("#input-2").tokenInput("http://railsapp.com/your/search/action", {
                onResult: function (results) {
                    var input-1 = $("#input-1").val();
                    $.each(results, function (index, value) {
                        // some JS to keep only results which match input-1 values
                    });

                    return results;
                }
            });
        });
        </script>

DISCLAIMER: This is just an idea.

Hope this helps.

当爱已成负担 2024-11-12 18:37:06

感谢您的回答。我知道,我需要使用 onResult 回调,但想错了方向。所以你的回答实际上帮助我找到了正确的方法。我让它以我需要的 95% 的方式工作,用这一点代码来丢弃任何不需要的结果:

if (value.club_id != verein1){
                        results.splice(index,1)     
                    }

但问题是,不幸的是我不得不发现, onResult 回调仅在新搜索时调用。 TokenInput 插件缓存搜索/结果以减少服务器负载。因此,当我在第二个框中输入第一个字符时,它会触发搜索,调用 onResult 并且我的结果会被正确过滤。所以一切看起来都很好。但是,如果我删除第一个字符并再次输入,则插件将使用缓存的结果,并且 onResult 不会被调用,并且结果将显示为未过滤。这意味着,如果我在第一个框中输入俱乐部,然后在第二个框中搜索团队,所有内容都会被正确过滤。但是,如果我随后决定删除俱乐部并输入另一个俱乐部(例如,因为我选择了错误的俱乐部),则团队的结果将不再被正确过滤,因为每次搜索字符串出现两次时,插件都会抛出缓存的搜索结果。
顺便说一句:缓存的结果是原始的、未经过滤的结果,而不是通过我之前的过滤得到的结果数组。

现在我不确定这是否是一个错误,或者是否有意如此,并且我更改插件源代码的努力也没有取得任何成功。

Thanks for your answer. I knew, that i needed to use the onResult callback but thought in the wrong direction. So your answer actually helped me to get my approach right. I got it to work kind of 95% the way I need it to with this little bit of code to throw away any unwanted results:

if (value.club_id != verein1){
                        results.splice(index,1)     
                    }

The Problem is though, that I unfortunately had to discover, that the onResult callback is only called for NEW searches. The TokenInput plugin caches searches/results for less server load. So when I enter the first character in my second box, it triggers a search, onResult is called and my results get filtered correctly. So everything seems fine. But if I delete that first character and type it in again, the plugin uses the cached results and onResult DOESN'T get called and the results appear unfiltered. This means, that if I enter a Club in my first box, then search for a Team in the second box, everything gets filtered correctly. BUT if I then decide to remove the Club and enter another one (because I chose the wrong one for example), the results for the teams doesn't get filtered correctly anymore, because the plugin throws in cached searchresults everytime the search string appears twice.
By the way: The cached results are the raw, unfiltered ones, not the array of results that made it through my previous filtering.

Now I'm not sure if this is a bug or if it's intended to be this way and my efforts to change the sourcecode of the plugin to change that aren't of any succes either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文