简体   繁体   English

如何在表格中按顺序输入数字?

[英]How do I enter a numeric number in sequence in a table?

There is a table that is sorted by clicking from larger to smaller values.有一个表格,通过单击从大到小的值进行排序。 How to make sure that the ordinal number of the value is always between 1 and 5 after sorting the table?排序后如何确保值的序数始终在 1 到 5 之间?

That is, the numerical order should always be between 1 and 5 and should not be sorted.也就是说,数字顺序应始终在 1 和 5 之间,不应排序。

 $(document).ready(function() { var $table = $('#simpleTable').stupidtable(); $table.find('thead th[data-sort]').on('click', function() { $(this).eq(0).stupidsort(); }); });
 <table id="simpleTable"> <thead> <tr> <th data-sort="int">#</th> <th data-sort="int">int</th> <th data-sort="float">float</th> <th data-sort="string">string</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>15</td> <td>18</td> <td>banana</td> </tr> <tr> <td>2</td> <td>95</td> <td>36</td> <td>coke</td> </tr> <tr> <td>3</td> <td>2</td> <td>152.5</td> <td>apple</td> </tr> <tr> <td>4</td> <td>53</td> <td>88.5</td> <td>zebra</td> </tr> <tr> <td>5</td> <td>195</td> <td>858</td> <td>orange</td> </tr> </tbody> </table> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/stupidtable/1.1.3/stupidtable.min.js"></script>

Never heard of - and therefore never used - stupidtable but it doesn't look like there is a call-back event once the sorting has completed.从未听说过 - 因此从未使用过 - stupidtable的,但一旦排序完成,它看起来不像有回调事件。

So instead, I've used a timeout as part of the click event.因此,我使用超时作为click事件的一部分。

The within the timeout finds all the first-child <td> elements, and uses the index of each to set the text of the element.超时内查找所有first-child <td>元素,并使用each元素的索引来设置元素的文本。

As the stupidsort code does the sort 10-milliseconds after the client event, I've had to make it 20 in the code for it to work...由于愚蠢的排序stupidsort在客户端事件后 10 毫秒进行排序,因此我必须在代码中将其设为 20 才能使其工作......

 $(function() { var $table = $('#simpleTable').stupidtable(); $table.find('thead th[data-sort]').on('click', function() { $(this).eq(0).stupidsort(); // After a short period, set the values setTimeout(function() { $table.find("tbody tr td:first-child").each(function(i, v) { $(this).text((i + 1).toString()); }); }, 20); }); });
 <table id="simpleTable"> <thead> <tr> <th data-sort="int">#</th> <th data-sort="int">int</th> <th data-sort="float">float</th> <th data-sort="string">string</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>15</td> <td>18</td> <td>banana</td> </tr> <tr> <td>2</td> <td>95</td> <td>36</td> <td>coke</td> </tr> <tr> <td>3</td> <td>2</td> <td>152.5</td> <td>apple</td> </tr> <tr> <td>4</td> <td>53</td> <td>88.5</td> <td>zebra</td> </tr> <tr> <td>5</td> <td>195</td> <td>858</td> <td>orange</td> </tr> </tbody> </table> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/stupidtable/1.1.3/stupidtable.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM