【问题标题】:Return view (Table) from controller laravel从控制器 laravel 返回视图(表)
【发布时间】:2019-10-22 16:43:46
【问题描述】:

我正在从控制器函数及其工作(通过 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. 制作 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;
    }
    

【问题讨论】:

    标签: ajax laravel


    【解决方案1】:

    请使用它,它对我有用

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

    【讨论】:

      【解决方案2】:

      一个简单的解决方案是将它们分开连接。虽然我不确定这是否是最好的解决方案。 你可以尝试如下

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

      【讨论】:

        猜你喜欢
        • 2015-10-15
        • 1970-01-01
        • 2020-06-07
        • 2016-11-18
        • 2021-03-02
        • 1970-01-01
        • 2013-01-08
        • 1970-01-01
        • 2017-02-13
        相关资源
        最近更新 更多