在 jQuery DataTables 中使用锚标记对列进行排序
我使用 jQuery 数据表插件对表数据进行排序。如果列包含简单文本,排序效果很好。如果我在文本上放置任何锚标记条件,则列排序无法正确排序。
我以以下方式显示值:
<td><?php if ($allAptArr[$d][27]['staffinactive'] == 1) { ?>
<?=ucwords(stripslashes($allAptArr[$d][5]['staff_name']));?>
<?php } else { ?>
<a href='#' onClick="redirectToStaff('<?=$allAptArr[$d][10]['staff_id']?>');">
<?=ucwords(stripslashes($allAptArr[$d][5]['staff_name']));?>
</a>
<?php } ?> </td>
使用此代码,列排序失败。
I used the jQuery datatable plugin in sort the table data. The sorting works fine if a column contains simple text. If I put any anchor tag condition on a text then the column sorting does not sort properly.
I displayed the values in following manner:
<td><?php if ($allAptArr[$d][27]['staffinactive'] == 1) { ?>
<?=ucwords(stripslashes($allAptArr[$d][5]['staff_name']));?>
<?php } else { ?>
<a href='#' onClick="redirectToStaff('<?=$allAptArr[$d][10]['staff_id']?>');">
<?=ucwords(stripslashes($allAptArr[$d][5]['staff_name']));?>
</a>
<?php } ?> </td>
with this code the column sorting fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在ready函数之前添加这个:
并在ready函数处:
这就是它对我来说如何使用锚点的工作方式,整数按其应有的方式排序。
add this before the ready function:
and at the ready function:
This is how it works for me with anchors, integers are getting ordered as they should.
将列类型设置为“html”:
这将在排序之前去除 HTML 标签。
请参阅sType 参数文档
Set column type to 'html':
This will strip HTML tags before sorting.
See documentation for sType parameter
我只是在涉及链接时使用数据表进行排序时遇到问题 - 我有一列在某些单元格中有链接,而在其他单元格中没有链接。它似乎是排序,但它是对所有链接进行排序,然后对所有非链接进行排序,而不是同时对链接和非链接进行排序。事实证明,问题出在标记中的间距。当我去掉任何 a 标签周围的所有非必要间距时,我的列排序完美。
I just had an issue with sorting using datatables when links were involved - I had a column that had links in some cells, and no links in others. It seemed to be sorting, but it was sorting all links and then all non links, as oppose to sorting both links and non links together. The issue turned out to be spacing in the markup. When I took away all non essential spacing around any a tags, my columns sorted perfectly.
这对于答案来说已经很晚了,但它仍然对其他读者有帮助,你可以采取一个虚拟专栏。还隐藏该虚拟列并按虚拟列对实际列进行排序。假设我有三列,然后添加第四列作为虚拟列并将其放入数据表调用中
This is quite late for the answer, still it will helpful for other readers, You can take a dummy column. Also hide that dummy column and sort actual column by dummy column. Suppose I have three columns then add fourth coulmn as dummy and put this in datatable call
我使用 DataTables HTML5 数据属性 来获取所需的功能包含在锚标记中的可排序日期。
以下是多个表格单元格之一的 HTML 片段:
I used the DataTables HTML5 data attributes to get the desired function of a sortable date that is wrapped in an anchor tag.
Here is an HTML snippet for one of many table cells:
我认为这是因为您没有将锚点与同一列中的锚点数据混合。你也不应该使用onClick,特别是jquery,只需使用正确的选择器设置一个click()函数。
I think its because your are mixing not anchor with anchor data in the same column. You also shouldn't use onClick, aspecialy with jquery, just setup a click() function with the right selector.
正如这些人所说,并非您放入 tds 中的所有内容都是可排序的。例如,需要专门禁用链接。下面的代码片段将禁用表中第一列的排序。
As the guys said, not all content you put in the tds is sortable. Links for example need to be specifically disabled. The code snippet below will disable sorting for the first column in the table.
我的数据表有 2 列,第一列有
元素,我试图对第一列进行排序,但排序不起作用。我尝试了很多方法,但没有成功,所以我添加了一个带有
@Html.Raw
的隐藏列,它绑定到与我绑定到相同的模型属性,并在我的隐藏列。请参阅下面的链接
Jquery/JavaScript:对数据表进行排序具有 的列
My datatable had 2 columns and first column had
<a>
element and I was trying to sort my first column but sort was not working. I tried many ways but didnot work so I added a hidden column with@Html.Raw
which binded to same model property that I bind to<a>
and sorted on my hidden column. Please see below linkJquery/JavaScript : Sort a datatable on column that has <a>