Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery表绑定中获取更多数据时,如何将旧数据保存在表体?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 在jQuery表绑定中获取更多数据时,如何将旧数据保存在表体?

Javascript 在jQuery表绑定中获取更多数据时,如何将旧数据保存在表体?,javascript,jquery,html,Javascript,Jquery,Html,这是我的HTML表格 <table id="tblscroll"> <thead> <tr> <th>Account Code</th> <th>Description</th> <th>Total Amount</th> <th>Amt1</th> <th>Amt2</th&

这是我的HTML表格

<table id="tblscroll">
  <thead>
    <tr>
      <th>Account Code</th>
      <th>Description</th>
      <th>Total Amount</th>
      <th>Amt1</th>
      <th>Amt2</th>
      <th>Amt3</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>
<img id="loader" src="Image/myLoading.gif" style="display: none" />

帐户代码
描述
总金额
Amt1
Amt2
Amt3
这是我的加载数据。首先,我加载一点数据。滚动后,它会得到一些要绑定的数据,但我以前的数据会被新数据替换

$(document).ready(function() {
  GetRecords();
});

$(window).scroll(function() {
  if ($(window).scrollTop() == $(document).height() - $(window).height()) {
    pageIndex++;
    if (pageIndex >= 2) {
      $("#loader").show();
      GetRecords();
    }

  }
});

comcod = <%=this.GetCompCode()%>;

function GetRecords() {
  $("#loader").show();
  $.ajax({
    type: "POST",
    url: "TestScrollDatat.aspx/Getdata",
    data: '{comcod: ' + comcod + ',pageindex:' + pageIndex + '}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function(response) {
      alert(response.d);
    },
    error: function(response) {
      alert(response.d);
    }
  });
  //}
}

function OnSuccess(response) {
  //alert("Enter");
  var customer = JSON.parse(response.d);
  var row;
  $.each(customer,
    function(i, item) {

      row += "<tr>";
      row += "<td>" + item.actcode + "</td>";
      row += "<td>" + item.actdesc + "</td>";
      row += "<td>" + item.totamt + "</td>";
      row += "<td>" + item.amt01 + "</td>";
      row += "<td>" + item.amt02 + "</td>";
      row += "<td>" + item.amt03 + "</td>";
      row += "</tr>";
      $("#tblscroll tbody").html(row);
    });


  $("#loader").hide();
}
$(文档).ready(函数(){
GetRecords();
});
$(窗口)。滚动(函数(){
if($(窗口).scrollTop()==$(文档).height()-$(窗口).height()){
pageIndex++;
如果(页面索引>=2){
$(“#加载程序”).show();
GetRecords();
}
}
});
comcod=;
函数GetRecords(){
$(“#加载程序”).show();
$.ajax({
类型:“POST”,
url:“TestScrollDatat.aspx/Getdata”,
数据:{comcod:'+comcod+',pageindex:'+pageindex+'}',
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:一旦成功,
故障:功能(响应){
警报(response.d);
},
错误:函数(响应){
警报(response.d);
}
});
//}
}
函数OnSuccess(响应){
//警报(“输入”);
var customer=JSON.parse(response.d);
var行;
美元。每个(客户,
职能(一、项目){
行+=”;
行+=“”+item.actcode+“”;
行+=“”+item.actdesc+“”;
行+=“”+item.totam+“”;
行+=“”+item.amt01+“”;
行+=“”+item.amt02+“”;
行+=“”+item.amt03+“”;
行+=”;
$(“#tblscroll tbody”).html(行);
});
$(“#加载程序”).hide();
}
我想保留旧数据并用新数据绑定。

代码>行+=”
var行之后导致
行===“未定义”
。不要附加到未初始化的变量。使用
行=”用于第一行。这也避免了在更新表后追加到同一变量

$(document).ready(function() {
  GetRecords();
});

$(window).scroll(function() {
  if ($(window).scrollTop() == $(document).height() - $(window).height()) {
    pageIndex++;
    if (pageIndex >= 2) {
      $("#loader").show();
      GetRecords();
    }

  }
});

comcod = <%=this.GetCompCode()%>;

function GetRecords() {
  $("#loader").show();
  $.ajax({
    type: "POST",
    url: "TestScrollDatat.aspx/Getdata",
    data: '{comcod: ' + comcod + ',pageindex:' + pageIndex + '}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function(response) {
      alert(response.d);
    },
    error: function(response) {
      alert(response.d);
    }
  });
  //}
}

function OnSuccess(response) {
  //alert("Enter");
  var customer = JSON.parse(response.d);
  var row;
  $.each(customer,
    function(i, item) {

      row += "<tr>";
      row += "<td>" + item.actcode + "</td>";
      row += "<td>" + item.actdesc + "</td>";
      row += "<td>" + item.totamt + "</td>";
      row += "<td>" + item.amt01 + "</td>";
      row += "<td>" + item.amt02 + "</td>";
      row += "<td>" + item.amt03 + "</td>";
      row += "</tr>";
      $("#tblscroll tbody").html(row);
    });


  $("#loader").hide();
}
下一步,使用而不是

您还可以缩短函数,不需要某些变量:

function OnSuccess(response) {
  //alert("Enter");
  const customer = JSON.parse(response.d),
    columns = [
      "actcode",
      "actdesc",
      "totamt",
      "amt01",
      "amt02",
      "amt03"
    ];

  $.each(customer, function(i, item) {
    console.log("<tr>" + $.map(columns, (prop) => "<td>" + item[prop] + "</td>").join("") + "</tr>");
  });
  $("#loader").hide();
}
函数成功(响应){
//警报(“输入”);
const customer=JSON.parse(response.d),
列=[
“行动守则”,
“actdesc”,
“总金额”,
“amt01”,
“amt02”,
“amt03”
];
$。每个(客户、职能(i、项目){
console.log(“+$.map(columns,(prop)=>”+项[prop]+”).join(“+”);
});
$(“#加载程序”).hide();
}

使用
append
而不是
html