简体   繁体   English

使用从ajax响应创建的变量填充表

[英]Populate table with variable created from ajax response

I have a table that is populated with data that I then need to use to run a second query. 我有一个填充有数据的表,然后需要使用该数据来运行第二个查询。 I am using jquery datatables and those are the functions that you see that allow me to grab all the content from the table. 我正在使用jquery数据表,这些是您看到的函数,它们使我能够从表中获取所有内容。 So for example I am grabbing an integer out of the fourth column like this and then using that number to run a query for a json response : 因此,例如,我从这样的第四列中获取一个整数,然后使用该数字来运行json响应查询:

var cells = [];
var rows = oTable.fnGetNodes();
for( var i=0;i<rows.length;i++)
        {
            var grabsku = $j(rows[i]).find('td:eq(3)').text();
            grabsku = grabsku.substring(0, 6) + "-0" + grabsku.substring(7, grabsku.length - 4);
            s7url = 'http://jsonquery.com/' + grabsku  + '?req=exists,json';        

      $j.ajax({
        url: s7url,
        dataType: 'jsonp'
        });
       }

THen with that ajax request the json returns either a 1 or 0 which I can then form the correct info depending on what is returned. 通过该ajax请求,json返回1或0,然后我可以根据返回的内容形成正确的信息。

function s7jsonResponse(response)
        {
        var s7img = sku;
        s7img = '<img src="http://imageserver.com/is/image/' + s7img.substring(0, 6) + "-0" + s7img.substring(7, s7img.length - 4) + '">';
         x = response["catalogRecord.exists"];  
             z = x == "0" ? "NO IMG" : s7img;
             console.log(z);
        }

Console now shows me what I want for z variable, either an image path or NO IMG. 控制台现在显示了我想要的z变量(图像路径或NO IMG)。 My problem is getting this out of console and actually back into the table. 我的问题是将其从控制台中移出并实际上又回到表中。 What I want to happen is as each response comes in to replace a td in the table with the z variable. 我想发生的是,随着每个响应的出现,用z变量替换表中的td。 I tried to put this in the jsonResponse but it did not work correctly. 我试图将其放在jsonResponse中,但无法正常工作。 How could I iterate through the table, in this case (td:eq(2)) and replace each cell with the variable z I get back from this query? 在这种情况下(td:eq(2)) ,如何遍历表并将每个单元格替换为从该查询返回的变量z

Need to create a function to do the ajax that you can pass the row index and the url that contains data query string to as arguments. 需要创建一个函数来执行ajax,您可以将行索引和包含数据查询字符串的URL作为参数传递给ajax。 Since you know the cell index , once data returned from ajax you can call fnUpdate for using new data and row index within success of the ajax 因为您知道单元fnUpdate引,所以一旦从ajax返回数据,您就可以调用fnUpdate在ajax成功的情况下使用新数据和行索引

var cells = [];
var rows = oTable.fnGetNodes();
for (var i = 0; i < rows.length; i++) {
    var grabsku = $j(rows[i]).find('td:eq(3)').text();
    grabsku = grabsku.substring(0, 6) + "-0" + grabsku.substring(7, grabsku.length - 4);
    s7url = 'http://jsonquery.com/' + grabsku + '?req=exists,json';
    var rowIndex = i;
    doAjax(s7url, rowIndex)
}



function doAjax(url, rowIndex) {
    $j.ajax({
        url: s7url,
        dataType: 'jsonp',
        success: function(repsonse) {

            var s7img = sku;
            s7img = '<img src="http://imageserver.com/is/image/' + s7img.substring(0, 6) + "-0" + s7img.substring(7, s7img.length - 4) + '">';
            x = response["catalogRecord.exists"];
            z = x == "0" ? "NO IMG" : s7img;
            console.log(z);
        /* do fnUpdate here using new data and rowIndex and you know the cell index already*/
        }



    });
}

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

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