html - Bootstrap typeahead ajax 结果格式 - 示例

标签 html ajax asp.net-mvc-3 twitter-bootstrap bootstrap-typeahead

我正在使用带有 ajax 函数的 Bootstrap typeahead,想知道什么是正确的 Json 结果格式,以返回一个 Id 和一个描述。 我需要 Id 将预先选择的元素与 mvc3 模型绑定(bind)。

这是代码:

    [Html]

    <input id="myTypeahead" class='ajax-typeahead' type="text" data-link="myUrl" data-provide="typeahead" />


    [Javascript]

    $('#myTypeahead').typeahead({
        source: function (query, process) {
            return $.ajax({
                url: $('#myTypeahead').data('link'),
                type: 'post',
                data: { query: query },
                dataType: 'json',
                success: function (jsonResult) {
                    return typeof jsonResult == 'undefined' ? false : process(jsonResult);
                }
            });
        }
    });



This works properly when I return a simple list of strings, for example:
{item1, item2, item3}

But I want to return a list with Id, for example:
{
 {Id: 1, value: item1},
 {Id: 2, value: item2},
 {Id: 3, value: item3}
}

如何在 ajax "success: function()"中处理这个结果?

使用 jquery Autocomplete 这很容易,因为我可以返回一个 Json 对象列表。

[jquery Autocomplete process data example]
...            
success: function (data) {
                response($.map(data, function (item) {
                    return { label: item.Id, value: item.Value, id: item.Id, data: item };
                })
...

但这不适用于 boostrap Typeahead。

谁能帮帮我?

谢谢。

最佳答案

我试了两天,终于可以用了。 Bootstrap Typeahead 默认不支持对象数组,只支持字符串数组。因为“matcher”、“sorter”、“updater”和“highlighter”函数需要字符串作为参数。

相反,“Bootstrap”支持可自定义的“matcher”、“sorter”、“updater”和“highlighter”功能。所以我们可以在 Typeahead 选项中重写这些函数。

我使用了 Json 格式,并将 Id 绑定(bind)到一个隐藏的 html 输入。

代码:

$('#myTypeahead').typeahead({
    source: function (query, process) {
        return $.ajax({
            url: $('#myTypeahead').data('link'),
            type: 'post',
            data: { query: query },
            dataType: 'json',
            success: function (result) {

                var resultList = result.map(function (item) {
                    var aItem = { id: item.Id, name: item.Name };
                    return JSON.stringify(aItem);
                });

                return process(resultList);

            }
        });
    },

matcher: function (obj) {
        var item = JSON.parse(obj);
        return ~item.name.toLowerCase().indexOf(this.query.toLowerCase())
    },

    sorter: function (items) {          
       var beginswith = [], caseSensitive = [], caseInsensitive = [], item;
        while (aItem = items.shift()) {
            var item = JSON.parse(aItem);
            if (!item.name.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(JSON.stringify(item));
            else if (~item.name.indexOf(this.query)) caseSensitive.push(JSON.stringify(item));
            else caseInsensitive.push(JSON.stringify(item));
        }

        return beginswith.concat(caseSensitive, caseInsensitive)

    },


    highlighter: function (obj) {
        var item = JSON.parse(obj);
        var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
        return item.name.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
            return '<strong>' + match + '</strong>'
        })
    },

    updater: function (obj) {
        var item = JSON.parse(obj);
        $('#IdControl').attr('value', item.id);
        return item.name;
    }
});

关于html - Bootstrap typeahead ajax 结果格式 - 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14901535/

相关文章:

javascript - 基本 xmlHttp 问题

javascript - 通过 AJAX 阅读后显示视频 blob

javascript - 发布方法不起作用

asp.net-mvc-3 - 部署到 azure 计算模拟器时缺少静态内容

html - 在没有异物的情况下模拟 SVG 中可编辑的内容

html - Chrome 正在覆盖我的 CSS 中指定的字体大小

javascript - innerHTML 和 addEventListener 不适用于 html 和 javascript 网站

html - 如何将具有 Z 索引的内容居中

asp.net-mvc - 如何访问 Razor 编辑器模板中包含的模型实例?

c# - Visual Studio MVC3 强类型 View 展示一切