我想列出从数据库表到HTML页面的所有数据。当前共有3行数据,但仅列出2行数据。
HTML。
<script src="lib/jquery-1.11.2.min.js"></script>
<script src="lib/jquery.mobile-1.4.5.min.js"></script>
<div class="expandable-content">
<div id="SearchResult" class="ui-content">
<table data-role="table" data-mode="reflow" class="uiresponsive" id="CoTable">
<thead>
<tr>
<th>User</th>
<th>Personnel</th>
<th>License</th>
</tr>
</thead>
<tbody id="mybody"></tbody>
</table>
</div>
</div>
JS。
function searchperson() {
var xmlhttp = new XMLHttpRequest();
var url = serverURL() + "/searchfriends.php";
url += "?userid=" + localStorage.getItem("userid") + "&search=" +
$("#search").val();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
getSearchResults(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function getSearchResults(response) {
var arr = JSON.parse(response);
var i;
$("#mybody").find("tr").remove();
for (i = 0; i < arr.length; i++) {
$("#mybody").append("<tr><td>" + arr[i].userid +
"</td><td>"
+ arr[i].personnel + "</td><td>"
+ arr[i].license + "</td></tr>");
}
$('#Results').bind('pageinit', function () {
$('#CoTable').table('refresh');
//$("#CoTable").table('refresh');
}
我希望列出所有数据,但只列出2行数据。 [未捕获的错误:无法在初始化之前调用表上的方法;尝试调用方法“刷新”。]我使用了jquery移动页面ID,它摆脱了错误,但仍然缺少数据。请帮助,非常感谢。
对于JQM表小部件,该方法称为[rebuild
(参考:https://api.jquerymobile.com/table/)。
由于http请求的异步行为,您可能不知道何时会收到响应,所以恕我直言,处理该响应的最简单方法是检查表小部件是否已被实例化,然后调用所需的方法:
function isInstanceOf(id, widget) {
var el = document.getElementById(id);
return !!$.data(el, 'mobile-' + widget);
}
function addRow(id, evt) {
var html = '',
$table = $('#' + id),
isInstance = isInstanceOf(id, 'table'),
tableMethod = isInstance ? 'rebuild' : null;
html += '<tr>';
html += '<td>' + id + '</td>';
html += '<td>' + evt + '</td>';
html += '<td>' + isInstance + '</td>';
html += '<td>' + (isInstance ? tableMethod : '[create]') + '</td>';
html += '</tr>';
$table.find('tbody').append(html);
$table.table(tableMethod);
}
$(document).ready(function() {
addRow("CoTable", "DomReady");
});
$(document).on("pagecontainercreate", function(e, ui) {
addRow("CoTable", e.type);
} );
$(document).on("tablecreate", function(e, ui) {
addRow(e.target.id, e.type);
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css" />
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header" data-theme="b">
<h1>jQuery Mobile Table</h1>
</div>
<div data-role="content">
<table data-role="table" data-mode="reflow" id="CoTable" class="ui-responsive">
<thead>
<tr>
<th data-priority="1">Id</th>
<th data-priority="persist">Source</th>
<th data-priority="2">Instanced</th>
<th data-priority="3">Method</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</body>
</html>
无需担心event
序列。
这里是一个小矮人:https://plnkr.co/edit/S8m8BPYnsfUFklYLwEz7?p=preview