【问题标题】:Customising Algolia instantsearch searchbox and results自定义 Algolia 即时搜索搜索框和结果
【发布时间】:2018-07-30 23:22:28
【问题描述】:

我正在构建一个使用 Algolia instantsearch.js 来显示搜索结果的服务。

在开始项目时,我创建了一个模板,在表格中显示了我们的客户。然后我想添加当用户输入特定客户信息(例如手机号码)时表格内容发生变化的功能。这就是我使用 Algolia 和 Instantsearch.js 来实现这一目标的原因。

我设法让它工作了,但我在设计整个东西时遇到了问题:

searchboxhits 小部件(显示结果的地方)被添加到具有特定 HTML/CSS 结构的 DOM:

搜索框:

<!- Input field -->
<input autocapitalize="off" autocomplete="off" autocorrect="off" placeholder="Search for customers.." role="textbox" spellcheck="false" type="text" value="" class="ais-search-box--input">

<!- Algolia Powered by logo -->
<span class="">
    <div class="ais-search-box--powered-by">
      ...
      <a class="ais-search-box--powered-by-link">...</a>
    </div>
</span>

<!- Search magnifier icon -->
<span class="ais-search-box--magnifier-wrapper">
    <div class="ais-search-box--magnifier">...</div>
</span>

<!- Clear search icon -->
<span class="ais-search-box--reset-wrapper" style="display: none;">
    <button type="reset" title="Clear" class="ais-search-box--reset">...</button>
</span>

点击次数:

<div class="ais-hits">
    <div class="ais-hits--item">
        First customer data (all of them)
    </div>
    <div class="ais-hits--item">
        Second customer data (all of them)
    </div>
    <div class="ais-hits--item">
        Third customer data (all of them)
    </div>
    <div class="ais-hits--item">
        First customer data (all of them)
    </div>
</div>

尝试处理这些结果令人沮丧,因为我已经准备好 HTML/CSS 代码(结果的搜索字段和表格旨在与网站其余部分的外观和感觉相匹配)。

我想从搜索中得到一个响应,我可以使用它并将数据添加到正确的位置。例如,现在我试图用客户的数据填充,但我不能:

<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Surname</th>
            <th>Email</th>
            <th>Mobile</th>
        </tr>
    </thead>
    <tbody>
        <tr id="hits"></tr>
    </tbody>
</table>

下面是我使用的模板:

<script type="text/html" id="hit-template">
    <td>@{{ name }}</td>
    <td>@{{ surname }}</td>
    <td>@{{ email }}</td>
    <td>@{{ mobile }}</td>
</script>

由于我使用的是 Laravel 和刀片模板,因此我在值前面添加了 @。

我得到的是一个表格,其中包含来自所有客户的所有数据。只创建一个表格行。

在 JS 代码中,我使用以下代码:

var search = instantsearch({
    appId: 'my-app-id',
    apiKey: 'my-search-only-api-key',
    indexName: 'my-indices',
    urlSync: false,
    searchParameters: {
        hitsPerPage: 10
    }
});

search.addWidget(
    instantsearch.widgets.searchBox({
        container: '#searchbox',
        placeholder: 'Search for customers..',
        poweredBy: true,
        wrapInput: false
    })
);

search.addWidget(
    instantsearch.widgets.hits({
        container: '#hits',
        templates: {
            item: document.getElementById('hit-template').innerHTML,
            empty: "We didn't find any results for the search <em>\"{{query}}\"</em>"
        }
    })
);


search.start();

有没有办法只从即时搜索而不是小部件获取 JSON 响应?我不想沿着服务器站点路线走,因为这是instantsearch.js 的重点。

如果我无法返回简单的 JSON 响应,我必须做些什么来更改小部件的布局。覆盖所有 ais 类?

提前致谢!

【问题讨论】:

    标签: javascript html customization algolia instantsearch.js


    【解决方案1】:

    您无法完全控制使用小部件 API 呈现的内容。但是还有另一个 API 称为连接器。这使您可以创建自定义渲染,而无需重新实现小部件的核心逻辑。您可以在 the documentation 上查看此 API。

    【讨论】:

    • 非常感谢!使用 connectSearchbox 和 connectHits 我设法搞定了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 2018-09-05
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    相关资源
    最近更新 更多