[英]HTML: Additional search filters control
I am using Node.js as a backend and JQuery as a frontend and I have a search box with filters as checkboxes below it see the next image 我将Node.js用作后端,将JQuery用作前端,并且我有一个带有过滤器的搜索框作为其下方的复选框,请参见下图
Now when I click on the filter it will be added to a used filter list also provided as an image
现在,当我单击过滤器时,它将被添加到也用作图像提供的使用过的过滤器列表中
Then when i click on the x next to the filter it will be deleted and also unchecked. 然后,当我单击过滤器旁边的x时,它将被删除并且也未选中。 all of the prev things are working in the next code:
所有先前的事情都在下一个代码中起作用:
$('[id^="facet-"]').on('change',function(){
let xMark = '<i id="'+$(this).attr('id')+'-remove-filter" class="fa fa-times-circle" aria-hidden="true"></i>'
let key = $(this).data('key')
let keyVal = '<div id="'+$(this).attr('id')+'-span" class="btn"><span>'+$(this).data('key')+ ' > ' +$(this).data('value')+'</span>'+xMark+'</div>'
if($(this).is(':checked')){
$('.'+key+'-filters').append(keyVal)
}else{
$('#'+$(this).attr('id')+'-span').remove()
}
$('i[id$="-remove-filter"]').on('click',function(){
let checkboxId = $(this).attr('id').split('-remove-filter')[0]
$('#'+checkboxId).prop('checked', 0)
$(this).parent().remove()
})
})
As I said it's working and good so far, what should happen next is when you click on search it will take all checkboxes and post them to get some search results (this is also working since I surrounded the page with one form. 就像我说的那样,到目前为止它是有效的,接下来应该发生的是,当您单击“搜索”时,它将选中所有复选框并发布它们以获取一些搜索结果(这也是可行的,因为我用一种表格将页面包围了。
What I need is when the filters set has at least one checkbox inside it to be bordered like the next photo 我需要的是,当滤镜组内部至少有一个复选框要像下一张照片一样被边框化时
I did this using the following piece of code: 我使用下面的代码来做到这一点:
$('[aria-controls="'+$(this).data('key')+'-facets"]').addClass('has-selected')
This will keep it bordered whenever some filter has been selected once and I can remove the border using 只要选择了某个过滤器,就可以保持边框,我可以使用
$('[aria-controls="'+$(this).data('key')+'-facets"]').removeClass('has-selected')
Here I have couple of questions 在这里我有几个问题
Let me know if this works, I sadly can't test it: 让我知道这是否有效,但遗憾的是我无法对其进行测试:
$('[id^="facet-"]').on('change',function(){
var id = $(this).attr('id')
var key = $(this).data('key')
var value = $(this).data('value')
if($(this).is(':checked')){
let xMark = $('<i id="'+id+'-remove-filter" class="fa fa-times-circle" aria-hidden="true"></i>')
let keyElement = $('<div id="'+id+'-span" class="btn"><span>'+key+ ' > ' +value+'</span></div>')
$('.'+key+'-filters').append(keyElement)
keyElement.append(xMark)
xMark.bind('click',function(){
let checkboxId = id.split('-remove-filter')[0]
$('#'+checkboxId).prop('checked', 0).change()
// adding the change() method here will trigger the event all over again to control adding/removing class
$(this).parent().remove()
})
}else{
$('#'+id+'-span').remove()
}
var panel = $('[aria-controls="'+key+'-facets"]')
var checked_num = panel.find('input[type=checkbox]:checked').length
if(checked_num>0)
panel.addClass('has-selected')
else
panel.removeClass('has-selected')
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.