新视图后过滤数据后,该复选框变为未选中状态。筛选数据后如何停止未选中的复选框

时间:2019-06-12 10:36:37

标签: javascript jquery ajax laravel checkbox

我在侧栏中有一个制造商列表,从中可以在选中复选框时根据制造商过滤数据。但是当视图加载时,数据将被过滤,但未选中该复选框。数据过滤后,如何阻止复选框变为未选中状态。

在laravel 5.1上工作

控制器

      $productsFeatured = Product::select('products.id', 'products.name','products.low_emitting_material_prerequisite_file',
            'manufacturers.phone','manufacturers.address','manufacturers.linkedin_url','manufacturers.city', 'products.specs_file','products.specs_file_url','products.low_emitting_material_credit_file', 'products.logo', 'products.division_id', 'products.section_id', 'products.manufacturer_id', 'divisions.name as division_name', 'divisions.code as division_code', 'sections.name as section_name', 'sections.code')
                    ->whereIn('products.status', ['active'])
                    ->where(function ($query) {
                        $query->where('products.low_emitting_material_prerequisite_file', '!=', '');
                    })
                    ->where('users.user_type', 'manufacturer_paid')
                    ->leftJoin('products_selected_leeds', 'products.id', '=', 'products_selected_leeds.product_id')
                    ->leftJoin('sections', 'products.section_id', '=', 'sections.id')
                    ->leftJoin('divisions', 'products.division_id', '=', 'divisions.id')
                    ->join('manufacturers', 'products.manufacturer_id', '=', 'manufacturers.id')
                    ->join('users', 'manufacturers.user_id', '=', 'users.id')
                    ->orderBy('divisions.code', 'asc')
                    ->orderBy('sections.code', 'asc')
                    ->groupBy('products.id');

                    if($request->ajax() && $request->manufacturer){

                        $productsFeatured->whereIn('products.manufacturer_id', $request->manufacturer);
                        $productsFeatured = $productsFeatured->get();                     

                    } else{
                        $productsFeatured = $productsFeatured->get();
                    }


   if($request->ajax()){

                    return  view('guest::new_design_frontend.product-library')->with($productsFeatured); 

            }else{
                return  view('guest::new_design_frontend.product-library')->with($productsFeatured); 
            }

脚本

$(document).ready(function() {

        // filter_data();

        function filter_data() {

            var manufacturer = get_filter('manufacturer');
            $.ajax({   
                url: "{{url('CHPS')}}",
                method: 'POST',
                headers: {
                        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
                    },
                data:{manufacturer:manufacturer} ,

                success:function(data){
                 $('body').html();

                    $('body').html(data);
                }
            });
        }
        function get_filter(class_name) {
            var filter = [];

            $('.'+class_name+':checked').each(function(){
            filter.push($(this).val());
        });
        return filter
        }
        $('.common_selector').click(function(){
        filter_data();
    });

    });

视图

manufacturers as sidebar

 @foreach($manufacturers as $leedsManufacturer)
                                <div class="post" id="post{{$leedsManufacturer['mfgg_id']}}">
                                    <label class=" my-checkbox gry2" id="manufacturer">{{str_limit($leedsManufacturer['mfgg_name'], 300)}}
                                        <input type="checkbox" class="manufacturer common_selector" name="manufacturer[]" value="{{$leedsManufacturer['mfgg_id']}}">
                                        <span class="checkmark"></span>
                                    </label>
                                </div>
                         @endforeach


checkbox is unchecked after filter the data whereas it should be not

1 个答案:

答案 0 :(得分:0)

通过$ request->制造商值进行查看。 然后,您需要使用检查后的数组$ request-> manufacturer检查当前值$ leedsManufacturer ['mfgg_id'],如果是,则添加属性“ checked”

<input type="checkbox" class="manufacturer common_selector" name="manufacturer[]" value="{{$leedsManufacturer['mfgg_id']}}"  {{(in_array($leedsManufacturer['mfgg_id'],$checkedManufactureArray)?"checked":"")}}>

您需要将检查后的值传递给视图
    $ checkedManufactureArray = $ request->制造商;