我正在try 将DataTables的两个实例发送到我的视图.该场景包括一个选项卡菜单,每个选项卡包含一个对应的表.以下是我采取的方法.

我的控制器

    public function show(AuthorityPage $authorityPage, 
        AuthorityPageGuideContentDataTable $authorityPageGuideContentDataTable, 
        AuthorityPageAccordionDataTable $authorityPageAccordionDataTable
        )
    {
       
        $blogPosts = BlogPost::where('domain_id', getCurrentDomain())->where('is_active',true)
        ->select(['id','name'])->get();
        $tours = Tour::where('domain_id', getCurrentDomain())
            ->with('generalDetail')->get();
        $destinations = Destination::where('domain_id', getCurrentDomain())
            ->select(['id','name'])->get();
        $keywordIntents = [
            'i' => 'Informational',
            'n' => 'Navigational',
            't' => 'Transactional',
            'c' => 'Commercial',
        ];

$accordionDt = $authorityPageAccordionDataTable->with([
            'authorityPage' => $authorityPage
        ])->html();
$authorityContentDt = $authorityPageGuideContentDataTable->with([
            'authorityPage' => $authorityPage
        ])->html();

return view('pages.authority-pages.show', compact(
            'authorityPage', 
            'blogPosts', 
            'tours', 
            'destinations', 
            'keywordIntents', 
            'authorityContentDt', // first dt
            'accordionDt' //2nd dt
        ));

在我的第一个选项卡视图中,我通过以下方式呈现DataTable

    <div class="card-body py-4">
        <div class="table-responsive">
            {{ $authorityContentDt->table()}}
        </div>
    </div>

在我的第二个选项卡中

  <div class="card-body py-4">
            <div class="table-responsive">
                {{ $accordionDt->table() }}
            </div>
        </div>

我的一个DataTable类(类似于我的所有DataTable类)

class AuthorityPageGuideContentDataTable extends DataTable
{
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
        ->editColumn('guide_type', function (DestinationGuideDetails $destinationGuideDetails) {
            return DestinationGuideType::byValue($destinationGuideDetails->guide_type)->name;
        })
        ->editColumn('content', function (DestinationGuideDetails $destinationGuideDetails) {
            return strip_tags($destinationGuideDetails->content);
        })
        ->addColumn('action', function (DestinationGuideDetails $destinationGuideDetails) {
            return '<button class="btn btn-icon btn-active-light-primary w-30px h-30px" onclick="deletedestinationGuideContent('.$this->authorityPage->id.' , '.$destinationGuideDetails->id.')"> '.getIcon("trash","fs-3").'</button>
            <button data-bs-toggle="modal" data-bs-target="#kt_modal_1" class="btn btn-icon btn-active-light-primary w-30px h-30px" onclick="getContent('.$this->authorityPage->id.','.$destinationGuideDetails->id.')"> '.getIcon("notepad-edit","fs-3").'</button>';
        })
        ->rawColumns(['action'])
        ->setRowId(function ($destinationGuideDetails) {
            return "destination-guide-details-{$destinationGuideDetails->id}";
        });
    }

    public function query(DestinationGuideDetails $model): QueryBuilder
    {
        $query = $model->newQuery()
        ->join('destination_guides', 'destination_guide_details.destination_guide_id', '=', 'destination_guides.id')
        ->where('destination_guides.guideable_id', $this->authorityPage->id)
        ->where('destination_guides.guideable_type', 'App\Models\AuthorityPage')
        ->select('destination_guide_details.*');
        return $query;
    }

    public function html(): HtmlBuilder
    {
        return $this->builder()
        ->setTableId('destination-guide-table')
        ->columns($this->getColumns())
        ->minifiedAjax()
        ->dom('rt' . "<'row'<'col-sm-12 col-md-5'l><'col-sm-12 col-md-7'p>>",)
        ->addTableClass('table align-middle table-row-dashed fs-6 gy-5 dataTable no-footer text-gray-600 fw-semibold')
        ->setTableHeadClass('text-start text-muted fw-bold fs-7 text-uppercase gs-0');
    }


    public function getColumns(): array
    {
        return [
            Column::make('guide_type')->name('guide_type'),
            Column::make('content')->name('Content'),
            Column::make('action')->name('Action')->width('10%'),
        ];
    }

    protected function filename(): string
    {
        return 'AuthorityPageGuideContent_' . date('YmdHis');
    }
}

问题是它会返回一条警告

DataTables warning: table id=destination-guide-table - Invalid JSON response. 
For more information about this error, please see http://datatables.net/tn/1

推荐答案

首先,设置调用DataTable类的路由:

Route::get('/authority-pages/{authorityPage}/destination-guide', [AuthorityPageController::class, 'getAuthorityPageGuideContentData'])->name('authority-pages-get-destination-guide-content');
Route::get('/authority-pages/{authorityPage}/accordions', [AuthorityPageController::class, 'getAuthorityPageAccordionData'])->name('authority-pages-get-accordions');

在控制器中,确保方法如下所示:

public function getAuthorityPageAccordionData(AuthorityPage $authorityPage, AuthorityPageAccordionDataTable $authorityPageAccordionDataTable)
{
    return $authorityPageAccordionDataTable->with([
        'authorityPage' => $authorityPage
    ])->render('pages.authority-pages.show');
}

public function getAuthorityPageGuideContentData(AuthorityPage $authorityPage, AuthorityPageGuideContentDataTable $authorityPageGuideContentDataTable)
{
    return $authorityPageGuideContentDataTable->with([
        'authorityPage' => $authorityPage
    ])->render('pages.authority-pages.show');
}

现在,在show方法中,从DataTable实例调用html()方法.您可以在视图中使用结果变量,如下所示:

$accordionDt = $authorityPageAccordionDataTable->with([
    'authorityPage' => $authorityPage
])->html();

$authorityContentDt = $authorityPageGuideContentDataTable->with([
    'authorityPage' => $authorityPage
])->html();

return view('pages.authority-pages.show', [
    'authorityPage' => $authorityPage,
    'blogPosts' => $blogPosts,
    'tours' => $tours,
    'destinations' => $destinations,
    'keywordIntents' => $keywordIntents,
    'authorityContentDt' => $authorityContentDt,
    'accordionDt' => $accordionDt
]);

现在,在您的视图中,您可以使用{{$authorityContentDt->table()}}{{$authorityContentDt->scripts()}}来包括DataTable的HTML和脚本.

Php相关问答推荐

使用WooCommerce本机产品短码时不考虑价格筛选(_SHORT CODE)

无法使用DOMPDF在PDF中呈现非ANSI字符

WooCommerce在购买后多次重定向

使用PHP编码的字符串与使用OpenSSL编写的shell 代码之间的差异

在WooCommerce获取所有具有全球产品属性品牌的简单产品

如何在汇总表构建器中操作LALAVEL细丝Sum::Make()值

在WooCommerce上克隆订单时使用`add_product()`添加产品元数据

如果WooCommerce购物车在 checkout 时被清空,请重定向至store 页面

如何从phpseclib和SFTP获取日志(log)记录?

用ajax获取文件 - CORS问题&;设置缓存?

如何在自定义邮箱内容中获取WooCommerce订单项目的详细信息?

更改特定产品标签的 Woocommerce 产品名称

如何解决 Laravel 外键错误?

Filament v3:具有 hasMany 和 BelongsTo 的关系管理器

Wordpress,配置一周第一天选项

无法在 WordPress 上获取 JSON 数据

invalid_grant 和无效的 JWT 签名

保留 .htaccess 中的符号登录 URL

在 EasyAdmin 中添加全局操作,将我的按钮放在表格下方而不是上方

为什么 PDO 允许使用带命名占位符的索引数组,但仅在禁用仿真时才允许?