简体   繁体   English

如何从JSON数组动态构建表?

[英]How to dynamically build a table from a JSON array?

I've placed an AJAX GET call in an Enyo view. 我已经在Enyo视图中放置了一个AJAX GET调用。 The GET calls a web service which returns an array of records including the column headers. GET调用Web服务,该服务返回包含列标题的记录数组。

My aim is to dynamically build a table with this returned array, where a row is created for each index and columns for each header within the index. 我的目标是使用返回的数组动态构建表,其中为每个索引创建一行,为索引内的每个标头创建一列。

What I do know in terms of Enyo is to create one record by mapping the AJAX response headers to component fields: 我对Enyo的了解是通过将AJAX响应标头映射到组件字段来创建一条记录:

        this.$.actionsTaken.setContent( inResponse.ActionsTaken);

But I'm not sure how to do that dynamically and create the table on the fly. 但是我不确定如何动态地进行操作并动态创建表。

So for example when I inspect the web service response my index 0 contains the following: (Where ActionsTaken , Application and EM are the col headers.) 因此,例如,当我检查Web服务响应时,我的索引0包含以下内容:(其中ActionsTakenApplicationEM是col标头。)

{
    ActionsTaken: "Tested uptime"
    Application: "2011 Hanko"
    EM: "EM102 "
}

Question: 题:

How can you dynamically build a table from a JSON array? 如何从JSON数组动态构建表?

The AJAX GET: AJAX GET:

 fetch: function() { 
    var ajax = new enyo.Ajax({


        url: "http://testservice.net/WebService/GetHistory",
        async: false,
        handleAs:"json",
        xhrFields: {withCredentials: true}

    });        

    ajax.go(this.data);      
    ajax.response(this, "gotResponse");
    ajax.error(this, function(inSender, inError) {
        Misc.hideMask();
        ViewLibrary.back();
        Misc.showToast("Error retrieving data");
    });

},
gotResponse: function(inSender, inResponse)
{

    var debugVar = inResponse;


        this.$.actionsTaken.setContent( inResponse.ActionsTaken);
        this.$.configurationItemLogicalName_value.setContent( inResponse.Application);
        this.$.eM.setContent( inResponse.EM);


},

The components that hold the data values: 包含数据值的组件:

{name:"outage_table", kind: "FittableRows",components:[

            {content: "Actions Taken",                     name: "actionsTaken",           },
            {content: "Application",          name: "application",      },
            {content: "EM",          name: "eM",      },


        ]}

Depending on the complexity of all your data, you might be able to do this fairly simply. 根据所有数据的复杂性,您可能可以相当简单地完成此操作。 Iterate through your array and on each object, you can then iterate through its keys and create each column with its data. 遍历数组和每个对象,然后可以遍历其键并使用其数据创建每一列。

Something like: 就像是:

for (var k in theObject) {
    // make column k
    // add theObject[k] to it
}

I think the problem is that you have created named components that match this example data, but it is unknown if those will always be the same keys? 我认为问题在于您已经创建了与该示例数据匹配的命名组件,但是不知道这些组件将始终是相同的键吗?

There is a kind (enyo.DataTable, which, surprisingly, I have never used) that you might use instead. 您可能会使用一种(enyo.DataTable,我从未使用过)。 It lets you add rows (no headers) so you would make your first row from all the object keys, then the next row would be those keys' data. 它使您可以添加行(无标题),以便从所有对象键中创建第一行,然后下一行是这些键的数据。 It is derived from DataRepeater so there may be some implementation to work out, such as using an enyo.Collection to store your data and then set the collection to the DataTable. 它是从DataRepeater派生的,因此可能需要进行一些实现,例如使用enyo.Collection存储数据,然后将集合设置为DataTable。

The other alternative that matches closer to what you have would be to just dynamically create the components as you need them: 另一个更接近于您的替代方法是根据需要动态创建组件:

this.$.outage_table.createComponents([{...}]);
this.$.outage_table.render(); // need to re-render when dynamically adding components

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

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