-1

我的问题是我想要一个过滤系统,该系统将通过选中的复选框进行过滤。该工具是我比较电视套餐的比较网站。

  1. 我的访客应该按他们想要的电视频道过滤包裹。

例子;
复选框 1:探索
复选框 2:动物星球
复选框 3:迪士尼频道

输出应该是匹配的电视包
包 1:(包含探索和迪士尼频道)
包 2:(包含动物星球,迪士尼频道)
包 3:(包含动物星球)

因此,如果复选框 1 被选中,它应该只显示包 1。
如果复选框 1 + 复选框 2 被选中,它应该说“没有找到匹配项,但是这个包最接近你的选择”
如果复选框 2 + 复选框 3 被选中,它应该只显示与访问者选择完全匹配的包 2。

我希望你能帮助我。我一直在寻找这个特定的解决方案,但没有成功。

4

2 回答 2

0

我认为它应该在 Jquery 中。我看过一些类似的过滤示例,但没有一个像我上面的愿望。

于 2013-03-18T23:04:54.847 回答
0

This is an old question, but... I'll take a shot. Working jsfiddle: http://jsfiddle.net/a0nnrfua/

I think a lot really depends on how you intend to define "closest", but presuming a jQuery solution and hopefully your browser requirements aren't TOO far in the past, you could use the data- attributes and jQuery to come up with some relatively simple functions. Or even use the value portions of the checkboxes really.

Psuedocode, it would look like:

  1. Define a click or change handler to detect whenever a checkbox has been touched/changed.
  2. Define a function that will scan all checked items and pass the values into your "closest package" function.
  3. Based on the results of that function, filter your package selection so that your choices are highlighted or marked in some way.

So let's presume the following HTML markup:

    <h3>TV Channels</h3>

<div id="TVChannelSelections">
    <input type="checkbox" class="tvchannel" name="tvchannel" id="tvchannel_Discovery" value="Discovery" />
    <label for="tvchannel_Discovery">Discovery Channel</label>
    <br/>
    <input type="checkbox" class="tvchannel" name="tvchannel" id="tvchannel_AnimalPlanet" value="Animal Planet" />
    <label for="tvchannel_AnimalPlanet">Animal Planet</label>
    <br/>
    <input type="checkbox" class="tvchannel" name="tvchannel" id="tvchannel_DisneyChannel" value="Disney Channel" />
    <label for="tvchannel_Disney">Disney Channel</label>
    <br/>
</div>

<div id="message"></div>
<h3>Packages</h3>

<div id="FilteredPackages">
    <div class="package deselected" id="Package1" data-channels="Discovery,Disney Channel">Package #1</div>
    <div class="package deselected" id="Package2" data-channels="Animal Planet,Disney Channel">Package #2</div>
    <div class="package deselected" id="Package3" data-channels="Animal Planet">Package #3</div>
</div>

So in jQuery, your generic change or click handler would be defined in code: Note that I'm saying, any element on your page that has the class "tvchannel" defined, if there's ever a change that occurs, run my filter function.

<script type="text/javascript" src="../path/to/jQuery/library"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(".tvchannel").on("change", function() {
            FilterMySelectedChannels();
        });
    });
</script>

Now we can define your Filter function. We're going to assume two things. #1, that we want to find all the selected checkboxes and their values. Then we're going to iterate through the data-channels property of all of our packages (defined as elements with class = "package"). We'll use some form of string comparison and boolean logic to define what a complete match is vs. a close but no cigar match vs. a complete fail.

In order to keep track of things I'm using 3 classes, selected, deselected, and close.

In css, you can decide whether you want notselected to mean "hide the package completely" (i.e. display: none;) or maybe you want it to be visible but greyed out and "struck out" (i.e. text-decoration: strikethrough; color: grey;}

I'm going to use kind of a brute force way of doing the comparison. There are better array functions and comparison functions in javascript, but this should be relatively clear for most people and I trust the good folks at stackoverflow to chime in with better solutions. But this should get you started. :)

<script type="text/javascript">
    function FilterMySelectedChannels() {
        $checkedboxes = $(".tvchannel:checked");
        $packages = $(".package");
        var bAnyFound = false;
        $packages.each(function () {
            var bCloseButNoCigar = false;
            var bCompleteMatch = true;
            var packagearray = $(this).data("channels").split(",");
            var $currentPackage = $(this);
            $checkedboxes.each(function () {
                if ($.inArray($(this).val(), packagearray) != -1) {
                    bCloseButNoCigar = true;
                } else {
                    bCompleteMatch = false;
                }
            });

            if (bCompleteMatch) {
                $currentPackage.removeClass("selected").removeClass("deselected").removeClass("close").addClass("selected");
                bAnyFound = true;
            } else if (bCloseButNoCigar) {
                $currentPackage.removeClass("selected").removeClass("deselected").removeClass("close").addClass("close");
            } else {
                $currentPackage.removeClass("selected").removeClass("deselected").removeClass("close").addClass("deselected");
            }
        });

        if (bAnyFound) {
            $("#message").html("The following matches were found");
        } else {
            $("#message").html("No actual matches were found, but here are some close matches based on your selections");
            $(".package.close").removeClass("deselected").removeClass("close").removeClass("selected").addClass("selected");
        }
    }
</script>

<style type="text/css">
    .selected {
        color: red;
        background-color: yellow !important;
    }

    .deselected {
        color: grey;
        text-decoration: strike-through !important;
        background-color: white !important;
    }
</style>

There are obvious optimizations that could probably work here, but it's a start for those trying to do something similar. Note that it assumes that your markup is dynamically generated or properly coded. If you need to guard against human typos, converting your text using .toLowerCase/UpperCase and using the .Trim functions to eliminate extra space will assist. But you still have to choose your data values wisely so there's no overlap. And if you choose them well enough you can use better techniques such as regular expressions and wildcard searches to make the code a bit shorter.

Hope this helps someone!

于 2014-09-15T15:23:42.130 回答