从控制器laravel返回视图(表)

时间:2019-10-22 07:54:40

标签: ajax laravel

我正在从控制器功能及其工作(通过ajax)返回表视图。但要在'tr'标签上方使用foreach。但不知道如何使用。

  1. 用于返回Ajax响应的函数

     public function displaySearch(Request $request)
         {
            $result = Team::where('name', $request->somthing)
                     ->orwhere('order_id', $request->filter)->get();
    
             $data = $this->make_ui($result);
             return $data;
         }
    
  2. Make UI的私有功能

    private function make_ui($result){
              $data='';
    
              foreach($result as $c){
              $data.='<div class="table-responsive">
                <table class="table table-striped table-bordered">
                <thead>
                <tr>
                    <th>Action</th>
                    <th>ID</th>
                    <th>Date</th>
                    <th>Description</th>
                </tr>
                </thead>
                <tbody>
                    <tr class="gradeX">
                        <td>'. $c->id .'</td>
                        <td>'. $c->order_id .'</td>
                        <td>'. $c->name .'</td>
                        <td>'. $c->role .'</td>
                    </tr>
                </tbody>
                <tfoot>
                <tr>
                    <th>Action</th>
                    <th>ID</th>
                    <th>Date</th>
                    <th>Description</th>
                </tr>
                </tfoot>
            </table>
        </div>';
    }
      return $data;
    }
    

2 个答案:

答案 0 :(得分:1)

plz为我使用它的工作

private function make_ui($result){
          $data='<div class="table-responsive">
            <table class="table table-striped table-bordered">
            <thead>
            <tr>
                <th>Action</th>
                <th>ID</th>
                <th>Date</th>
                <th>Description</th>
            </tr>
            </thead>
            <tbody>';

          foreach($result as $c){
          $data.='
                <tr class="gradeX">
                    <td>'. $c->id .'</td>
                    <td>'. $c->order_id .'</td>
                    <td>'. $c->name .'</td>
                    <td>'. $c->role .'</td>
                </tr>
            ';
           }
          $data .= '</tbody>
            <tfoot>
            <tr>
                <th>Action</th>
                <th>ID</th>
                <th>Date</th>
                <th>Description</th>
            </tr>
            </tfoot>
        </table>
    </div>';
  return $data;
}

答案 1 :(得分:0)

一个简单的解决方案是分别连接它们。虽然我不确定这是否是最佳解决方案。 您可以尝试以下操作

private function make_ui($result){     
    $data.='<div class="table-responsive">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>Action</th>
                            <th>ID</th>
                            <th>Date</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>';
    foreach($result as $c){
        $data.='<tr class="gradeX">
                    <td>'. $c->id .'</td>
                    <td>'. $c->order_id .'</td>
                    <td>'. $c->name .'</td>
                    <td>'. $c->role .'</td>
                </tr>';
    }
    $data.='</tbody>
            <tfoot>
                <tr>
                    <th>Action</th>
                    <th>ID</th>
                    <th>Date</th>
                    <th>Description</th>
                </tr>
            </tfoot>
        </table>
    </div>';
    return $data;
}