简体   繁体   English

未创建新行

[英]The new rows are not being created

My goal is to create 4 columns with 15 rows. 我的目标是创建15列的4列。

However, right now I'm able to create only two rows. 但是,现在我只能创建两行。 One header row and another long row with 15 columns. 标题行和另一行长的15列。

New rows are not being created and it is inserting all the values into one row. 没有创建新行,而是将所有值插入一行。

I'm really confused why it is only inserting it into one row, even though I opened and closed tag outside of the row that creates the columns. 我很困惑为什么它只将其插入一行,即使我在创建列的行之外打开和关闭了标签。

Can someone explain me why it is not creating new rows? 有人可以解释我为什么不创建新行吗? Any help will be greatly appreciated! 任何帮助将不胜感激!

Here is my code: 这是我的代码:

var $list = $('<table>');                    
                    $list.append($('<tr>'));

                    TFS_Wit_WebApi.getClient().getWorkItem(284)
                        .then(function (query) {

                            $list.append($('<th>').text("Title"));
                            $list.append($('</th>'));

                            $list.append($('<th>').text("State"));
                            $list.append($('</th>'));

                            $list.append($('<th>').text("Created by"));
                            $list.append($('</th>'));


                            $list.append($('<th>').text("Created date"));
                            $list.append($('</th>'));
                            $list.append($('</tr>'));


                            for (i = 0; i < query.rev; i++) {
                                $list.append($('<tr>'));
                                for (j = 0; j < 3; j++) {                                 
                                    TFS_Wit_WebApi.getClient().getRevision(284, 6)
                                     .then(function (query) {
                                         $list.append($('<td>').text(query.fields['System.State']));
                                         $list.append($('</td>'));
                                     });

                                }
                                $list.append($('</tr>'));
                            }
                        });

                    $list.append($('</table>'));
                    var $container = $('#query-info-container');
                    $container.empty();
                    $container.append($list);

EDITED: 编辑:

Here is the json file that I'm trying to access and display the information: 这是我尝试访问并显示信息的json文件:

{
  "count": 15,
  "value":
  [
    {
      "id": 284,
      "rev": 1,
      "fields": {
        "System.WorkItemType": "User Story",
        "System.State": "New",
        "System.Reason": "New",
        "System.CreatedDate": "2016-06-23T14:31:37.567Z",
        "System.CreatedBy": "zxc",
        "System.ChangedDate": "2016-06-23T14:31:37.567Z",
        "System.ChangedBy": "zxc",
        "System.TeamProject": "zxc",
        "System.AreaPath": "zxc",
        "System.IterationPath": "zxc",
        "System.Title": "Story5",
        "System.BoardColumnDone": false,
        "Microsoft.VSTS.Common.Priority": 2,
        "Microsoft.VSTS.Common.ValueArea": "Business",
        "WEF_A698283685984E51AE185C4A3D29694A_Kanban.Column.Done": false,
        "sigArbor.TestProcess.Stage": "Unassigned",
        "System.BoardColumn": "New",
        "Microsoft.VSTS.Common.StateChangeDate": "2016-06-23T14:31:37.567Z",
        "WEF_A698283685984E51AE185C4A3D29694A_Kanban.Column": "New"
      },
      "url": "https://zxc.visualstudio.com/_apis/wit/workItems/284/revisions/1"
    },
    {
      "id": 284,

      "rev": 2,
      "fields": {
        "System.WorkItemType": "User Story",
        "System.State": "New",
        "System.Reason": "New",
        "System.CreatedDate": "2016-06-23T14:31:37.567Z",
        "System.CreatedBy": "zxc",
        "System.ChangedDate": "2016-06-23T14:31:37.92Z",
        "System.ChangedBy": "zxc",
        "System.TeamProject": "zxc",
        "System.AreaPath": "zxc",
        "System.IterationPath": "zxc",
        "System.Title": "Story5",
        "System.BoardColumnDone": false,
        "Microsoft.VSTS.Common.Priority": 2,
        "Microsoft.VSTS.Common.ValueArea": "Business",
        "WEF_A698283685984E51AE185C4A3D29694A_Kanban.Column.Done": false,
        "sigArbor.TestProcess.Stage": "Unassigned",
        "Microsoft.VSTS.Common.StackRank": 1624983310.0,
        "System.BoardColumn": "New",
        "Microsoft.VSTS.Common.StateChangeDate": "2016-06-23T14:31:37.567Z",
        "WEF_A698283685984E51AE185C4A3D29694A_Kanban.Column": "New"
      },
      "url": "https://zxc.visualstudio.com/_apis/wit/workItems/284/revisions/2"
    },

    {
      "id": 284,
      "rev": 3,
      "fields": {
        "System.WorkItemType": "User Story",
        "System.State": "New",
        "System.Reason": "New",
        "System.AssignedTo": "zxc",
        "System.CreatedDate": "2016-06-23T14:31:37.567Z",
        "System.CreatedBy": "zxc",
        "System.ChangedDate": "2016-06-23T14:32:00.777Z",
        "System.ChangedBy": "zxc",
        "System.TeamProject": "zxc",
        "System.AreaPath": "zxc",
        "System.IterationPath": "zxc",
        "System.Title": "Story5",
        "System.BoardColumnDone": false,
        "Microsoft.VSTS.Common.Priority": 2,
        "Microsoft.VSTS.Common.ValueArea": "Business",
        "WEF_A698283685984E51AE185C4A3D29694A_Kanban.Column.Done": false,
        "sigArbor.TestProcess.Stage": "Unassigned",
        "Microsoft.VSTS.Common.StackRank": 1624983310.0,
        "System.BoardColumn": "New",
        "Microsoft.VSTS.Common.StateChangeDate": "2016-06-23T14:31:37.567Z",
        "WEF_A698283685984E51AE185C4A3D29694A_Kanban.Column": "New"
      },
      "url": "https://zxc.visualstudio.com/_apis/wit/workItems/284/revisions/3"

and the same format continues... 并继续使用相同的格式...

You need to append your td elements to the tr elements (and you shouldn't try to explicitly close the tags) 您需要将td元素追加到tr元素(并且您不应该尝试显式关闭标签)

var $tr = $list.append('<tr>')
for (...) {
  var $td = $('<td>').text(...)
  $tr.append($td)
}

Think of it as building the tree structure, creating the top level nodes first, then adding the child nodes to their parents. 将其视为构建树结构,首先创建顶层节点,然后将子节点添加到其父节点。

Since you have one async request inside another and your code doesn't append when the promise is resolved, you will almost always have 15 rows created together and ajax requesting processing in parallel, which when data is available will populate the data in the last row everytime. 因为您在另一个请求中有一个异步请求,并且在解决了诺言后您的代码没有追加,所以几乎总是在一起创建15行,并且ajax并行请求处理,当有数据时,将在最后一行填充数据每次。

I believe a single api request should be sufficient. 我相信一个API请求就足够了。 However, if you have no choice in that matter then you should be appending the row and cells after the api sends the response. 但是,如果您别无选择,则应在api发送响应后附加行和单元格。

Solution below might need some change depending on the API's response data. 以下解决方案可能需要根据API的响应数据进行一些更改。

var $list = $('<table>');
TFS_Wit_WebApi.getClient().getWorkItem(284).then(function(query) {
    $list.append('<tr><th>Title</th><th>State</th><th>Created by</th><th>Created date</th></tr>');
    for (i = 0; i < query.rev; i++) {
        TFS_Wit_WebApi.getClient().getRevision(284, 6).then(function(query) {
            var tr = $list.append('<tr>');
            for (j = 0; j < 3; j++) {
                tr.append('<td>').text(query.fields['System.State']));
            }
        });
    }
});
var $container = $('#query-info-container');
$container.empty();
$container.append($list);

EDIT: Also a big problem for async request in for loop is that if data has say 100 rows, then 100 requests will be sent to fetch one row at a time. 编辑:for循环中异步请求的另一个大问题是,如果数据说100行,则将发送100个请求以一次获取一行。 Better is to receive all 100 rows in one request and then create the table from the data. 更好的方法是在一个请求中接收所有100行,然后根据数据创建表。

EDIT: Based on the server response, try the following: 编辑:基于服务器的响应,请尝试以下操作:

var $list = $('<table>');
TFS_Wit_WebApi.getClient().getWorkItem(284).then(function(response) {
    $list.append('<tr><th>Title</th><th>State</th><th>Created by</th><th>Created date</th></tr>');
    // I am assuming entire json data is response
    for (i = 0; i < response.value.length; i++) {
        var tr = $list.append('<tr>'), data = response.value[i];
        for (j = 0; j < 3; j++) {
            tr.append('<td>').text(data['System.State']));
        }
    }
});
var $container = $('#query-info-container');
$container.empty();
$container.append($list);

Also, do not use "." 另外,请勿使用“。” in the key of json object. 在json对象的键中。 You should refer to data['System.State'] as data.System.State, but you won't be able to because there is no "System" object in the "data". 您应该将data ['System.State']引用为data.System.State,但由于“数据”中没有“系统”对象,因此您将无法执行此操作。

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

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