从两个json结构对数据表进行排序 [英] Sorting data tables from two json structures

查看:62
本文介绍了从两个json结构对数据表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据字段名对数据进行排序,将其放在表的正确位置以便于读取,并在需要时导出.

I'm trying to sort data based on their field name, in the correct position on the table to be read easily and exported if need be.

到目前为止,我在布局方面遇到了麻烦,请确保所有内容都正确布置.

So far I'm having trouble with the layout, ensuring that everything is laid out correctly.

我有四个表:事件",卡牌","card_events(多态)"和潜在顾客".

I have four tables: Events, Cards, card_events(polymorphic) and Leads.

卡片属于事件,卡片包含JSON形式的字段,如下所示:

Cards belong to Events and a Card contains fields in the form of JSON which looks like this:

[
  {
    "name": "Title",
    "type": "dropdown",
    "options": [
      "Mr",
      "Miss",
      "Mrs",
      "Ms",
      "Dr"
    ]
  },
  {
    "name": "First Name",
    "type": "text"
  },
  {
    "name": "Last Name",
    "type": "text"
  },
  {
    "name": "Email",
    "type": "text"
  },
  {
    "name": "Phone Number",
    "type": "numeric"
  },
  {
    "name": "Opt in?",
    "type": "bool"
  }
]

由于这些字段可以是用户根据他们希望用户填写的数据定义的任何内容,因此在尝试使所有数据正确同步时,使工作变得更加困难.这是我目前所拥有的:

Since these fields can be anything defined by the user based on the data they want users to fill in it makes life a lot harder when trying to get all the data to sync up correctly. Here's what I currently have so far:

如您所见,数据遍布整个地方.更为困难的是,可以在任何给定时间更改卡的顺序.

As you can see, the data is kind of all over the place. What makes this more difficult, is the card order can be changed at any given time.

当前,它的工作方式是,如果卡的排列顺序与您填写时的顺序相同,则效果很好,但是如果用户未填写特定的卡,则可以将其禁用,这就是问题所在开始.

Currently it works in a way that if the cards stayed in the same order as you filled them out, it would work great however if users aren't filling in a specific card, you could disable it and this is where the issues begin.

所以这是我的代码,用于显示我试图与表格标题名称匹配的销售线索:

So here's my code for displaying the leads which I've attempted to match up to the table heading name:

@foreach($leads as $lead)
    <?php
    $fields = json_decode($lead->data);
    ?>
    <tr>
        @foreach($fields as $f)

            @foreach($cards as $card)
                <?php $fields = json_decode($card->fields); ?>
                @foreach($fields as $field)
                    @if($field->name == $f->name)
                        @if($field->type == 'bool')
                            <td>{{$f->value === true ? 'Yes' : 'No'}}</td>
                        @else
                            <td>{{$f->value}}</td>
                        @endif
                    @endif
                @endforeach
            @endforeach

        @endforeach
    </tr>
@endforeach

这是我如何生成表标题:

This how I generate the table headings:

@foreach($cards as $card)
    <?php $fields = json_decode($card->fields); ?>
    @foreach($fields as $field)
        <th>{{$field->name}}</th>
    @endforeach
@endforeach

由于这取决于用户填写的内容,因此以下是某些值的数据结构:

Since this is dependant on the the user has filled in, here is the data structure for some of the values:

[
  {
    "name": "Title",
    "value": "Mrs"
  },
  {
    "name": "First Name",
    "value": "Sally"
  },
  {
    "name": "Last Name",
    "value": "Jones"
  },
  {
    "name": "Email",
    "value": "test@exmaple.com"
  },
  {
    "name": "Phone Number",
    "value": "0123456789"
  },
  {
    "name": "Opt in?",
    "value": true
  },
  {
    "name": "Flat\/House Number",
    "value": "123"
  },
  {
    "name": "Street",
    "value": "Generic Street"
  },
  {
    "name": "Post Code",
    "value": "TE57 1NG"
  }
]

我可以单独显示每条引线,这会使生活更轻松,但是将来在导出数据时,我仍然会遇到这个问题.

I could show each lead individually which would make life easier but I'd still run in to this issue in the future when I come to export the data.

我认为应该做的事情与我已经做过的事情类似,请尝试将标题与字段名称匹配.

What I think should be done is something similar to the way I've done it already, try and match up the headings to the field name.

如果有人可以帮助整理数据,我将不胜感激!

If there's anyone who could help get this data lined up, I would highly appreciate it!

很高兴我在网站的其他地方使用Vue时,可以在基于Vue的JS中回答此问题.

推荐答案

在浪费了整个上午和整个下午的时间后,我返回了解决方案!

After wasting all morning and half of my afternoon, I return with the solution!

如果您想改善这篇文章的答案,那不是最漂亮的,绝对不是最好的.

It's not the prettiest, definitely not the best, if you want to improve upon this post an answer.

基本上只是$ needle/$ haystack路线,这是代码!

Basically just the $needle / $haystack route, here's the code!

leads.blade.php

<table class="table is-striped is-fullwidth">
                <thead>
                <tr>
                    @foreach($headings as $head)
                        <th>{{$head}}</th>
                    @endforeach
                </tr>
                </thead>
                <tbody>
                @foreach($leads as $lead)
                    <tr>
                        @foreach($headings as $key => $head)
                            <?php  $placed = false; ?>
                            @foreach($lead as $val)
                                @if($val->name === $head)
                                    <td>
                                        {{ $val->value ? $val->value : 'N/A' }}
                                    </td>

                                    <?php
                                    $placed = true;
                                    break;
                                    ?>
                                @endif
                            @endforeach
                            @if($placed === false)
                                <td></td>
                                <?php  continue; ?>

                            @endif
                        @endforeach
                    </tr>
                @endforeach
                </tbody>
            </table>

控制器

$headings = [];
    foreach($cards as $card) {
        $fields = json_decode($card->fields);
        foreach($fields as $field) {
            array_push($headings, $field->name);
        }
    }

    $leads = [];
    foreach($event->leads as $lead) {
        $data = json_decode($lead->data);
        array_push($leads, $data);
    }

这篇关于从两个json结构对数据表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆