将 jQuery table 排序器与 javascript table 数组一起使用
Using jQuery tablesorter with javascript table array
我正在尝试在 javascript table 生成的 jQuery 插件 tablesorter (tablesorter.com) 上使用=] 数组当我的页面加载时。 table 获得样式,table 在单击列 headers 时不会排序。
这是我目前的情况:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/lib/jquery-ui/jquery-ui-1.11.4/external/jquery/jquery.js"></script>
<script type="text/javascript" src="/lib/jquery-ui/jquery-ui-1.11.4/jquery-ui.js"></script>
<link type="text/css" rel="stylesheet" href="/lib/jquery-ui/jquery-ui-1.11.4/jquery-ui.css">
<script type="text/javascript" src="/lib/jquery/tablesorter/jquery.tablesorter.js"></script>
<link type="text/css" rel="stylesheet" href="/lib/jquery/tablesorter/themes/blue/style.css">
<style>
</style>
<script>
$( document ).ready( function() {
$( "table" ).tablesorter();
$( "p" ).click( function() {
$( this ).hide();
});
});
$( function() {
$( "#datepicker" ).datepicker();
});
</script>
<script>
var frameData = [
["Phoenix Smasher", 15],
["Bone Breaker", 16],
["DeathFist", 60],
["Thruster", 20],
["S Jab", 10]
];
function pageLoad() {
var t = "";
t += "<thead>";
t += "<tr>";
t += "<th>Move</th>";
t += "<th>Start Up</th>";
t += "</tr>";
t += "</thead>";
t += "<tbody>";
for (var i = 0; i < frameData.length; i++) {
t += "<tr>";
t += "<td>" + frameData[i][0] + "</td><td>" + frameData[i][1] + "</td>";
t += "</tr>";
}
t += "</tbody>";
document.getElementById("frameTable").innerHTML = t;
}
</script>
</head>
<body onload="pageLoad()">
<p>Click the table headers to sort the array in descending order.</p>
<br />
<br />
<div id="demo"></div>
<table id="frameTable" class="tablesorter">
</table>
<p>jQuery test. This text will disappear on click.</p>
<input type="text" id="datepicker">
</body>
</html>
我的尝试: 奇怪的是,当我去掉 javascript 数组并放置实际的 html table 数据时在 <table>
和 </table>
标签之间,tablesorter 插件工作正常。另外,我用 jQuery 代码尝试了 re-arranging 数组和 pageLoad()
函数,但一点运气都没有。
知道如何让它工作吗?
该页面在我的服务器上:http://sketchcarellz.com/multiArray.html
您的问题是您在构建 table 之前初始化插件。
切换执行顺序。要做到这一点,只需使用一个负载处理程序即可确保您知道顺序
$( document ).ready( function() {
pageload();
// table is build, call plugin
$( "table" ).tablesorter();
});
或者,您可以使用 Build Table Widget
从 JSON 创建一个 table
确保包含构建 table 小部件:
<script src="<a href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/js/widgets/widget-build-table.min.js" rel="nofollow noreferrer">https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/js/widgets/widget-build-table.min.js</a>";></script>
如果您有值数组,可以将 header 添加到第一行,如下所示:
var table数据=[
<b>// header
[ "Move", "Start Up"],</b>
// 行
[ "Phoenix Smasher", 15],
[ "Bone Breaker", 16],
[ "DeathFist", 60],
[ "Thruster", 20],
[ "S Jab", 10]
]
然后将数组和一些参数传递给 widgetOptions
中的构建源选项,如下所示:
$("#jsonTable").tablesorter({
theme: 'materialize',
widgets: ['zebra'],
widgetOptions: {
build_type: 'array', // can sometimes be detected if undefined
build_source: tableData,
build_headers: {
rows : 1, // Number of header rows from the array
widths : [ '70%', '20%' ] // set header cell widths
},
build_footers : {
rows : 0, // Number of header rows from the array
},
}
});
jsFiddle 和 StackSnippets 中的演示:
var tableData = [
// header
[ "Move" , "Start Up"],
// rows
[ "Phoenix Smasher", 15],
[ "Bone Breaker", 16],
[ "DeathFist", 60],
[ "Thruster", 20],
[ "S Jab", 10]
]
$("#jsonTable").tablesorter({
theme: 'materialize',
widgets: ['zebra'],
widgetOptions: {
build_type: 'array', // can sometimes be detected if undefined
build_source: tableData,
build_headers: {
rows : 1, // Number of header rows from the array
widths : [ '70%', '20%' ] // set header cell widths
},
build_footers : {
rows : 0, // Number of header rows from the array
},
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/css/theme.default.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/css/theme.materialize.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/js/jquery.tablesorter.combined.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/js/widgets/widget-build-table.min.js"></script>
<div id="jsonTable"></div>
进一步阅读:
我正在尝试在 javascript table 生成的 jQuery 插件 tablesorter (tablesorter.com) 上使用=] 数组当我的页面加载时。 table 获得样式,table 在单击列 headers 时不会排序。
这是我目前的情况:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/lib/jquery-ui/jquery-ui-1.11.4/external/jquery/jquery.js"></script>
<script type="text/javascript" src="/lib/jquery-ui/jquery-ui-1.11.4/jquery-ui.js"></script>
<link type="text/css" rel="stylesheet" href="/lib/jquery-ui/jquery-ui-1.11.4/jquery-ui.css">
<script type="text/javascript" src="/lib/jquery/tablesorter/jquery.tablesorter.js"></script>
<link type="text/css" rel="stylesheet" href="/lib/jquery/tablesorter/themes/blue/style.css">
<style>
</style>
<script>
$( document ).ready( function() {
$( "table" ).tablesorter();
$( "p" ).click( function() {
$( this ).hide();
});
});
$( function() {
$( "#datepicker" ).datepicker();
});
</script>
<script>
var frameData = [
["Phoenix Smasher", 15],
["Bone Breaker", 16],
["DeathFist", 60],
["Thruster", 20],
["S Jab", 10]
];
function pageLoad() {
var t = "";
t += "<thead>";
t += "<tr>";
t += "<th>Move</th>";
t += "<th>Start Up</th>";
t += "</tr>";
t += "</thead>";
t += "<tbody>";
for (var i = 0; i < frameData.length; i++) {
t += "<tr>";
t += "<td>" + frameData[i][0] + "</td><td>" + frameData[i][1] + "</td>";
t += "</tr>";
}
t += "</tbody>";
document.getElementById("frameTable").innerHTML = t;
}
</script>
</head>
<body onload="pageLoad()">
<p>Click the table headers to sort the array in descending order.</p>
<br />
<br />
<div id="demo"></div>
<table id="frameTable" class="tablesorter">
</table>
<p>jQuery test. This text will disappear on click.</p>
<input type="text" id="datepicker">
</body>
</html>
我的尝试: 奇怪的是,当我去掉 javascript 数组并放置实际的 html table 数据时在 <table>
和 </table>
标签之间,tablesorter 插件工作正常。另外,我用 jQuery 代码尝试了 re-arranging 数组和 pageLoad()
函数,但一点运气都没有。
知道如何让它工作吗?
该页面在我的服务器上:http://sketchcarellz.com/multiArray.html
您的问题是您在构建 table 之前初始化插件。
切换执行顺序。要做到这一点,只需使用一个负载处理程序即可确保您知道顺序
$( document ).ready( function() {
pageload();
// table is build, call plugin
$( "table" ).tablesorter();
});
或者,您可以使用 Build Table Widget
从 JSON 创建一个 table确保包含构建 table 小部件:
<script src="<a href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/js/widgets/widget-build-table.min.js" rel="nofollow noreferrer">https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/js/widgets/widget-build-table.min.js</a>";></script>
如果您有值数组,可以将 header 添加到第一行,如下所示:
var table数据=[
<b>// header
[ "Move", "Start Up"],</b>
// 行
[ "Phoenix Smasher", 15],
[ "Bone Breaker", 16],
[ "DeathFist", 60],
[ "Thruster", 20],
[ "S Jab", 10]
]
然后将数组和一些参数传递给 widgetOptions
中的构建源选项,如下所示:
$("#jsonTable").tablesorter({
theme: 'materialize',
widgets: ['zebra'],
widgetOptions: {
build_type: 'array', // can sometimes be detected if undefined
build_source: tableData,
build_headers: {
rows : 1, // Number of header rows from the array
widths : [ '70%', '20%' ] // set header cell widths
},
build_footers : {
rows : 0, // Number of header rows from the array
},
}
});
jsFiddle 和 StackSnippets 中的演示:
var tableData = [
// header
[ "Move" , "Start Up"],
// rows
[ "Phoenix Smasher", 15],
[ "Bone Breaker", 16],
[ "DeathFist", 60],
[ "Thruster", 20],
[ "S Jab", 10]
]
$("#jsonTable").tablesorter({
theme: 'materialize',
widgets: ['zebra'],
widgetOptions: {
build_type: 'array', // can sometimes be detected if undefined
build_source: tableData,
build_headers: {
rows : 1, // Number of header rows from the array
widths : [ '70%', '20%' ] // set header cell widths
},
build_footers : {
rows : 0, // Number of header rows from the array
},
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/css/theme.default.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/css/theme.materialize.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/js/jquery.tablesorter.combined.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.0/js/widgets/widget-build-table.min.js"></script>
<div id="jsonTable"></div>
进一步阅读: