简体   繁体   English

jQuery / JavaScript:对具有

[英]Jquery/JavaScript : Sort a datatable on column that has <a>

This is similar to the problem below but not exactly the same since the post below does not tell about how the user is creating the datatable. 这与下面的问题类似,但不完全相同,因为下面的帖子并未说明用户如何创建数据表。 I googled a lot but could not find any helpful resource. 我在Google上搜索了很多,但是找不到任何有用的资源。

https://stackoverflow.com/questions/36476345/jquery-datatables-and-server-side-sorting-in-asp-net-mvc https://stackoverflow.com/questions/36476345/jquery-datatables-and-server-side-sorting-in-asp-net-mvc

I also followed below but none works 我也在下面关注,但是没有用

Sorting column with anchor tags in jQuery DataTables 在jQuery DataTables中使用锚标记对列进行排序

I have a datatable like below and I am trying to sort on my first column which has an <a> element. 我有一个像下面这样的数据表,我正在尝试对具有<a>元素的第一列进行排序。 If I remove <a> and use @Html.Raw(item.ID) it works fine but sorting does not happen when I use the <a> element. 如果我删除<a>并使用@Html.Raw(item.ID)它可以正常工作,但是当我使用<a>元素时不会进行排序。 Please let me know how can I sort a datatable on a column that has <a> element. 请让我知道如何在具有<a>元素的列上对数据表进行排序。

<table class="datatable table table-bordered table-hover" border="1">
    <thead>
        <tr>
            <th class="text-center">ID</th>@*this is for sorting purpose*@
            <th class="text-center">ID</th>
            <th class="text-center">Name</th>
        </tr>
    </thead>
    @foreach (var item in Model.FieldList)
    {
        <tr>
            @*this is for sorting purpose*@
            <td class="col-md-1">
                @Html.Raw(item.ID)
            </td>
            <td class="col-md-1">
                <a href='@Url.Action("StudentDetails", "Admin", new {id=@item.ID})'>
                    @Html.DisplayFor(model => item.ID)
                </a>
            </td>    
            <td class="col-md-4">
                @Html.DisplayFor(model => item.Name)                
            </td>
        </tr>
    }
</table>   
<script type="text/javascript">
    $(document).ready(function () {
        $('.datatable').dataTable({
            "sPaginationType": "bs_full",
            "aaSorting": [[0, "asc"]],
            "aoColumns": [{ "bVisible": false }, null, null]
        });       
    });
</script>

Thanks in advance. 提前致谢。

[UPDATE 1] [更新1]

As per Damian advice I added the code below. 根据Damian的建议,我添加了以下代码。 I can notice that for a small fraction of a second I can see the values sorted but datatable is not preserving the order and it is refreshing as if we are reloading the page. 我可以注意到,在很短的时间里,我可以看到排序的值,但是数据表没有保留顺序,并且刷新后就像重新加载页面一样。

"aoColumns": [{ "asSorting": ["asc"], "sType": "html" }, null]

[UPDATE 2] [更新2]

Thanks to Damian. 感谢达米安。 I used below. 我在下面用过。 Now my datatable is sorted but getting JavaScript error. 现在我的数据表已排序,但出现JavaScript错误。 Looking into it. 看着它。

"aoColumns": [{ "asSorting": ["asc"], "sType": "html" }, {null}]

SOLUTION

Thanks to Damian for pointing me correct direction. 感谢Damian为我指出正确的方向。 I solved by adding one hidden column and sorted using that column. 我通过添加一个隐藏列并使用该列进行排序来解决。 Please see above for my solution 请参阅上面的解决方案

You have two options: 您有两种选择:

1) Update to DataTables above 1.7, because previous versions do not support sorting on HTML elements. 1)更新到1.7以上的DataTables,因为以前的版本不支持对HTML元素进行排序。

2) Add the sType parameter to that column. 2)将sType参数添加到该列。 This will ignore the HTML in the function sort, in your case, something like: 在您的情况下,这将忽略函数排序中的HTML,例如:

"aoColumns": [
    { "sType": "html" }
   ],

Here is the official documentation about that. 这是关于此的官方文档

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

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