javascript - 使用 ajax 和 laravel 基于状态的过滤

标签 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(完全关闭)

这些是我用来过滤的,但如果我选择状态 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>

过滤 Controller 函数

 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');
        }
    }

最佳答案

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');       
        }    
//    }
}

关于javascript - 使用 ajax 和 laravel 基于状态的过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50021234/

相关文章:

javascript - Ajax get with jsonp 给出 "SyntaxError: missing ; before statement"错误

javascript - div 部分中的 d3 图

php - 如何在没有路由的情况下调用 Controller 文件夹外部的函数?

asp.net - 在 WPF 和 Silverlight 之间进行选择

javascript - 如何使用javascript获取laravel存储路径

angularjs - 因为我现在正在使用 Laravel,所以我应该学习 Vuejs/Angular/React 吗?

javascript - Vue.js 组件父事件

javascript - 如何设置 ajax 每 5 秒运行一次

php - 验证然后重定向到 Laravel 中的发布路由

javascript - 你如何编写 mybind 的代码