简体   繁体   English

自定义Algolia instantSearch.js addWidget函数

[英]customize Algolia instantSearch.js addWidget function

I want to edit the container of Hits , based on the results returned by Algolia, assume the following: hits-container 我想根据Algolia返回的结果编辑Hits容器,假设如下: hits-container

<div id="hits">
    <div class="col-md-2">{{ id }}</div>
    <div class="col-md-2">{{ user }}</div>
    <div class="col-md-2">{{ email }}</div>
    <div class="col-md-2">{{ date }}</div>

    <a href="/orders/{{id}}/edit" class="btn" >
        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
    </a>
</div>

addWidget function : addWidget函数:

<script type="text/javascript">
search.addWidget(
    instantsearch.widgets.hits({
        container: '#hits-container',
        templates: {
            item: $('#hits').html(),
            empty: 'No Orders',
        }
    })
);
</script>

suppose I want to disable the edit link if the date is less than the current date, addWidget will display all the results returned by Algolia inside this container , now I want to apply some logic before displaying the results. 假设我想在日期小于当前日期时禁用编辑链接,addWidget将显示Algolia在此容器中返回的所有结果,现在我想在显示结果之前应用一些逻辑。 any suggestions ? 有什么建议么 ?

InstantSearch.js widget templates can be defined either as a Mustache string or as a function receiving the widget data and returning a string. InstantSearch.js小部件模板可以定义为Mustache字符串,也可以定义为接收小部件数据并返回字符串的函数。

This function could contain the logic about disabling your edit link. 此功能可能包含有关禁用编辑链接的逻辑。 Something like: 就像是:

templates: {
  item: function(data) {
    let className = "";
    if (data.rating < 4) {
      className = "bad-rating";
    }
    return (
      "<div" +
      ' class="' +
      className +
      '">' +
      data._highlightResult.title.value +
      "</div>"
    );
  }
}

Here is a complete jsfiddle showcasing it. 这是一个完整的jsfiddle展示它。

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

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