简体   繁体   English

HTML:其他搜索过滤器控件

[英]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 在这里我有几个问题

  • how to keep the border as long as there is at least one selected checkbox and remove it only when there are 0 selected? 如何保持边框,只要有至少一个选中的复选框,并仅当选中了0时才将其删除? please note that there is more than one filters set! 请注意,有多个过滤器!
  • I think this could be some sort of a library for people to use (with some adjustments of course). 我认为这可能是供人们使用的某种库(当然要进行一些调整)。 Where do I start with this? 我从哪里开始呢?
  • Is there any better to apply this whole procedure? 有没有更好的方法来应用整个过程? adding a filter , removing the checkbox? 添加过滤器,删除复选框?

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.

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