Skip to main content
 首页 » 编程设计

jquery之Angular Datatables 对超链接内的文本而不是 href 进行排序

2026年07月28日11duanxz

我正在尝试按字母数字方式对包含 anchor 标记的列进行排序,该 anchor 标记似乎“拦截”了排序功能。它看起来像以下几行:

<tbody> 
    <tr><td><a href="/1"></a>Fox</td><tr> 
    <tr><td><a href="/2"></a>Cow</td><tr> 
    <tr><td><a href="/3"></a>Dog</td><tr> 
</tbody> 

它将分别返回 Fox,Cow,Dog 和 Dog,Cow,Fox。如何根据 a 标记的内容而不是 a 标记本身的文本进行排序?

ctrl.dtOptions = DTOptionsBuilder.newOptions() 
    .withPaginationType('full_numbers') 
    .withOption("order", [ 
        [1, "asc"] 
    ]) 
    .withOption('paging', false) 
    .withOption('searching', false) 
    .withOption("sDom", '<"top">rt<"bottom"flp><"clear">'); 
 
ctrl.dtColumnDefs = [ 
    DTColumnDefBuilder.newColumnDef(0).notSortable(), 
    DTColumnDefBuilder.newColumnDef(1) 
]; 

1:不可排序
2:字母数字(问题栏)
3-10:字母数字 - td 只有数字,因此排序正常

请您参考如下方法:

尝试对包含 HTML 的列使用 .withOption('type', 'html'),如下所示:

ctrl.dtColumnDefs = [ 
    DTColumnDefBuilder.newColumnDef(0).notSortable(), 
    DTColumnDefBuilder.newColumnDef(1).withOption('type', 'html') 
]; 

来自documentation :

html - Basic string processing for HTML tags

Sorting - sorted with HTML tags removed
Filtering - HTML tags removed from filtering string

参见columns.type了解更多信息。