【问题标题】:Show in the table the search results when the user clicks in the "Search" button当用户点击“搜索”按钮时,在表格中显示搜索结果
【发布时间】:2018-12-01 19:36:33
【问题描述】:

我在 ParticipantController 中有这两种方法。 index() 显示会议中所有注册的一些信息。然后 search() 用于视图中的搜索表单,仅在表格中显示注册用户的名称与用户在搜索表单中引入的名称类似的结果:

class ParticipantController extends Controller
{
    public function index($id){
        $conference = Conference::find($id);
        $conference->load('registrations.participants.answers.question');

        return view('participants.index')->with('conference',  $conference);
    }

    public function search($id, Request $request)
    {
        $name = $request->get('participant');
        $conference = Conference::findOrFail($id);

        $searchInfo = $conference->load([
            'registrations.participants' => function ($query) use ($name) {
                return $query->where('name', 'like', '%' . $name . '%');
            },
            'registrations.participants.answers.question'
        ]);

        return view('participants.index', compact('conference'));
    }
}

我对这些方法的路线:

  Route::get('conference/edit/{id}/participants',    [ 'uses' => 'ParticipantController@index', 'as'    =>    'participants.index']);

 Route::get('conference/edit/{id}/participants/search', [
    'uses' => 'ParticipantController@search',
    'as'   => 'participants.search'
]);

我的疑问是用户在搜索表单中输入一些数据并单击“搜索”后如何在视图中显示搜索结果。当用户访问参与者/index.blade.php 时,所有注册都已经列在一个表格中,下面的 foreach 使用 index() 方法中的代码:

@foreach($conference->registrations as $registration)
    <tr>
        <td>{{$registration->customer->name}} {{$registration->customer->surname}}</td>
        <td>{{ $registration->status === 'C' ? 'Complete' : 'Incomplete' }}</td>
        <td><a data-regid="{{$registration->id}}"> More details</a></td>
    </tr>
@endforeach

而且工作正常,该表显示了所有注册的详细信息,例如:

User that did the registration       Quantity           Status                Details

  Jake K                               2                Complete            Show
    ...

但是在这个视图中也有搜索表单,我的疑问是当用户在搜索表单中输入姓名并点击“搜索”时,如何在表格中显示搜索结果。

视图中搜索表单的 HTML:

<form  action="{{ route('participants.search', ['id' => $conference->id] )}}" method="get">
    <div class="form-row">
        <div class="form-group col col-md-6">
            <label for="participant">Search</label>
            <div class="input-group">
                <input type='text' name="participant" class="form-control" placeholder="Name" />
            </div>
        </div>
    </div>
</form>

【问题讨论】:

  • 使用 jquery 数据表来显示列表。它会自动处理搜索、分页等。

标签: php laravel


【解决方案1】:

像这样改变你的搜索操作

public function search($id, Request $request)
{
    $name = $request->get('participant');
    $conference = Conference::with(['registrations.participants' => function() ($query) use ($name){
                       return $query->where('name', 'like', '%' . $name . '%');
                  },'registrations.participants.answers.question'])->findOrFail($id);

    return view('participants.index', compact('conference'));
}

现在,如果您仔细查看 index 和 search 具有相同的返回类型 conference ,唯一的区别在于搜索我们过滤了参与者。

您也可以在 index 操作中添加搜索功能

public function index($id, Request $request){
   $name = $request->get('participant');

   if($name){
       $conference = Conference::with(['registrations.participants' => function() ($query) use ($name){
                       return $query->where('name', 'like', '%' . $name . '%');
                  },'registrations.participants.answers.question'])->findOrFail($id);
   }else{
       $conference = Conference::with('registrations.participants.answers.question')->findOrFail($id);
   }

   return view('participants.index')->with('conference',  $conference);
}

【讨论】:

  • 谢谢,但是如何在视图中显示过滤后的参与者?是否需要两次相同的foreach?而且过滤的参与者结果应该只在点击“搜索”按钮时出现。
  • 当您单击提交按钮时,它会呈现相同的视图,但会显示过滤结果
【解决方案2】:

这将帮助您满足您的需求 - https://datatables.net/examples/styling/bootstrap

【讨论】:

  • 谢谢,您知道搜索栏和每页结果的 html 在哪里吗?
  • 这是库本身添加的,你只需要在你的内容表上初始化数据表,如图 - $('#your_table_id').DataTable();
【解决方案3】:

您可以使用数据表 链接:https://github.com/yajra/laravel-datatables

检查。这将解决您的问题。

【讨论】:

  • 谢谢,您知道搜索栏和每页结果的 html 在哪里吗?
  • 检查js文件。在 javascript 文件中创建的每页搜索框和结果。
猜你喜欢
  • 2018-12-08
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多