【问题标题】:Process data after using Datatables server side on Laravel 5.3在 Laravel 5.3 上使用 Datatables 服务器端后处理数据
【发布时间】:2017-06-05 06:55:58
【问题描述】:

我在数据库中有一个包含 73k 条记录的表。 因此,我想使用 Datatables 仅在一个视图中显示它。


这个表应该是: Datatables in client side

我已经有一个带有数据表的表,但它是有限的项目。 第一列不在数据库中, “显示图像”列是特殊功能,当经理将鼠标悬停在此文本上时,显示的图像 我想在服务器端使用数据表来加载所有数据并改变外观。 所以我做了一个控制器:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Yajra\Datatables\Datatables;
use App\Models\GS1;
class DataTablesController extends Controller
{
    private $sa24Repository;

//    function __construct(SA24Repository $sa24Repository)
//    {
//        $this->sa24Repository = $sa24Repository;
//    }
    public function getIndex() {;
        return Datatables::of(Product::query())->make(true);
    }
}
?>

用一个简单的视图: Products table 它运行良好,但是当我输入搜索输入时,它会搜索所有列。 我必须将此代码添加到使用过滤器:

<script>
    $('.testTable').dataTable();
    $.fn.dataTable.ext.errMode = 'throw';
    $('.testTable').dataTable({
        destroy: true,
        processing: true,
        serverSide: true,
        ajax: '/datatables',
        columns: [
            {data: 'gtin'},
            {data: 'brand_name'},
            {data: 'description_short'}
        ]
    });
</script>

如果我删除此代码:

columns: [
    {data: 'gtin'},
    {data: 'brand_name'},
    {data: 'description_short'}
]

当我搜索浏览器记录时:

Uncaught Error: DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
    at Ae (all.js?v=1484881746:6)
    at k (all.js?v=1484881746:5)
    at j (all.js?v=1484881746:5)
    at M (all.js?v=1484881746:5)
    at U (all.js?v=1484881746:6)
    at all.js?v=1484881746:5
    at d (all.js?v=1484881746:5)
    at Object.success (all.js?v=1484881746:5)
    at d (all.js?v=1484881746:2)
    at Object.fireWith [as resolveWith] (all.js?v=1484881746:2)

我什么都搜索不到

所以你能帮我使用数据表来搜索我想要的任何列吗? 而且我不必在脚本中添加这个 sn-p:

columns: [
    {data: 'gtin'},
    {data: 'brand_name'},
    {data: 'description_short'}
]

【问题讨论】:

    标签: php jquery laravel datatables


    【解决方案1】:

    我已经通过在 DatatableController 中进行修改来解决这个问题。而且效果很好

    【讨论】:

      【解决方案2】:

      columns 数组决定了要在数据表中显示的内容。想一想,如果客户端甚至不知道要显示哪些列,它会如何渲染表格?

      如果您只想搜索有限的列,您可以在这样的列中添加searchable: false(假设您不想根据品牌名称进行搜索):

      columns: [
          {data: 'gtin'},
          {data: 'brand_name', searchable: false},
          {data: 'description_short'}
      ]
      

      【讨论】:

      猜你喜欢
      • 2014-06-24
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      相关资源
      最近更新 更多