开发者

Adding Integer Value dynamically to table

If I have to add an integer value, like newFeature.OrderTotal has an integer value in it.

How can I show it dynamically in a table that I am creating dynamically?

Any suggestion will be appreciate.

txt += "<td>"+newFeature.OrderTotal+"</td>"; 

How can this be done?

 function dataSelectHandler(transaction, results){

  // Handle the results
  for (var i=0; i<results.rows.length; i++) {

    var row = results.rows.item(i);

    var newFeature = new Object();

        newFeature.OrderTotal   = row['OrderTotal'];//OrderTotal is coming from `(//"SELECT SUM(amount1) AS OrderTotal FROM budget1 WHERE month='"+hello+"' ";)`
    var q1 = newFeature.OrderTotal;//q1 has an integer value suppose 46
    alert(q1);//here alert box is showing properly value of q1
    }
}

var txt = "";
var myTable = "";
txt += "<table id='myTable' border='5'>";
txt += "<tr>";
txt += "<th>Month</th>";
txt += "<th>Amount</th>";
txt += "</tr>";
txt += "<tr>";
txt += "<td>"+$('#mchoose').val()+"</td>";
txt += "<td>"+q1+"</td>";//m not gett开发者_开发百科ing any values in this q1 in the table row
txt += "</tr>";
txt += "</table>";

$('#ss').html(txt);


You need to create a table with rows and data containing your elements, here's an example:

function addTable(parentElementId) {
  var data = [1, 2, 3, 4];
  // Create the table with a row and cell for each data element.
  var t = document.createElement("table");
  t.id = "myTable";
  for (var i=0; i<data.length; i++) {
    var tr = t.appendChild(document.createElement("tr"))
      , td = tr.appendChild(document.createElement("td"));
    td.innerHTML = data[i];
  }
  // Add the table to the given element.
  var p = document.getElementById(parentElementId);
  p.appendChild(t);
}


If this idea suitable to your problem, then you can get the code:

(1) Add the table in the #ss html directly statically or just like your own way from jquery $("#ss").html(tableText). note the right tag hierarchy is table > tbody > tr > td, Since tr and td is dynamic. so only add table and tbody.

(2) In your dataSelectHandler() for loop, build your string in format "<tr><td>"+newFeature.OrderTotal+"</td></tr>" and append to tbody like this $("#myTable > tbody").html(rowText); to replace the alert();.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜