【问题标题】:Datatables(yajrabox) nested object display (laravel hasMany relationship )Datatables(yajrabox) 嵌套对象展示(laravel hasMany 关系)
【发布时间】:2017-10-27 19:49:32
【问题描述】:

我最近一直在使用它,它运行得很好,但现在我需要调用一个“hasMany”关系并将其显示在表格中

这是我的控制器:

public function anyData()
{
  $posts = Test::with('cola')->select('tests.*');
  return \DataTables::eloquent($posts)->make(true);
}

它给了我一个这样的数组(testanyData 路由):

{
    "draw": 0,
    "recordsTotal": 1,
    "recordsFiltered": 1,
    "data": [
        {
            "id": "1",
            "nombre": "Sigrid Mann",
            "descripcion": "lPcA0",
            "stock": "49",
            "imagen": "s14gN",
            "created_at": "2017-10-27 18:35:54",
            "updated_at": "2017-10-27 18:35:54",
            "cola": [
                {
                    "test_id": "1",
                    "tipo": "1",
                    "precio": "1",
                    "created_at": null,
                    "updated_at": null
                }
            ]
        }
    ],
    "queries": [
        {
            "query": "select count(*) as aggregate from (select '1' as `row_count` from `tests`) count_row_table",
            "bindings": [],
            "time": 0.6
        },
        {
            "query": "select `tests`.* from `tests`",
            "bindings": [],
            "time": 0.46
        },
        {
            "query": "select * from `precios` where `precios`.`test_id` in (?)",
            "bindings": [
                1
            ],
            "time": 0.52
        }
    ],
    "input": []
}

这是我的 js:

$(document).ready(function() {
  listar();
});
var listar = function () {
  var table = $('#productos').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "testanyData",
    "columns":[
      {data:'id'},
      {data:'nombre'},
      {data:'stock'},
      {data: 'cola.precio'},
      {defaultContent: "some buttons"}
    ],
    "language": idioma_esp
  });
}

但是当我加载视图时,我会收到一个警报:

DataTables 警告:表 id=productos - 请求的未知参数 'cola.precio' 用于第 0 行第 3 列。有关此的更多信息 错误,请看http://datatables.net/tn/4

这些是我在视图中的表:

<div class="row-fluid margin-body">
  <table id="productos" class="table table-hover table-condensed">
    <thead>
      <tr>
        <th>Id</th>
        <th>Producto</th>
        <th>Stock</th>
        <th>Precio</th>
        <th></th>
      </tr>
    </thead>
  </table>
</div>

我想在标签“Precio”中显示“precio”。

【问题讨论】:

    标签: jquery laravel datatables


    【解决方案1】:

    您的可乐关系似乎是 hasMany,因为它返回响应中指示的数组。

    "cola": [ { "test_id": "1", "tipo": "1", "precio": "1", "created_at": null, "updated_at": null } ]

    在这方面,您也应该以数组的形式访问数据。

    {data: 'cola.0.precio', name: 'cola.precio'},

    或者您可以使用 dataTables.js 库的render api 并循环可乐的所有可能值并相应地呈现它。

    【讨论】:

    • 它有效,非常感谢你,这让我发疯了
    猜你喜欢
    • 2019-08-01
    • 2013-09-14
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 2014-10-27
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多