简体   繁体   English

Rails / JSON:如何使用jQuery UI自动完成表单的JSON

[英]Rails/JSON: How to use JSON for jquery UI autocomplete form

Please note, this is for a rails app. 请注意,这是针对Rails应用的。 I've been working through trying to find the best way to pull data from a json file, and I can't quite seem to find the answer with what I want to do anywhere. 我一直在努力寻找从json文件中提取数据的最佳方法,但我似乎还找不到我想在任何地方做什么的答案。 I have a list of static data, like so (It's a list like 8,000 lines long): 我有一个静态数据列表,如下所示(它的列表长8,000行):

    [
    {"web_page": "http://www.usjc.uwaterloo.ca/", "country": "Canada", "domain": "usjc.uwaterloo.ca", "name": "University of St. Jerome's College"},
    {"web_page": "http://www.ustanne.ednet.ns.ca/", "country": "Canada", "domain": "ustanne.ednet.ns.ca", "name": "St. Anne University"}
    ]

^^ I saved the above in my apps/assets/javascripts folder as universitydata.json ^^我在我的apps / assets / javascripts文件夹中将以上内容另存为universitydata.json

And my javascript: 而我的javascript:

    function updateUniSearch() {
         $("#university-field").autocomplete({
                source: universitydata
            });
    }        

Here's the embedded ruby file where I have the event for the updateUniSearch() function as being on keydown: 这是嵌入式的ruby文件,在该文件中,按下keydown时具有updateUniSearch()函数的事件:

    <%= f.text_field :university, :id => "university-field", :onkeydown=>"updateUniSearch()" %>

So, let's say i define universitydata as the following: 因此,假设我将universitydata定义如下:

   var universitydata = ["Maine", "Harvard"];

And then I go to type in the autocomplete form 'm'- 'Maine" will appear as an option. But in this file I have saved as universitydata.json in my javascript assets folder, nothing happens. Am I setting this up all wrong? Am I saving this json file improperly? Why does it only work with the local array? Thank you in advance. 然后我输入自动完成形式'm'-'Maine'将作为一个选项出现,但是在这个文件中,我将其另存为universitydata.json保存在我的javascript资产文件夹中,没有任何反应。 ?我是否不正确地保存了这个json文件?为什么它只能与本地数组一起使用?预先感谢您。

A few things: 一些东西:

Most autocomplete libs require that you only “bind” to the input once. 大多数自动完成库要求您仅“绑定”一次输入。 Based on your stack overflow post, you have it happening in the onkeydown event. 根据堆栈溢出后的情况,您会在onkeydown事件中发生这种情况。 So every time you type, you're rebinding to the input. 因此,每次键入时,您都会重新绑定到输入。

Consider instead: 考虑改为:

$(document).on('ready', function(){
  $("#university-field").autocomplete({
    source: universitydata
  });
});

This will bind the autocomplete only once (on page load) 这将仅绑定一次自动完成(在页面加载时)

Secondly, and to answer your primary question, if you wanted to load the json data from file, you'd have to issue an AJAX request to load it into a variable. 其次,要回答您的主要问题,如果要从文件加载json数据,则必须发出AJAX请求以将其加载到变量中。 Given the data is static and the dataset is realatively small, it's perfectly fine to have it loaded as a variable before you bind the autocompleter. 鉴于数据是静态的,并且数据集实际上很小,因此在绑定自动完成程序之前将其作为变量加载是完全可以的。 Put all of this (the data and your autocomplete event binding) in a .js file and use 将所有这些(数据和自动完成事件绑定)放入.js文件中,并使用

<script src="/your-path-to-the-javascript.js"></script>

Thirdly, JavaScipt conventions recommend snake case for variable names, so consider naming your variable universityData 第三,JavaScipt约定建议对变量名使用蛇形,因此请考虑为变量命名为universityData

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

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