根据搜索输入隐藏/显示div

时间:2017-11-14 15:00:12

标签: javascript html css

我已经制定了一些javascript来使用我的搜索栏来过滤/隐藏与搜索输入不匹配的内容。我说它的工作率约为95%我会说但是我有一个问题要解决。

所以我的页面显示家具组及其包含的家具。组名称/编号和描述作为标题div存在,下面有一个用实际家具创建的表格。只要我在打字沙发'或者'主席'这将在表格行中。但是,如果我键入家具组的名称,它只显示名称/编号/描述和图像,但隐藏表格。组名称/描述在此块中:

@foreach ($orderFormData->pgroups as $pgroup)
<div class="group-container">
        <h3 style="font-size: 26px; padding: 10px 0;">{{ $pgroup->group_name 
}} - {{ $pgroup->group_code }}</h3>
        <p class="uk-text-muted" style="font-size: 20px;" >{!! 
html_entity_decode($pgroup->group_desc) !!}</p>

所以,我需要尝试稍微重构一下以添加功能,这样如果我的输入与组名称或描述匹配,它仍会显示该div的整个表格。

我的想法是添加类似的东西

<script type="text/javascript">
if($('.group-container').children('tr:visible').length == 0) {
$('.group-container').hide();
} else {
$('.group-container').show();
}
</script>

在我的第一行html下面,在foreach循环下面。但我不知道这是不是正确的想法,也不知道如何正确地使用它。

HTML:

@foreach ($orderFormData->pgroups as $pgroup)
<div class="group-container">
        <h3 style="font-size: 26px; padding: 10px 0;">{{ $pgroup->group_name }} - {{ $pgroup->group_code }}</h3>
        <p class="uk-text-muted" style="font-size: 20px;" >{!! html_entity_decode($pgroup->group_desc) !!}</p> <!--Group Description-->

        <div class="uk-grid">
            <div class="uk-width-2-10">
                <ul style="margin: 0; padding: 0; list-style-type: none; float: left; width: 100%;">
                    @foreach ($pgroup->image_names as $image_name)
                    <li><a href="/imagelib/Bigthumbs/{{ substr($image_name, 0, strpos($image_name, ',')) }}" target=_blank><img src="/imagelib/Bigthumbs/{{ substr($image_name, 0, strpos($image_name, ',')) }}" style="width: 100%; height: auto;" /></a><span class="uk-text-center" style="padding: 0 0 5px;">{{ substr($image_name, strpos( $image_name, ',') + 1)  }}</span></li>
                    @endforeach
                </ul>
            </div>

            <div class="uk-width-8-10">
                <table id="userTbl" class="uk-table" style="width: 100%; min-width: 768px;">
                    <thead>
                    <tr>
                        <th style="width: 10%; font-size: 20px;">Frame</th>
                        <th style="width: 20%; font-size: 20px;">Description</th>
                        <th style="width: 15%; font-size: 20px;">Cover/Color</th>
                        <th style="width: 15%; font-size: 20px;">Cover/Color</th>
                        <th style="width: 20%; font-size: 20px;">Quantity</th>
                        <th style="width: 15%; font-size: 20px; text-align: center;"><b>Price</b></th>
                    </tr>
                    </thead>

                    <tbody>

                    @foreach ($pgroup->pskus as $psku)
                    <?php $tempdata['sku-' . $i] = $psku ?>
                    <tr class="@if (isset($psku->quantity) && $psku->quantity > 0) {{ highlight }} @endif">
                        <td style="font-weight: 500; line-height: 30px; font-size: 14px;">{{ $psku->frame_fmt }}</td>
                        <td style="font-weight: 500; line-height: 30px; font-size: 14px;">{!! html_entity_decode($psku->frame_desc) !!}</td>
                        <td style="font-weight: 500; line-height: 30px; font-size: 14px;">{{ $psku->cover1_code }}/{{ $psku->color1_code }} {{ $psku->color1_desc }}</td>
                        <td style="font-weight: 500; line-height: 30px; font-size: 14px;">{{ $psku->cover2_code }}/{{ $psku->color2_code }} {{ $psku->color2_desc }}</td>
                        <td style="font-weight: 700; line-height: 30px; font-size: 14px;">
                            <span style="text-align: center; display: block; width: 100%;">${{ $psku->price }}</span>
                        </td>
                    </tr>
                    <?php $i++; ?>
                    @endforeach
                    </tbody>
                </table>
            </div>

        </div>
</div>
@endforeach

JS:

<script>
$(document).ready(function(){
$("#srch-term").keyup(function(){

    // Retrieve the input field text and reset the count to zero
    var filter = $(this).val(), count = 0;

    // Loop through the main container as well as the table body and row that contains the match
    $(".group-container, tbody tr").each(function(){

        // If the list item does not contain the text phrase fade it out
        if ($(this).text().search(new RegExp(filter, "i")) < 0) {
            $(this).fadeOut();

        // Show the list item if the phrase matches and increase the count by 1
        } else {
            $(this).show();
            count++;
        }
    });
  });
});
</script>

1 个答案:

答案 0 :(得分:2)

您可以先搜索群组,如果名称/描述匹配,则显示整个群组及其所有行。否则按常规程序进行。

<script>
$(document).ready(function(){
$("#srch-term").keyup(function(){

    // Retrieve the input field text and reset the count to zero
    var filter = $(this).val(), count = 0;
    var search_regex = new RegExp(filter, "i")

    // Loop through the main container as well as the table body and row that contains the match
    $(".group-container").each(function(){
        //check if filter matches the group name or description
        var group_name = $(this).children('h3').text()
        var group_description = $(this).children('.uk-text-muted').text()

        if(group_name.search(search_regex)>=0 || group_description.search(search_regex)>=0){ // filter matches
            $(this).show() // show group
            $(this).find("tbody tr").show() // and all children
            return // skip tr filtering
        }

        var no_matches = true

        $(this).find("tbody tr").each(function(){

            // If the list item does not contain the text phrase fade it out
            if ($(this).text().search(search_regex) < 0) {
                $(this).fadeOut();

            // Show the list item if the phrase matches and increase the count by 1
            } else {
                $(this).show();
                count++;
                no_matches = false
            }
        });

        if(no_matches){ // if no tr matched the search either, hide whole group
            $(this).fadeOut();
        }

    });
  });
});
</script>