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

前端之家收集整理的这篇文章主要介绍了html – Bootstrap typeahead ajax结果格式 – 示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用带有ajax功能的Bootstrap typeahead,并想知道什么是正确的Json结果格式,以返回Id和描述.
我需要Id来使用mvc3模型绑定所选元素.

这是代码

  1. [Html]
  2.  
  3. <input id="myTypeahead" class='ajax-typeahead' type="text" data-link="myUrl" data-provide="typeahead" />
  4.  
  5.  
  6. [Javascript]
  7.  
  8. $('#myTypeahead').typeahead({
  9. source: function (query,process) {
  10. return $.ajax({
  11. url: $('#myTypeahead').data('link'),type: 'post',data: { query: query },dataType: 'json',success: function (jsonResult) {
  12. return typeof jsonResult == 'undefined' ? false : process(jsonResult);
  13. }
  14. });
  15. }
  16. });
  17.  
  18.  
  19.  
  20. This works properly when I return a simple list of strings,for example:
  21. {item1,item2,item3}
  22.  
  23. But I want to return a list with Id,for example:
  24. {
  25. {Id: 1,value: item1},{Id: 2,value: item2},{Id: 3,value: item3}
  26. }

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

这是非常容易的jquery自动完成,因为我可以返回一个Json对象列表.

  1. [jquery Autocomplete process data example]
  2. ...
  3. success: function (data) {
  4. response($.map(data,function (item) {
  5. return { label: item.Id,value: item.Value,id: item.Id,data: item };
  6. })
  7. ...

但是,这并不适用于头发的头发.

有人可以帮我吗

谢谢.

解决方法

我试了两天,终于可以工作了.
Bootstrap Typeahead默认情况下不支持对象数组,只能使用字符串数组.因为“匹配器”,“分类器”,“更新器”和“荧光笔”功能需要字符串作为参数.

相反,“Bootstrap”支持可定制的“匹配器”,“排序器”,“更新器”和“荧光笔”功能.所以我们可以在Typeahead选项中重写这些功能.

II使用Json格式,并将Id绑定到隐藏的html输入.

代码

  1. $('#myTypeahead').typeahead({
  2. source: function (query,process) {
  3. return $.ajax({
  4. url: $('#myTypeahead').data('link'),success: function (result) {
  5.  
  6. var resultList = result.map(function (item) {
  7. var aItem = { id: item.Id,name: item.Name };
  8. return JSON.stringify(aItem);
  9. });
  10.  
  11. return process(resultList);
  12.  
  13. }
  14. });
  15. },matcher: function (obj) {
  16. var item = JSON.parse(obj);
  17. return ~item.name.toLowerCase().indexOf(this.query.toLowerCase())
  18. },sorter: function (items) {
  19. var beginswith = [],caseSensitive = [],caseInsensitive = [],item;
  20. while (aItem = items.shift()) {
  21. var item = JSON.parse(aItem);
  22. if (!item.name.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(JSON.stringify(item));
  23. else if (~item.name.indexOf(this.query)) caseSensitive.push(JSON.stringify(item));
  24. else caseInsensitive.push(JSON.stringify(item));
  25. }
  26.  
  27. return beginswith.concat(caseSensitive,caseInsensitive)
  28.  
  29. },Highlighter: function (obj) {
  30. var item = JSON.parse(obj);
  31. var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,'\\$&')
  32. return item.name.replace(new RegExp('(' + query + ')','ig'),function ($1,match) {
  33. return '<strong>' + match + '</strong>'
  34. })
  35. },updater: function (obj) {
  36. var item = JSON.parse(obj);
  37. $('#IdControl').attr('value',item.id);
  38. return item.name;
  39. }
  40. });

猜你在找的HTML相关文章