简体   繁体   English

如何在mongodb的laravel中使用实时搜索?

[英]How can I use live search in laravel with mongodb?

I am using live search in laravel, I am working in department module, in that there is 2 field name of department and created by fields My database is mongoDb, here is my view file code, 我在laravel中使用实时搜索,我在部门模块中工作,因为有2个部门的字段名称并由字段创建我的数据库是mongoDb,这是我的视图文件代码,

                    <div class="table-responsive m-t-40">
                        <div class="form-group">
                            <input type="text" name="search" id="search" class="form-control" placeholder="Search Department">
                        </div>

                        <table class="table table-bordered table-striped ">
                            <thead>
                                <tr>
                                    <th>Department Name</th>
                                    <th>Created By</th>
                                    <th>Created On</th>
                                    @if (App\User::isPermitted(['edit_department', 'update_department', 'delete_department']))
                                    <th>Action</th>
                                    @endif
                                </tr>
                            </thead>
                            <tbody>
                               @if($listOfDepartment != null)
                                    @foreach($listOfDepartment as $departmentList)
                                        <tr>
                                            <td>{{$departmentList->nameOfDepartment}}</td>
                                            <td>{{$departmentList->createdBy}}</td>
                                            <td>{{$departmentList->created_at}}</td>
                                            @if (App\User::isPermitted(['edit_department', 'update_department', 'delete_department']))
                                                <td>
                                                    <a href="{{route('edit_department', $departmentList->id)}}"  data-toggle="modal" data-target="#myEditModal"><i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i></a>&emsp;
                                                    <a href="{{route('delete_department', $departmentList->id)}}"><i class="fa fa-trash fa-lg" onclick="delete_user(this); return false;" style="color:red" aria-hidden="true"></i></a>
                                                </td>
                                            @endif
                                        </tr>
                                    @endforeach
                                @endif
                            </tbody>
                        </table>
                    </div>
                </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
        $(document).ready(function(){

         fetch_customer_data();

         function fetch_customer_data(query = '')
         {
          $.ajax({
           url:"{{ route('list_department') }}",
           method:'GET',
           data:{query:query},
           dataType:'json',
           success:function(data)
           {
            $('tbody').html(data.table_data);
           }
          })
         }

         $(document).on('keyup', '#search', function(){
          var query = $(this).val();
          fetch_customer_data(query);
         });
        });

</script>
@endsection

here is my route file 这是我的路线档案

Route::get('list-department', 'DepartmentController@listDepartment')->name('list_department');

Here is my code in controller file 这是我在控制器文件中的代码

  public function listDepartment(Request $request)
    {
        $listOfDepartment = Department::all();  

        if($request->ajax())
        {
            $output = '';
            $query = $request->get('query');
            if($query != '')
            {
                $data = Schema::table('department')
                    ->where('nameOfDepartment', 'like', '%'.$query.'%')
                    ->orWhere('createdBy', 'like', '%'.$query.'%')
                    ->get();
            }
            else
            {
                $data = Schema::table('department')
                    ->orderBy('nameOfDepartment', 'asc')
                    ->get();
            }

            $total_row = $data->count();
            if($total_row > 0)
            {
                foreach($data as $row)
                {
                    $output .= '<tr>
                    <td>'.$row->nameOfDepartment.'</td>
                    <td>'.$row->createdBy.'</td>
                    </tr>';
                }
            }
            else
            {
                $output = '
                <tr>
                    <td align="center" colspan="5">No Data Found</td>
                </tr>
                ';
            }
                $data = array(
                'table_data'  => $output,
                'total_data'  => $total_row
                );
                echo json_encode($data);

            }
        return view('pages.department', compact('listOfDepartment'));
    }

the issue is that whatever I wrote in search bar it now give any output. 问题是,无论我在搜索栏中写什么,它现在都会提供任何输出。

You can use jquery contains which returns every string which is its inside 您可以使用jquery contains返回其内部的每个字符串

 $(document).ready(function(){
     var string =  'Any string';
     jQuery.expr[":"].Contains = jQuery.expr.createPseudo(function(arg) {
       return function( elem ) {
       return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
     };
    });
    jQuery("tr").hide()//It will hide all the tr elements
    jQuery('tr:Contains("'+ string +'")').show();//then will show only tr which has the string
});

I have made this algorithm and also used this 我已经制作了这个算法并且也使用了这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM