javascript - laravel 和数据表无法集成

标签 javascript laravel datatable datatables

您好,我在理解如何在 Laravel 的 datatable.net 中使用数据表时遇到问题。

这是我的 Controller 来生成 View

public function hospital()
    {
        $view                   =   View::make('doctor.hospital', array());
        return $view;
    }

这是我的数据表 Controller

public function allHospitalList()
    {

        $hospitals = HospitalList::hospitalsList();
        return $hospitals;
    }

这是我的模型

<?php
class HospitalList extends Eloquent {

    protected $table = 'hospital_list';

    public static function hospitalsList() 
    {
        $data = HospitalList::join('Cities', 'Cities.id', '=', 'hospital_list.Address3')
        ->join('Province', 'Province.id', '=', 'hospital_list.Address4')
        // ->paginate(10);
        ->get();

        return $data;
    }
}

在我的 route

Route::get('hospital',
        array(
            'uses'=>'DoctorController@hospital'
            )
        );

/* datatables */
    Route::get('hospitallist',
    array(
        'as' => 'dt_hospital_all',
        'uses'=>'DatatableController@allHospitalList'
        ));

这是我在 View 中的表格

<table class="table table-hover radius" id="hospitaList">
                            <thead>

                                    <tr>
                                        <th>

                                        </th>
                                        <th>
                                             Name
                                        </th>
                                        <th>
                                             Type
                                        </th>
                                        <th>
                                             Contact
                                        </th>
                                    </tr>
                            </thead>
                            <tbody>
                                    <?php
                                    foreach ($hospitals as $hospital) 
                                    {?>
                                        <tr>
                                            <td>
                                                <!-- <span class="row-details row-details-close"></span> -->
                                                <button type="button" class="btn red"><i class="fa fa-plus-square-o"></i>
                                                </button>
                                            </td>
                                            <td>
                                                 {{ $hospital->name }}</br>
                                                 {{ $hospital->address1 }} {{ $hospital->address2 }}
                                                 <?php
                                                    if($hospital->address1 != '' || $hospital->address2 != '') echo "</br>";
                                                 ?>
                                                 {{ $hospital->City }}, {{ $hospital->province }}
                                            </td>
                                            <td>
                                                 {{ $hospital->type }}
                                            </td>
                                            <td>
                                                 {{ $hospital->contact }}
                                            </td>
                                        </tr>
                                    <?php
                                    }
                                    ?>
                            </tbody>

                        </table>

这是我在 View 上的 JavaScript

var url = "{{ URL::route('dt_hospital_all') }}";

    $(document).ready(function() {
    $('#hospitalList').dataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": url
    } );
} );

我收到错误

Undefined variable: hospitals (View: /Applications/AMPPS/www/docsearch/app/views/doctor/hospital.blade.php)

最佳答案

现在,您引用的是一个不存在的变量,$hospitals

您需要向您的 View 提供$hospitals:

View::make('doctor.hospital')->with('hospitals', Hospital::all())

这假设您有一个Hospital模型。

关于javascript - laravel 和数据表无法集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24584470/

相关文章:

php - gmail登录后cookie被删除

php - Where 和 If 语句 Laravel Eloquent

xml - 使用嵌套表将数据集写入 xml

twitter-bootstrap - 如何在 Bootstrap 容器内移动 dataTables 搜索框?包含 JSFiddle

javascript - 关于第一个 :child and last:child in CSS 的问题

javascript - 多个 JS api 调用

javascript - 根据过滤器输入更改排序顺序

javascript - 物化日期选择器不工作

php - 在 laravel forge 和 homestead 上慢吞吞

c# - System.Data.DataTable、DataRowCollection 和 DataColumns : How does the indexing work?