使用ajax和laravel进行基于状态的过滤

时间:2018-04-25 11:32:46

标签: javascript ajax laravel laravel-5.5 laravel-5.6

我在数据库中有一个数据,其中2个表称为注册,ssi_tracks我需要根据track_first_status显示注册表数据,我需要按状态明智地过滤数据。实际上脚本正在运行但是我没有任何工作正确答案

  1. 第一个状态= 0(未调用)
  2. 第一个状态= 1(部分称为)
  3. 第一个状态= 2(等待)
  4. 第一个状态= 3(黑名单)
  5. 第一个状态= 4(完全关闭)
  6. 这些是我用来过滤但如果我选择状态2它不能正常工作 它返回状态1结果,但如果我选择状态1,则没有任何事情发生

    Ajax代码

     $(function () {
            $("#dropcallstatus").change(function () {
    
              let $value;
    
                if (($(this).val() === "0") || $(this).val() === "1" || $(this).val() ==="2" || $(this).val() ==="3" || $(this).val() ==="4") {
                    $value = $(this).val();
                    $.ajax({
                        type: 'GET',
                        url: "{{url("callstatus")}}",
                        data: {'dropcallstatus': $value},
                        dataType: 'json',
                        success: function (data) {
                           $('#listdetails').html(data);
    //               console.log(data);
                        }
                    });
    
                }
                else {
                    alert('Select Any Status');
                }
            });
        });
    

    索引文件

    <div class="content-page">
        <!-- Start content -->
        <div class="content">
            <div class="container-fluid">
    
                <div class="row">
                    <div class="col-12">
                        <div class="page-title-box">
                            <h4 class="page-title float-left">SSI TRACK</h4>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
                <!-- end row -->
                <div class="row">
                    <div class="col-12">
                        <div class="card-box table-responsive">
                            <h4 class="m-t-0 header-title"><b>SSI TRACKS</b></h4>
    
                            <div id="datatable_wrapper" class="dataTables_wrapper container-fluid dt-bootstrap4 no-footer">
                                <div class="row">
    
                                    <div class="col-sm-6">
    
                                        <select class="form-control" style="width: 130px" id="dropselect" name="dropselect">
    
                                            <option>Select Status</option>
                                            <option value="24Hours">24 Hours</option>
                                            <option value="15Days">15 Days</option>
                                            {{--<option value="3">All</option>--}}
    
                                        </select>
                                        <br>
    
                                        <select class="form-control" style="width: 130px" id="dropcallstatus"
                                                name="dropselectstatus">
    
                                            <option>Select Calls</option>
                                            <option value="0">Not Called</option>
                                            <option value="1">Partially Called</option>
                                            <option value="2">Waiting Calls</option>
                                            <option value="3">Black Listed Calls</option>
                                            <option value="4">Fully Closed Calls</option>
    
                                        </select>
    
                                    </div>
    
    
                                </div>
    
    
                                <div class="row">
                                    <div class="col-md-12">
                                        <label>From
                                        </label> <input type="date" name="start_date" id="start_date" class="form-control"
                                                        style="width:150px;">
                                        <label>To</label> <input type="date" name="end_date" id="end_date"
                                                                 class="form-control"
                                                                 style="width:150px;">
                                        <button class="btn btn-info" id="filter" name="filter">Filter</button>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-sm-12">
                                        <table id="datatable" class="table table-bordered dataTable table-responsive-lg">
                                            <thead>
                                            <tr>
                                                <th>slno</th>
                                                <th>Address</th>
                                                <th>Model</th>
                                                <th>Chassis</th>
                                                <th>Delivery Date</th>
                                                <th>Call</th>
    
                                            </tr>
                                            </thead>
                                            <tbody id="listdetails" name="listdetails">
    
    </tbody>
    </div>
    </div>
    </div>
    </div>
    </div>
    

    过滤控制器功能

     public function callstatus(Request $request)
        {
    
            $dropselect = $request->input('dropcallstatus');
    
    
            if ($dropselect === '1') {
                $call = DB::table('registrations')
                    ->join('ssi_tracks', 'registrations.registration_id', '=', 'ssi_tracks.registration_id')
                    ->select('address', 'model', 'chassis', 'delivery_date')
                    ->where([["ssi_tracks.track_first_status", "=", 0]])
                    ->get();
    
    
                $output = "";
                $count = 1;
                foreach ($call as $calls) {
    
                    $output .= '<tr>' .
                        '<td>' . $count++ . '</td>' .
                        '<td>' . $calls->address . '</td>' .
                        '<td>' . $calls->model . '</td>' .
                        '<td>' . $calls->chassis . '</td>' .
                        '<td>' . $calls->delivery_date . '</td>' .
                        '<td>' . '<button class="btn btn-primary btn-rounded button">Call Customer
                                                        </button>' . '</td>' .
    
                        '</tr>';
    
    
                }
                return response()->json($output);
            } elseif ($dropselect === '2') {
    
                $call = DB::table('registrations')
                    ->join('ssi_tracks', 'registrations.registration_id', '=', 'ssi_tracks.registration_id')
                    ->select('address', 'model', 'chassis', 'delivery_date')
                    ->where([["ssi_tracks.track_first_status", "=", 1]])
                    ->get();
    
    
                $output = "";
                $count = 1;
                foreach ($call as $calls) {
    
                    $output .= '<tr>' .
                        '<td>' . $count++ . '</td>' .
                        '<td>' . $calls->address . '</td>' .
                        '<td>' . $calls->model . '</td>' .
                        '<td>' . $calls->chassis . '</td>' .
                        '<td>' . $calls->delivery_date . '</td>' .
                        '<td>' . '<button class="btn btn-primary btn-rounded button">Call Customer
                                                        </button>' . '</td>' .
                        '</tr>';
    
    
                }
                return response()->json($output);
    
            } elseif ($dropselect === '3') {
                $call = DB::table('registrations')
                    ->join('ssi_tracks', 'registrations.registration_id', '=', 'ssi_tracks.registration_id')
                    ->select('address', 'model', 'chassis', 'delivery_date')
                    ->where([["ssi_tracks.track_first_status", "=", 2]])
                    ->get();
    
    
                $output = "";
                $count = 1;
                foreach ($call as $calls) {
    
                    $output .= '<tr>' .
                        '<td>' . $count++ . '</td>' .
                        '<td>' . $calls->address . '</td>' .
                        '<td>' . $calls->model . '</td>' .
                        '<td>' . $calls->chassis . '</td>' .
                        '<td>' . $calls->delivery_date . '</td>' .
                        '<td>' . '<button class="btn btn-primary btn-rounded button">Call Customer
                                                        </button>' . '</td>' .
                        '</tr>';
    
    
                }
                return response()->json($output);
    
            }
            elseif ($dropselect === '4') {
    
                $call = DB::table('registrations')
                    ->join('ssi_tracks', 'registrations.registration_id', '=', 'ssi_tracks.registration_id')
                    ->select('address', 'model', 'chassis', 'delivery_date')
                    ->where([["ssi_tracks.track_first_status", "=", 3]])
                    ->get();
    
    
                $output = "";
                $count = 1;
                foreach ($call as $calls) {
    
                    $output .= '<tr>' .
                        '<td>' . $count++ . '</td>' .
                        '<td>' . $calls->address . '</td>' .
                        '<td>' . $calls->model . '</td>' .
                        '<td>' . $calls->chassis . '</td>' .
                        '<td>' . $calls->delivery_date . '</td>' .
                        '<td>' . '<button class="btn btn-primary btn-rounded button">Call Customer
                                                        </button>' . '</td>' .
                        '</tr>';
    
    
                }
                return response()->json($output);
    
            }
            elseif ($dropselect === '5') {
                $call = DB::table('registrations')
                    ->join('ssi_tracks', 'registrations.registration_id', '=', 'ssi_tracks.registration_id')
                    ->select('address', 'model', 'chassis', 'delivery_date')
                    ->where([["ssi_tracks.track_first_status", "=", 4]])
                    ->get();
    
    
                $output = "";
                $count = 1;
                foreach ($call as $calls) {
    
                    $output .= '<tr>' .
                        '<td>' . $count++ . '</td>' .
                        '<td>' . $calls->address . '</td>' .
                        '<td>' . $calls->model . '</td>' .
                        '<td>' . $calls->chassis . '</td>' .
                        '<td>' . $calls->delivery_date . '</td>' .
                        '<td>' . '<button class="btn btn-primary btn-rounded button">Call Customer
                                                        </button>' . '</td>' .
                        '</tr>';
    
    
                }
                return response()->json($output);
            }
            else {
                return back()->with('warning', 'Please select a status');
            }
        }
    

1 个答案:

答案 0 :(得分:1)

public function callstatus(Request $request)
{

    $dropselect = $request->input('dropcallstatus');
//    if ($dropselect === '1') {
        $call = DB::table('registrations')
            ->join('ssi_tracks', 'registrations.registration_id', '=', 'ssi_tracks.registration_id')
            ->select('address', 'model', 'chassis', 'delivery_date')
            ->where([["ssi_tracks.track_first_status", "=", $dropselect]])
            ->get();

        if($call){
            $output = "";
            $count = 1;
            foreach ($call as $calls) {

                $output .= '<tr>' .
                    '<td>' . $count++ . '</td>' .
                    '<td>' . $calls->address . '</td>' .
                    '<td>' . $calls->model . '</td>' .
                    '<td>' . $calls->chassis . '</td>' .
                    '<td>' . $calls->delivery_date . '</td>' .
                    '<td>' . '<button class="btn btn-primary btn-rounded button">Call Customer
                                                    </button>' . '</td>' .

                    '</tr>';
            }
            return response()->json($output);
        }
        else
        {
            return back()->with('warning', 'Please select a status');       
        }    
//    }
}