DataTable变量未传递给视图

时间:2018-01-15 07:17:23

标签: laravel datatables

我第一次使用DataTable Editor并且遇到了错误:未定义的变量:dataTable。我在另一个视图中包含一个视图,因为datable是包含其他数据的页面的一部分。

以下是我的观点......

<script language="JavaScript" type="text/JavaScript">
$(function() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': '{{csrf_token()}}'
        }
    });

    var editor = new $.fn.dataTable.Editor({
        ajax: "/progress_monitoring_week",
        table: "#week",
        display: "bootstrap",
        fields: [
            {label: "id:", name: "id"},
            {label: "Monitoring ID:", name: "progress_monitoring_id"},
            {label: "Week Number:", name: "week_number"},
            {label: "Monitoring Date:", name: "progress_monitoring_date"},
            {label: "Test Score:", name: "test_score"},
            {label: "Responding:", name: "responding"},
            {label: "Recommendation:", name: "recommentation_id"},
            {label: "Created:", name: "created_at"},
            {label: "Updated:", name: "updated_at"},
        ]
    });

    $('#week').on('click', 'tbody td:not(:first-child)', function (e) {
        editor.inline(this);
    });

    {{$dataTable->generateScripts()}}
});
</script>
<div class="row">
<div class="col-md-12">
    {{$dataTable->table(['id' => 'week'])}}
</div> <!-- end column -->
</div> <!-- end row -->

这是控制器代码......

<?php

namespace App\Http\Controllers;

use App\DataTables\ProgressMonitoringWeekDataTable;
use App\DataTables\ProgressMonitoringWeekDataTablesEditor;
use Illuminate\Http\Request;

class ProgressMonitoringWeekController extends Controller
{
    public function index(ProgressMonitoringWeekDataTable $dataTable)
    {
        return $dataTable->render('progress_monitoring_week.index');
    }

    public function store(ProgressMonitoringWeekDataTablesEditor $editor)
    {
        return $editor->process(request());
    }
}

这是型号代码......

<?php

namespace App\DataTables;

use App\ProgressMonitoringWeek;
use Yajra\DataTables\Services\DataTable;

class ProgressMonitoringWeekDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables($query)
            ->addColumn('action', 'progressmonitoringweek.action');
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\ProgressMonitoringWeek $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(ProgressMonitoringWeek $model)
    {
        return $model->newQuery()->select('id', 'progress_monitoring_id', 'week_number', 'progress_monitoring_date', 'test_score', 'responding', 'recommentation_id', 'created_at', 'updated_at');
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->addAction(['width' => '80px'])
            ->parameters($this->getBuilderParameters());
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'id',
            'progress_monitoring_id',
            'week_number',
            'progress_monitoring_date',
            'test_score',
            'responding',
            'recommentation_id',
            'created_at',
            'updated_at',
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'ProgressMonitoringWeek_' . date('YmdHis');
    }
}

我不确定datable变量是否未传递给视图,因为它包含在另一个视图中,但这是我的第一个猜测,我不知道如何解决它。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在您的include语句中,只需将datatable变量作为第二个参数传递。这样子视图才能访问它。

@include('subview', $dataTable)