首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jQuery-Autocomplete定制建议html

如何使用jQuery-Autocomplete定制建议html
EN

Stack Overflow用户
提问于 2016-07-08 11:18:55
回答 2查看 2.1K关注 0票数 2

我正在使用这里看到的jQuery-Autocomplete插件:https://github.com/devbridge/jQuery-Autocomplete

我正在尝试控制作为建议呈现的项目。默认情况下,autocomplete-suggestion如下所示:

代码语言:javascript
复制
<div class="autocomplete-suggestion" data-index="0">438</div>

我希望能够构建该项目,这样我就可以执行以下操作:

代码语言:javascript
复制
<div class="autocomplete-suggestion" data-index="0">
    <div class="autocomplete-suggestion-photo"></div>
    <div class="autocomplete-suggestion-title"></div>
</div>

我使用的是正在调用的beforeRender,只是没有生效,下面是代码片段:

代码语言:javascript
复制
beforeRender: function (container) {
  console.log(container)
  xx = '<div class="autocomplete-suggestion" data-index="1">TEST</div>';
  return xx;
},

你知道我做错了什么吗?

更新了下面的代码。想法是更改beforeRender中的容器,以完全自定义正在呈现的内容。这没有效果。

代码语言:javascript
复制
beforeRender: function (container) {
  console.log(container)
  container = '<div class="autocomplete-suggestion" data-index="1">TEST</div>';
  return container;
},
EN

回答 2

Stack Overflow用户

发布于 2016-07-08 13:07:26

查看source,该插件只需调用beforeRender,然后显示容器:

代码语言:javascript
复制
if ($.isFunction(beforeRender)) {
  beforeRender.call(that.element, container, that.suggestions);
}

that.fixPosition();
container.show();

它不使用任何返回值。医生说:

beforeRender:在显示建议之前调用的function (container, suggestions) {}。您可以在显示建议DOM之前对其进行操作。

所以我认为你应该直接操作容器,比如:

代码语言:javascript
复制
beforeRender: function (container, suggestions) {
  container.find('.autocomplete-suggestion').each(function(i, suggestion){
   // suggestion.append(); suggestion.preppend(); suggestion.whatever()
  });
},
票数 3
EN

Stack Overflow用户

发布于 2020-06-20 16:39:06

这是一个老问题,但也面临着类似的问题。我想在div中显示结果,而不是默认的下拉列表。我想在任何人有这样的需求的情况下更新解决方案。

首先,在beforeRender方法之前调用formatResult方法。此外,该插件还为容器添加了内联样式属性,使容器的位置变为绝对位置。

现在回到最初的问题,正如@T J在答案中提到的那样,该方法不使用返回值,因此我们必须在方法本身中对容器进行更改。这是我想出来的。

代码语言:javascript
复制
beforeRender: function (container) {
    
    var html = "";
    
    //get inner html of each of the suggestion
    container.find('.autocomplete-suggestion').each(function(i, suggestion){
        html = html + $(suggestion).html();
    });
    
    //append the html to the div where you want to display the results
    $("#auto-results").html(html);
    
    /*
    * remove the inline style from the container that makes the position absolute
    * and other styles
    */
    $(container).removeAttr("style");
    
    //empty the inner html of the container
    $(container).html("");
},

这样,就不会显示默认容器,但所有建议都会显示在我自己的容器div中。

请记住,单个建议样式是由formatResult方法处理的。

您可以使用相同的方式对容器进行您想要的更改。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38258427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档