简体   繁体   English

显示从json数据到卡的数组

[英]Display array from json data to cards

So, im a little bit lost here and i need some help. 因此,即时通讯在这里有些失落,我需要一些帮助。 I have a json that come from the server with data that i dont know. 我有一个来自服务器的json,其中包含我不知道的数据。

Based on that i found a solution to display the data on html here on SO: https://stackoverflow.com/a/50352965/9721446 基于此,我找到了一种解决方案,可以在SO上的html上显示数据: https : //stackoverflow.com/a/50352965/9721446

But the problem is that each "item" is an entry from array, so if i ngfor array, it outputs each line as an item, and i want the item to be all entries of each result. 但是问题是每个“项目”都是数组中的一个条目,所以如果在数组中,它将每个行作为一个项目输出,而我希望该项目是每个结果的所有条目。

heres the html : 继承人的html

<ng-container *ngFor="let item of singleArray | paginate: { itemsPerPage:411, currentPage: p} ">
         <!-- All the entries -->
         <div class="w3-container">

          <!-- Table view-->
          <table class="center">
             <tr *ngIf="!item.tag.includes('URL') && !item.tag.includes('linkChal')">
              <td><div class="col-xs-auto thick">{{item.tag.toLowerCase() | translate}}</div></td>
               <td class="tab">{{item.value}}</td>
               </tr>
               <tr *ngIf="item.tag.includes('URL')">
                        <td>Link da entrada: </td>
                        <td> <a href="{{item.value}}">- Ver mais -</a></td>
                </tr>
                <tr *ngIf="item.tag.includes('linkChal')">
                         <td>Link do Challenge: </td>
                         <td> <a href="{{item.value}}">- Challenge -</a></td>
                </tr>
           </table>

           <div style="background-color: #ff7d2a">
               <ul *ngIf=" item.tag.includes('---------')"><p>New Entry</p></ul>
               </div>
            </div>
 </ng-container>

Ts: Ts:

for(let i in res)
        {

            //array with entities from json
            this.entity.push(i);

            for(let j in res[i])
            {
                let val = Number(j)+1;
                this.cont.push(i +"  - nº: " + val );
                this.singleArray.push({
                    tag: i,
                    value: val
                });

                for(let t in res[i][j])
                {
                    this.test.push(t);
                    this.cont.push(t +" - "+ this.responseList[i][j][t]) ;

                    if(t.split(".",2)[1] === "CompositeId")
                    {
                        this.test.push("URL:");
                        //Get the id
                        this.cont.push(this.moduleName + "/" + t.split(".",2)[0] + "/" + this.responseList[i][j][t].match(/=(.*)_/)[1]);
                        //debugger;
                        this.singleArray.push({
                            tag: "URL:",
                            value: this.moduleName + "/" + t.split(".",2)[0] + "/" + this.responseList[i][j][t].match(/=(.*)_/)[1]
                        });
                    }
                    else if(t.split(".",2)[1] === "Challenge")
                    {
                        this.singleArray.push({
                            tag: "linkChal",
                            value: this.moduleName + "/" +t.split(".",2)[1] + "/" + this.responseList[i][j][t].match(/=(.*)_/)[1]
                        });
                    }
                    else {
                        this.singleArray.push({
                            tag: t,
                            value: this.responseList[i][j][t]
                        });
                    }

                }
                this.test.push("\n");
                this.cont.push("\n");
                this.singleArray.push({
                    tag: "---------\n",
                    value: "--------\n"
                });

                 //it ends an item here 
            }
        }

Heres the output i have with that: 这是我的继承人输出:

结果

Each one line is an entry from the array, the big question is, how to transform all lines/entries until " New Entry " and made an single item to ngfor and display data into a card that i already have..) 每行是数组中的一个条目,最大的问题是,如何转换所有行/条目,直到“ New Entry ”,并将单个项目设置为ngfor并将数据显示到我已经拥有的卡中。)

I've tried to create an array and push the singleArray into it (hoping each entry of that new array was an item that i want), at the end of for(let j in res[i]) on .ts but it just repeated all the entries creating a bunch of entries.. here, at the end of that for, i've tried to push an array with something, then ngfor it (it gives me the number items that i want, but then i dont have the results to access them..) 我尝试在.ts上for(在res [i]中的let j )的末尾创建一个数组并将singleArray推入其中(希望新数组的每个条目都是我想要的项目),但它只是重复所有条目,创建一堆条目..在这里,最后,我尝试用某个东西推入一个数组,然后用ngfor(它给了我想要的数字,但是我没有结果以访问它们。)

Has anyone had this problem before? 有人遇到过这个问题吗? thanks in advance 提前致谢

Edit: here's what singleArray looks like: 编辑:这是singleArray的样子:

在此处输入图片说明

Your best bet here is to follow the single responsibility principal and separate the concerns of each class. 您最好的选择是遵循单一职责原则,分开每个班级的关注点

Stop trying to do this all in the view and separate out the responsibility of formatting the data and the problem will seem much simpler. 停止尝试在视图中完成所有操作,并排除格式化数据的责任,这个问题看起来会简单得多。

  • Make a new class to define the model you want your view to use 创建一个新的类来定义你希望你的视图使用模型
  • Have your view implement this new ideal model that you control 让您的视图实现您控制的新理想模型
    • Generate some test data to make get this looking like what you want 生成一些测试数据,使它看起来像您想要的
  • Create a new class who's entire responsibility is to turn the external model from the api response into this new internal model 创建一个新类,其全部职责是将外部模型从api响应转换为这个新的内部模型
    • json2ts may help generate a better external model from the response, but it may not be of much use in this case json2ts可能有助于从响应中生成更好的外部模型,但在这种情况下可能用处不大

Once you have done the above, based on your sample output, it should be fairly simple to convert from the external model into the internal model. 完成上述操作后,根据示例输出,从外部模型转换为内部模型应该相当简单。 It's hard to convey this, but assuming the hyphens are the item separator you could simply do something like the following: 很难传达这一点,但是假设连字符是项目分隔符,您可以简单地执行以下操作:

    const externalItems = // data from api
    const internalItems = [];
    let currentInternalItem = {};

    externalItems.forEach(item => {
     if (item.tag.startsWith('---------')) {
         internalItems.push(currentInternalItem);
         currentInternalItem = {};
     } else {
        currentInternalItem[item.tag] = item.value;
     }
    });

This would group the array back into an object that you can use in your view. 这会将数组重新组合为可以在视图中使用的对象。

I think I'm complicating too much.. The objective here is to display what comes from JSON into specific locations, like a card, with header and content, to better display the results. 我想我太复杂了。这里的目的是将JSON产生的内容显示在特定的位置(例如卡),标头和内容,以更好地显示结果。

I have a service that gives me a JSON, that i never knows what inside, that depends on the search term and can bring much information. 我有一个为我提供JSON的服务,该服务我永远不知道里面是什么,它取决于搜索词并可以带来很多信息。 For example: 例如:

If the term is "Idea": 如果术语是“想法”: 在此处输入图片说明

If the term is Challenge: 如果术语是“挑战”:
在此处输入图片说明

My .ts file is only console.log what comes from the api. 我的.ts文件只是console.log,它来自api。

 ngOnInit() {
    var setting = {setting to connect the server..}
    enter code here
    $.ajax(settings).done((rest) => this.response(rest));
    }

response(res){

        console.log(res);
}

How can i display that data the way i want? 如何以所需方式显示该数据? Sorry for the long post, and for not beeing objective on the main question. 很抱歉,任职时间较长,并且对主要问题不满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM