laravel和datatables无法集成

时间:2014-07-05 08:22:03

标签: javascript laravel datatable datatables jquery-datatables

我有问题了解如何在latable中的datatable.net中使用数据表。

这是我的Controller生成视图

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

这是我的数据表控制器

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::get('hospital',
        array(
            'uses'=>'DoctorController@hospital'
            )
        );

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

这是我在视图中的表格

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

这是我在视图上的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)

1 个答案:

答案 0 :(得分:1)

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

您需要向查看提供$hospitals

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

这假设您拥有Hospital模型。