gpt4 book ai didi

javascript - 动态创建表时的 JQuery Mobile 表回流问题

转载 作者:行者123 更新时间:2023-11-30 12:21:12 24 4
gpt4 key购买 nike

我是 Jquery Mobile 的新手,目前我在“使用回流的表”中使用 Jquery 动态加载数据时遇到问题。问题是即使我调用 $(table).refresh() 也只显示一个标题栏。首先,我使用 Jquery Mobile 1.3.1,然后更改为 JQuery Mobile 1.4.5,但问题没有解决。最初,我在 html 中添加表 thead 只是为了检查这是否是问题所在,然后我将其放入/加载到 javascript 下面是 HTML 代码::

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" href="css/custom.css"/>
<link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css"/>

<script src="js/jquery-2.0.3.min.js"></script>
<script src="js/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<!-- Start of first page -->
<div data-role="page" id="login-form">
<div data-role="header">
<h1>Header1</h1>
</div>
<div id="div-content" name="div-content" role="main" class="ui-content">
<form id="div-content-form" name="div-content-form" action="index.html">
<div data-role="fieldcontain">
<label for="applicantNos">Applicant Nos:</label>
<input type="text" id="applicantNos" name="applicantNos" required="required" />
</div>
<div data-role="fieldcontain">
<label for="idNos">ID Nos:</label>
<input type="text" id="idNos" name="idNos" required="required" />
</div>
<input type="button" value="Submit" class="button" />
</form>
</div>
<div data-role="footer"></div>
</div>
<!-- Start of second page -->
<div data-role="page" id="application-status" data-add-back-btn="true">
<div data-role="header">
<h4>Application Status</h4>
</div>
<div id="application-status-content" name="application-status-content" role="main" class="ui-content">
<table data-role="table" id="app-status-table" data-mode="reflow" class="ui-responsive table-stroke">
<thead>
</thead>
<tbody></tbody>
</table>
</div>
<div data-role="footer"></div>
</div>

<script src="cordova.js"></script>
<script src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>

Javascript 代码:

var app = {
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {

$(document).ready(function () {
$('.button').click(function() {
event.preventDefault();
var applicantNos = $('#applicantNos').val().trim();
var idNos = $('#idNos').val().trim();
if (applicantNos != '' && idNos != '') {
$.ajax({
url: "http://localhost:8083/fetchDetails",
type: "POST",
data: {
applicantNos : applicantNos,
idNos: idNos
}
})
.done(function(responseData){
$.mobile.changePage("#application-status", {
respData: responseData,
transition: "flip"
});
})
.fail(function(msg) {
alert(msg.responseText);
});
}
});

$(document).on("pagebeforechange", function (e, data) {
if (data.toPage[0].id == "application-status") {
showAppStatusData(data.options.respData);
}
});

function showAppStatusData(data) {
var pageData = data;
var tHead = $('table#app-status-table thead');

tHead.append($("<tr><th data-priority='1'>Application No.</th>"));
tHead.append($("<th data-priority='2'>Column 2</th>"));
tHead.append($("<th data-priority='3'>Column 3</th>"));
tHead.append($("<th data-priority='4'>Column 4</th>"));
tHead.append($("<th data-priority='5'>Column 5</th>"));
tHead.append($("<th data-priority='6'>Column 6</th>"));
tHead.append($("<th data-priority='7'>Column 7</th>"));
tHead.append($("<th data-priority='8'>Column 8</th>"));
tHead.append($("<th data-priority='9'>Column 9</th></tr>"));

var tBody = $('#app-status-table tbody');
tBody.append($("<tr><td>" + data.applicationNos + "</td>"));
tBody.append($("<td>" + data.column2 + "</td>"));
tBody.append($("<td>" + data.column3 + "</td>"));
tBody.append($("<td>" + data.column4 + "</td>"));
tBody.append($("<td>" + data.column5 + "</td>"));
tBody.append($("<td>" + data.column6 + "</td>"));
tBody.append($("<td>" + data.column7 + "</td>"));
tBody.append($("<td>" + data.column8 + "</td>"));
tBody.append($("<td>" + data.column9 + "</td></tr>"));

// $("#app-status-table").append(tHead).append(tBody);

$("#app-status-table").table("refresh");
//tHead.closest("table#app-status-table").table("refresh").trigger("create");
//tBody.closest("table#app-status-table").table("refresh").trigger("create");
}
});
}

};

附上只显示第一列标题的波纹模拟器快照,正文显示完美。即使我在 HTML 代码中添加了表格“thead”,但这也不起作用! enter image description here

此问题是否与此链接相关: https://github.com/jquery/jquery-mobile/pull/6007/files

请提供解决方案,因为我花了整整 1 天时间解决这个问题。谢谢

最佳答案

问题是您正在尝试附加 jQuery 对象:

tHead.append($("<tr><th data-priority='1'>Application No.</th>"));

所以 jQuery 会自动关闭 <TR>第一个标题列上的标记。

而是将整行构建为一个字符串,然后在末尾附加一次:

function showAppStatusData(data) {
var pageData = data;
var tHead = $('table#app-status-table thead');

var headRow = "<tr><th data-priority='1'>Application No</th>";
headRow += "<th data-priority='2'>Column 2</th>";
headRow += "<th data-priority='3'>Column 3</th>";
headRow += "<th data-priority='4'>Column 4</th>";
headRow += "<th data-priority='5'>Column 5</th>";
headRow += "<th data-priority='6'>Column 6</th>";
headRow += "<th data-priority='7'>Column 7</th>";
headRow += "<th data-priority='8'>Column 8</th>";
headRow += "<th data-priority='9'>Column 9</th></tr>";

tHead.append(headRow);

var tBody = $('#app-status-table tbody');
var theRow = "<tr><td>" + data.applicationNos + "</td>";
theRow += "<td>" + data.column2 + "</td>";
theRow += "<td>" + data.column2 + "</td>";
theRow += "<td>" + data.column2 + "</td>";
theRow += "<td>" + data.column2 + "</td>";
theRow += "<td>" + data.column2 + "</td>";
theRow += "<td>" + data.column2 + "</td>";
theRow += "<td>" + data.column2 + "</td>";
theRow += "<td>" + data.column2 + "</td></tr>";

tBody.append(theRow);

$("#app-status-table").table("refresh");
}

DEMO

关于javascript - 动态创建表时的 JQuery Mobile 表回流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31002847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com