我面临为algolia-autocomplete设置facetFilters的困难。如果我想根据一些组织进行过滤:
source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5, facetFilters:[ ['organization_id:1', 'organization_id:10'], ['listing_status:Published' ]] })
效果很好。
现在,我想从文本框中选择值,它不起作用。如果文本框的值为
<input type="hidden" id="marketplace_organization_ids" value="'organization_id:1', 'organization_id:10'">
source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5, facetFilters:[ [$('#marketplace_organization_ids').val()], ['listing_status:Published' ]] }),
也尝试过使用数组,但没有结果:
var name = ["'organization_id:1'","'organization_id:10'"];
source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5, facetFilters:[ [name.join(',')], ['listing_status:Published' ]] })
我已将上述内容用于一个类似的组织,并且t运作良好:
source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5, facetFilters:[ ['organization_id:'+$('#user_organization_id').val() , 'distributors.id:'+$('#user_organization_id').val()] ] }),
感谢任何帮助。
答案 0 :(得分:1)
由于未解析从输入中提取的值,因此您似乎在facetFilters中传递了错误的值。
source: $.fn.autocomplete.sources.hits(index, {
hitsPerPage: 5,
facetFilters:[
[$('#marketplace_organization_ids').val()],
['listing_status:Published']
]
})
评估为
source: $.fn.autocomplete.sources.hits(index, {
hitsPerPage: 5,
facetFilters:[
["'organization_id:1', 'organization_id:10'"],
['listing_status:Published']
]
})
根据Algolia's documentation about facetFilters,您应该提供字符串数组而不是数组的字符串表示形式,例如
source: $.fn.autocomplete.sources.hits(index, {
hitsPerPage: 5,
facetFilters:[
['organization_id:1', 'organization_id:10'], // no more double-quotes
['listing_status:Published']
]
})
您可以通过在将值传递到数组之前分割值来实现:
const organizationFilter = $('#marketplace_organization_ids').val()
.split(',') // split string into array
.map(filter => filter.trim()) // remove whitespaces
console.log(organizationFilter); // ['organization_id:1', 'organization_id:10']