【问题标题】:Show the item hit content only when the search box is not empty仅当搜索框不为空时才显示项目命中内容
【发布时间】:2018-03-04 00:50:33
【问题描述】:

我在我的 jekyll 网站的 algolia 文件中有这个。

<script>
const search = instantsearch({
  appId: '{{ site.algolia.application_id }}',
  apiKey: '{{ site.algolia.search_only_api_key }}',
  indexName: '{{ site.algolia.index_name }}',
  searchParameters: {
    restrictSearchableAttributes: [
      'title',
      'content'
    ],
    facetFilters: ['type:post']
  },
});

const hitTemplate = function(hit) {
  let date = '';
  if (hit.date) {
    date = moment.unix(hit.date).format('L');
    // date = moment.unix(hit.date).format('MMM Do YY');
    modifiedDate = moment.unix(hit.last_modified_at).format('MMM Do YY');
  }
  const url = hit.url;
  const title = hit._highlightResult.title.value;
  const content = hit._highlightResult.html.value;

  return `
    <div class="post-list">
      <span class="post-date-list-wrap">
      <span class="post-date-list">${date}
      <span class="post-title"><a href=".${url}"> ${title} </a> </span>
      </span>
       ${content}
    </div>
  `;
}

const hitTemplateTrans = function(hit) {
  let date = '';
  if (hit.date) {
    date = moment.unix(hit.date).format('MMM DD YYYY');
  }
  const url = hit.url;
  const title = hit._highlightResult.title.value;
  const content = hit._highlightResult.html.value;

  return `
    <div class="post-list">
      <span class="post-date-list-wrap">
      <span class="post-date-list">${date}
        <span class="post-title"><a href=".${url}"> ${title}</a></span>
       </span>
       </span>
    </div>
  `;
}

search.addWidget(
  instantsearch.widgets.searchBox({
    container: '#search-searchbar',
    placeholder: 'search notes',
    autofocus: true
  })
);

search.addWidget(
  instantsearch.widgets.hits({
    container: '#search-hits',
    templates: {
      empty: 'No results',
      item: hitTemplate
    },
  })
);

search.start();
</script>

无需在搜索框中输入任何内容,我就有了文章列表 附摘录,文章的简短介绍。 那是因为我有${content} 来显示某人时的亮点 输入搜索词。

很好,一切正常,但是...当搜索框为空时,我不想显示每个项目的内容。 如果搜索框为空,我只想保留标题和日期 但如果搜索框不为空,则照常显示标题/日期和摘录。

这似乎是一个简单的任务,但我现在卡住了,我尝试删除内容标签并放入命中转换功能,但它不起作用。

【问题讨论】:

    标签: search algolia


    【解决方案1】:

    instantsearch.js 库有一个函数挂钩,称为searchFunction,您可以在实例化库时定义。这是在执行任何搜索之前调用的。您可以使用它来检查当前查询是否为空,并根据此知识调整布局。

    这是一个稍微编辑过的脚本(删除了不相关的部分),它应该可以让你做你想要的:

    let additionalClass = '';
    const search = instantsearch({
      […]
      searchFunction: function(helper) {
        if (helper.getState().query === '') {
          additionalClass = 'post-item__empty-query';
        } else {
          additionalClass = '';
        }
        helper.search()
      }
    });
    
    […]
    
    const hitTemplate = function(hit) {
      return 
        `<div class="post-item ${additionalClass}">
            […]
        </div>`
      ;
    }
    
    .post-item__empty-query .post-snippet {
      display: none;
    }
    

    它所做的是定义一个将在命中模板中使用的全局变量 (additionalClass)(在根级别与 item-post 一起添加)。

    在每次搜索之前,我们检查查询是否为空。如果是这样,我们将additionalClass 设置为item-post__empty_query。我们还在 CSS 中定义,当应用这个类时,内容应该被隐藏。

    所有这些一起使不执行搜索时显示标题+日期,仅在搜索实际关键字时显示内容。

    希望对你有帮助,

    【讨论】:

    • 谢谢,这正是我需要的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多