首页 文章

如何获取数据表中对象的数据?

提问于
浏览 412
-2

我从json文件获取数据到我的数据表中 .

"columns": [
  {% for key, value in columns %}
    {
    "data": "{{ key }}"},
    {% endfor %}
]

像这样我得到以下输出:

id  name            slug      icon  
2   Mitarbeiter     members   [object Object]   
3   Angebote        offers    [object Object]   
4   Produkte        products  [object Object]   
5   Felder          fields    [object Object]

为了接收对象的数据,我将代码更改为:

"columns": [
      {% for key, value in columns %}
      {   "data": "{{ key }}.name",
      "defaultContent": "{{ key }}"},
      {% endfor %}
    ]

这对该对象很有效,但现在我的其他字段不再显示该值,显示该列的标签:

id  name    slug    icon 
id  name    slug    icon    
id  name    slug    anchor  
id  name    slug    adjust  
id  name    slug    cloud

列转储:

array:5 [▼
  "id" => ReflectionProperty {#6092 ▶}
  "name" => ReflectionProperty {#6094 ▶}
  "slug" => ReflectionProperty {#6096 ▶}
  "icon" => ReflectionProperty {#6097 ▶}
]

另一种方法是:

"columns": [
      {% for key, value in columns %}
      {% if key is iterable %}
      {"data": "{{ key }}"},
      {% else %}
      {"data": "{{ key }}.name"},
      {% endif %}
      {% endfor %}
    ]

但在这里我只得到 icons 行的输出......


json文件是这样的:

[{"id":2,"name":"Mitarbeiter","icon":{"id":2,"name":"anchor"},"slug":"members"},{"id":3,"name":"Angebote","icon":{"id":1,"name":"adjust"},"slug":"offers"},{"id":4,"name":"Produkte","icon":{"id":1,"name":"adjust"},"slug":"products"},{"id":5,"name":"Felder","icon":{"id":1,"name":"cloud"},"slug":"fields"}]

2 回答

  • 0

    真的使用:of_type('object')

    "columns": [
        {% for key, value in columns %}
            {
                "data": "{% if key is of_type('object') %}{{ key }}.name{% else %}{{ key }}{% endif %}"},
        {% endfor %}
        { "data": "id" }
    ]
    
  • -1

    终于找到了解决方案:

    "columnDefs": [
        {
            "render": function (data, type, row) {
              var type = typeof data;
              if(type == "object"){
                return data.name;
              } else {
                return data;
              }
    
            },
            "targets": "_all"
          }
        ],
    "columns": [
         {% for key, value in columns %}
         {   "data": "{{ key }}"},
         {% endfor %}
       ]
    

相关问题