简体   繁体   English

使用typeahead.js和jquery ajax调用

[英]Using typeahead.js with jquery ajax call

I am using 我在用

http://twitter.github.io/typeahead.js/ version 0.9.3 http://twitter.github.io/typeahead.js/版本0.9.3

and JQuery version 2.0.3 JQuery version 2.0.3

I have the below example, which I know works correctly. 我有下面的示例,我知道它可以正常工作。

<input id="subCategory" name="subCategory" type="text" />
<script>
    $('#subCategory').typeahead({
        name: "subCategory",
        local: ["how", "when", "where", "who", "why"],
        limit: 10
    });
</script>

How can I then change this so that I can use the successful result from an AJAX request for JSON? 然后,如何更改此设置,以便可以将AJAX请求的成功结果用于JSON?

The below example does not work, my first thought is because it is not waiting for the response from $.getJSON() and I to poll for updates or wait until the async call finishes. 下面的示例不起作用,我的第一个想法是因为它没有等待$.getJSON()的响应, $.getJSON()我轮询更新或等待异步调用完成。

<script>
    $('#subCategory').typeahead({
        name: "subCategory",
        local: $.getJSON("/subcategories/all/"),
        limit: 10
    });
</script>

My first thought is that I would have to apply the typeahead configuration above inside the success callback of the $.getJSON() function instead? 我首先想到的是,我必须在$.getJSON()函数的成功回调中应用上面的$.getJSON()配置吗? is there a better way of approaching this? 有没有更好的方法来解决这个问题?

The JSON call is to an MVC controller action that returns a JSONResult similar to this basic example below: JSON调用是对MVC控制器操作的,该操作返回类似于以下基本示例的JSONResult

    public ActionResult All()
    {
        return Json(_subCategoryService.GetAll(), JsonRequestBehavior.AllowGet);
    }

I have tested and know that this getJSON request works correctly. 我已经测试过,并且知道此getJSON请求可以正常工作。


UPDATE: 更新:

I get a bit further when thinking about it and doing the below instead of an async call, but the typeahead data shows 1 item as undefined but this seems more appropriate, however I was only intending to populate the full list once and then filter that on the client, rather than make this remote call when someone is typing into the input box with a query parameter. 我在考虑它并执行以下操作而不是异步调用时走得更远,但是预输入数据显示1个项目undefined但这似乎更合适,但是我只打算填充一次完整列表,然后对其进行过滤客户端,而不是在有人使用查询参数在输入框中键入内容时进行此远程调用。

<script>
    $('#subCategory').typeahead({
        name: "subCategory",
        remote: "/subcategories/all/",
        limit: 10
    });
</script>

UPDATE 2: 更新2:

I also now am realising that my first example is a list of primitives where as my subcategories is not :( duh.. example: 我现在也意识到,我的第一个示例是一个原始列表,其中作为我的子类别不是:( duh .. example:

[{ id: 1, name: "subcategory-1" }, { id: 2, name: "subcategory-2" }]

So now I am starting to look at the typeahead prefetch option and the filter attribute on that but I am really trying to use this as if it was a dropdown, so want to select the Id as the backing value for a particular entry in the list 因此,现在我开始查看typeahead prefetch选项和上的filter属性,但是我实际上是在尝试使用它,好像它是一个下拉列表一样,所以想选择Id作为列表中特定条目的支持值


UPDATE 3: 更新3:

Since I was trying to use the typeahead input as if it was a combobox, I have since altered my example, but using local data rather than my JSON response and the below works and stores the backing id value in a hidden field. 由于我试图像使用组合框一样使用typeahead输入,因此我更改了示例,但是使用本地数据而不是JSON响应,并且下面的方法将后备id值存储在隐藏字段中。

    <input id="subCategorySelection" type="hidden" />
    <input id="subCategory" name="subCategory" type="text" />

<script>
    $("#subCategory").typeahead({
        name: "subCategories", // the name for the dataset
        local: [{ id: 1, name: "subcategory-1" }, { id: 2, name: "subcategory-2" }],
        limit: 10,
        valueKey: "name" // the value shown in the textbox
    }).on("typeahead:selected typeahead:autocompleted",
        function(e, datum) {
            $("#subCategorySelection").val(datum.id);
        }
    );
</script>

I'm afraid this is not yet supported, atleast when I looked at it some weeks ago. 恐怕尚不支持此功能,至少在几周前进行查看时。

But...there's this pull request that does exactly what you are trying to do. 但是...确实有这个拉取请求可以执行您要尝试执行的操作。

https://github.com/twitter/typeahead.js/pull/220 https://github.com/twitter/typeahead.js/pull/220

The below is an example of doing it within the success callback, but I don't really like how this needs to be used. 下面是在成功回调中执行此操作的示例,但我并不真正喜欢这种用法。

    $.getJSON("/subcategories/all/", null, function(response) {
        $("#subCategory").typeahead({
            name: "subCategories", // the name for the dataset
            local: response,
            limit: 10,
            valueKey: "name"
        }).on("typeahead:selected typeahead:autocompleted",
            function(e, datum) {
                $("#subCategorySelection").val(datum.id);
            }
        );
    });

for now I don't need to do this anyway and have gone for this solution using prefetch 现在我仍然不需要这样做,并且已经使用prefetch来解决此问题

    $("#subCategory").typeahead({
        name: "subCategories", // the name for the dataset
        prefetch: {
            url: "/subcategories/all/", 
            ttl: 360000 /* 1 hour of local storage */
        },
        limit: 10,
        valueKey: "name"
    }).on("typeahead:selected typeahead:autocompleted",
        function(e, datum) {
            $("#subCategorySelection").val(datum.id);
        }
    );

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

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