简体   繁体   English

InstantSearch.js 搜索框的多个实例

[英]Multiple instances of InstantSearch.js searchBox

I am trying to have multiple searchfields.我正在尝试拥有多个搜索字段。 This is what I am trying but it results in "cloned" input fields.这是我正在尝试的,但它会导致“克隆”输入字段。 Is this possible to have multiple search boxes which would work independent of each?这是否可能有多个独立于每个搜索框工作的搜索框?

search.addWidgets([ 
        instantsearch.widgets.searchBox({
            container: '#searchbox',
            placeholder: 'Regular Searchbox',
            autofocus: true,
            searchAsYouType: false,
            showReset: true,
            showSubmit: true
        }),

        instantsearch.widgets.searchBox({
            container: '#searchbox-2',
            placeholder: 'Searchbox to be integrated with AutoComplete',
            searchAsYouType: true,
        })
]);

Any help of alternative solutions on this would be highly appreciated.对此替代解决方案的任何帮助将不胜感激。

Thanks!谢谢!

same question was asked on this forum在这个论坛上问了同样的问题

By wrapping the entire webpage in one InstantSearch instance, it only permits one query - that is why your multiple searchboxes all reflect the same query通过将整个网页包装在一个 InstantSearch 实例中,它只允许一个查询 - 这就是为什么您的多个搜索框都反映相同的查询

therefore a possible alternative would be to create another search instance因此,一种可能的替代方法是创建另一个搜索实例
for example例如

const searchClient = algoliasearch(
  'appId',
  'key'
);

const search = instantsearch({   //instance 1
  indexName: 'instant_search',
  searchClient,
});

const search2 = instantsearch({  //instance 2
  indexName: 'instant_search',
  searchClient,
});

and then create widgets like然后创建像

search.addWidgets([ 
  instantsearch.widgets.searchBox({
      container: '#searchbox',
      placeholder: 'Regular Searchbox',
      autofocus: true,
      searchAsYouType: false,
      showReset: true,
      showSubmit: true
  }),

]);
search2.addWidgets([ 
  instantsearch.widgets.searchBox({
    container: '#searchbox-2',
    placeholder: 'Searchbox to be integrated with AutoComplete',
    searchAsYouType: true,
})
]);

make sure to start them separately as well确保也分别启动它们

search.start();
search2.start();

There is a codesandbox demo in the forum link posted above上面发布的论坛链接中有一个代码框演示

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

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