【问题标题】:JQuery: using a for each function inside another different for each functionJQuery:在每个函数中使用一个用于每个函数的另一个不同的函数
【发布时间】:2021-11-25 12:09:09
【问题描述】:

首先,我将展示我将在另一个for each 中使用的for each,以显示有数据:

每个有问题的(下拉菜单)

Ajax 函数:

(function() {
    $.ajax({
        type: "GET",
        url: "/clinical/bbr-group-configuration-group-list-type-name",
        dataType: "json",
        success: function (response){
            var tbody="";
            $.each(response.all_group_type_names, function (key, group_type_name) {
            tbody+='<option value="'+group_type_name.group_type_id+'">'+group_type_name.group_type_name+'</option>';
            });
            
            $('#group-type-form select').html(tbody)
        }
    });
})();

现在对于表格,这个例子是为了展示 for each inside the for each on top 的工作原理。不同之处在于我使用的是完全相同的:

代码:

var tbody = "";
$.each(response.all_groups, function(key, group) {
    tbody += '<tr>' +
    '<td><p class="font-weight-bold mb-0">' + group.group_name + '</p>' + group.group_description + '</td>' +
    '<td>' + group.group_type_name + '</td>' +
    '<td>';
        $.each(response.all_groups, function(key, group) {
            tbody+='<p>yes</p>'
        });
    tbody += '</td>' +
    '<td>' + getgroupstatus(group.effective_start_datetime, group.effective_end_datetime) + '</td>' +
    '<td>' +
    '<button type="button" value="' + group.id + '" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button> ' +
    '<button type="button" value="' + group.group_id + '" title="delete ' + group.group_name + '" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>' +
    '</td>' +
    '</tr>';
});

这是for each 行:

$.each(response.all_groups, function(key, group) {
  tbody+='<p>yes</p>'
});

下面的截图显示它可以工作:

现在这是我的问题,通过为每个添加一个单独的,我当然假设我需要先声明该函数,然后再为每个使用一个,我的问题是它返回空白。我不确定我是否将代码定位错误,或者我需要添加其他内容,因为正如我的第一个示例所述,response.all_group_type_names 有数据并且可以在我的 UI 的另一部分工作。

var tbody = "";
$.each(response.all_groups, function(key, group) {
    tbody += '<tr>' +
    '<td><p class="font-weight-bold mb-0">' + group.group_name + '</p>' + group.group_description + '</td>' +
    '<td>' + group.group_type_name + '</td>' +
    '<td>';
        (function() {
            $.ajax({
                type: "GET",
                url: "/clinical/bbr-group-configuration-group-list-type-name",
                dataType: "json",
                success: function (response){
                    $.each(response.all_group_type_names, function (key, group_type_name) {
                    tbody+='<p>'+group_type_name.group_type_name+'</p>'
                    console.log(group_type_name.group_type_name);
                    });                                    
                }
            });
        })();
    tbody += '</td>' +
    '<td>' + getgroupstatus(group.effective_start_datetime, group.effective_end_datetime) + '</td>' +
    '<td>' +
    '<button type="button" value="' + group.id + '" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button> ' +
    '<button type="button" value="' + group.group_id + '" title="delete ' + group.group_name + '" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>' +
    '</td>' +
    '</tr>';
});

以下输出:(空白)

  • 请注意,我在for each 中添加了console.log,以表明数据仍然存在,但UI 中没有输出。

提前感谢您的帮助。

更新:

为避免混淆,我将在下面展示完整的功能。如您所见,我有两个ajax GET `for each:

fetchgroup();
function fetchgroup() {
    var current_category_id = $('#current_category_id').val();
    var current_status = $('#current_status').val();
    $.ajax({
        type: "GET",
        url: "/clinical/bbr-group-configuration-group-list/"+current_category_id+"/"+current_status,
        dataType: "json",
        success: function (response){
            var tbody = "";
            $.each(response.all_groups, function(key, group) {
              tbody += '<tr>' +
                '<td><p class="font-weight-bold mb-0">' + group.group_name + '</p>' + group.group_description + '</td>' +
                '<td>' + group.group_type_name + '</td>' +
                '<td>';
                    (function() {
                        $.ajax({
                            type: "GET",
                            url: "/clinical/bbr-group-configuration-group-list-type-name",
                            dataType: "json",
                            success: function (response){
                                $.each(response.all_group_type_names, function (key, group_type_name) {
                                tbody+='<p>'+group_type_name.group_type_name+'</p>'
                                });                                    
                            }
                        });
                    })();
                tbody += '</td>' +
                '<td>' + getgroupstatus(group.effective_start_datetime, group.effective_end_datetime) + '</td>' +
                '<td>' +
                '<button type="button" value="' + group.id + '" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button> ' +
                '<button type="button" value="' + group.group_id + '" title="delete ' + group.group_name + '" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>' +
                '</td>' +
                '</tr>';
            });
            
            $('#main-group-list tbody').html(tbody)
        }
    });
}

更新:

这是我目前得到的:

fetchgroup();
function fetchgroup() {
    var current_category_id = $('#current_category_id').val();
    var current_status = $('#current_status').val();
    $.ajax({
        type: "GET",
        url: "/clinical/bbr-group-configuration-group-list/"+current_category_id+"/"+current_status,
        dataType: "json",
        success: function (response){
            var tbody = "";
            $.each(response.all_groups, function(key, group) {
                tbody += '<tr>' +
                '<td><p class="font-weight-bold mb-0">' + group.group_name + '</p>' + group.group_description + '</td>' +
                '<td>' + group.group_type_name + '</td>' +
                '<td>';
                    (function() {
                        $.ajax({
                            type: "GET",
                            url: "/clinical/bbr-group-configuration-group-user-list",
                            dataType: "json",
                            success: function (response){
                                $.each(response.all_group_users, function (key, group_type_user) {
                                tbody+='<p>'+group_type_user.name+'</p>'
                                console.log(group_type_user.name);
                                });                                    
                            }
                        });
                    })();
                tbody += '</td>' +
                '<td>' + getgroupstatus(group.effective_start_datetime, group.effective_end_datetime) + '</td>' +
                '<td>' +
                '<button type="button" value="' + group.id + '" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button> ' +
                '<button type="button" value="' + group.group_id + '" title="delete ' + group.group_name + '" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>' +
                '</td>' +
                '</tr>';
            });
            
            $('#main-group-list tbody').html(tbody)
        }
    });
}

如您所见,它应该显示在 UI 中,但仅显示在 console.log 中。忽略 null,它只表示列表将包含 3 行和 2 个空格。

tbody+='<p>'+group_type_user.name+'</p>'
console.log(group_type_user.name);

是我尝试在 UI 中输出数据的内容,console.log 是屏幕截图中的工作内容。

【问题讨论】:

  • 在将$.each() 嵌套在另一个$.each() 中时,最好为每个keyvalue 使用唯一的名称。

标签: jquery ajax function foreach get


【解决方案1】:

根据 Rory McCrossan 的回答,它可以工作吗?是一个 $.when 有两个调用,然后是 foreachs 和 asnwers。

  $.when($.get("/clinical/bbr-group-configuration-group-list/"+current_category_id+"/"+current_status, "json"), $.get("/clinical/bbr-group-configuration-group-user-list", "json")).then(function (resp1, resp2) {
       var tbody = "";
        $.each(resp1.all_groups, function(key, group) {
            tbody += '<tr>' +
            '<td><p class="font-weight-bold mb-0">' + group.group_name + '</p>' + group.group_description + '</td>' +
            '<td>' + group.group_type_name + '</td>' +
            '<td>';
              
              $.each(resp2.all_group_users, function (key, group_type_user) {
                            tbody+='<p>'+group_type_user.name+'</p>'
                  });       
            tbody += '</td>' +
            '<td>' + getgroupstatus(group.effective_start_datetime, group.effective_end_datetime) + '</td>' +
            '<td>' +
            '<button type="button" value="' + group.id + '" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button> ' +
            '<button type="button" value="' + group.group_id + '" title="delete ' + group.group_name + '" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>' +
            '</td>' +
            '</tr>';

   });

 $('#main-group-list tbody').html(tbody)

【讨论】:

    【解决方案2】:

    这里有两个问题。首先,AJAX 请求为循环的每次迭代返回相同的内容,因此将其放入循环中是多余的。可以发送单个请求,然后您可以从那里构建一次 HTML 并将其附加到循环中。

    其次,AJAX 请求是异步的,因此您需要执行调用,根据结果构建 HTML,然后在所有逻辑完成后将其附加到 DOM。

    鉴于上述情况,请尝试以下示例:

    $.ajax({
      type: "GET",
      url: "/clinical/bbr-group-configuration-group-list-type-name",
      dataType: "json",
      success: function(response) {
        let groupNames = response.all_group_type_names.map(groupType => '<p>' + groupType.group_type_name + '</p>').join('');
        let rowHtml = response.all_groups.map(group => '<tr>' +
          '<td><p class="font-weight-bold mb-0">' + group.group_name + '</p>' + group.group_description + '</td>' +
          '<td>' + group.group_type_name + '</td>' +
          '<td>' + groupNames + '</td>' +
          '<td>' + getgroupstatus(group.effective_start_datetime, group.effective_end_datetime) + '</td>' +
          '<td>' +
            '<button type="button" value="' + group.id + '" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button> ' +
            '<button type="button" value="' + group.group_id + '" title="delete ' + group.group_name + '" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>' +
          '</td>' +
        '</tr>').join('');
    
        $('#group-type-form select').html(rowHtml)
      });
    });
    

    【讨论】:

    • 我想我需要澄清一下:首先,我不需要获取此标签:$('#group-type-form select').html(rowHtml) 我只想要&lt;td&gt; 中列中的名称列表
    • 我也更新了这个问题,实际上我每个人都有一个GET,第一个也是
    • 如果您有两个 get 请求,那么您需要发送两个请求并在两个请求都响应后执行上述逻辑:stackoverflow.com/q/4368946/519413
    • 我将使用您的示例进行测试,但我仍然难以理解,因为我已经在 success: function (response){ 之前收到了第一个 ajax 请求,如我更新的示例所示
    • 另外,我不太确定您给定的示例如何将我的数据循环到我的 UI 表中
    猜你喜欢
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多