gpt4 book ai didi

带有 number_format() 的 jQuery 表排序器;

转载 作者:行者123 更新时间:2023-12-01 00:10:51 25 4
gpt4 key购买 nike

我正在使用 jQuery tablesorter 插件,我想通过在千位上使用逗号分隔符来使我的表格更具可读性,即 number_format($value, 0, "", ",");

但是,这会扰乱排序。我一直在寻找解决方案(例如 http://lab.christianmontoya.com/jquery-tablesort-numbers-commas/ ),但我还没有设法调整它并适应我的 table 。我是脚本新手,所以请宽容。

我的 JavaScript block 应该是什么样子?

<head>
<script type="text/javascript">
$(document).ready(function() {
$("#myTable").tablesorter( {
sortList: [[1,1], [2,1]],
widgets: ['zebra'],
} );
});
</script>
</head>


<body>
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>Username</th>
<th>Level</th>
<th>Experience</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fiery</td>
<td><?php echo number_format(2400, 0, " ", ","); ?></td>
<td><?php echo number_format(378433689, 0, " ", ",");?></td>
</tr>
<tr>
<td>Sixten</td>
<td><?php echo number_format(1600, 0, " ", ","); ?></td>
<td><?php echo number_format(1000000000, 0, " ", ",");?></td>
</tr>
<tr>
<td>Penny</td>
<td><?php echo number_format(885, 0, " ", ","); ?></td>
<td><?php echo number_format(8900002, 0, " ", ","); ?></td>
</tr>
<tr>
<td>Petra</td>
<td><?php echo number_format(2400, 0, " ", ","); ?></td>
<td><?php echo number_format(398433889, 0, " ", ","); ?></td>
</tr>
<tr>
<td>Neu</td>
<td><?php echo number_format(4974, 0, " ", ","); ?></td>
<td><?php echo number_format(198433889, 0, " ", ","); ?></td>
</tr>
</tbody>
</table>
</body>

最佳答案

您需要将自定义格式应用于所需的标题/列:

标记:

<table id="myTable" class="tablesorter" border="0" cellpadding="0" cellspacing="1">
<thead>
<tr>
<th>
Price
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
1,000.00
</td>
</tr>
<tr>
<td>
100,100.00
</td>
</tr>
<tr>
<td>
100,980.00
</td>
</tr>
<tr>
<td>
100,122.00
</td>
</tr>
<tr>
<td>
120,122.00
</td>
</tr>
<tr>
<td>
540,122.00
</td>
</tr>
<tr>
<td>
222,122.00
</td>
</tr>
<tr>
<td>
100,020.00
</td>
</tr>
<tr>
<td>
100,251.00
</td>
</tr>
<tr>
<td>
100,364.00
</td>
</tr>
<tr>
<td>
300,122.00
</td>
</tr>
</tbody>
</table>

jQuery:

    $(document).ready(function () {
jQuery.tablesorter.addParser({
id: "fancyNumber",
is: function (s) {
return /^[0-9]?[0-9,\.]*$/.test(s);
},
format: function (s) {
return jQuery.tablesorter.formatFloat(s.replace(/,/g, ''));
},
type: "numeric"
});

$("#myTable").tablesorter({
headers: { 0: { sorter: 'fancyNumber'} },
widgets: ['zebra']
});
});

JSFiddle:http://jsfiddle.net/eAgDC/

关于带有 number_format() 的 jQuery 表排序器;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14965639/

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