簡體   English   中英

jQuery / JavaScript:對具有

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

這與下面的問題類似,但不完全相同,因為下面的帖子並未說明用戶如何創建數據表。 我在Google上搜索了很多,但是找不到任何有用的資源。

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

我也在下面關注,但是沒有用

在jQuery DataTables中使用錨標記對列進行排序

我有一個像下面這樣的數據表,我正在嘗試對具有<a>元素的第一列進行排序。 如果我刪除<a>並使用@Html.Raw(item.ID)它可以正常工作,但是當我使用<a>元素時不會進行排序。 請讓我知道如何在具有<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>

提前致謝。

[更新1]

根據Damian的建議,我添加了以下代碼。 我可以注意到,在很短的時間里,我可以看到排序的值,但是數據表沒有保留順序,並且刷新后就像重新加載頁面一樣。

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

[更新2]

感謝達米安。 我在下面用過。 現在我的數據表已排序,但出現JavaScript錯誤。 看着它。

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

感謝Damian為我指出正確的方向。 我通過添加一個隱藏列並使用該列進行排序來解決。 請參閱上面的解決方案

您有兩種選擇:

1)更新到1.7以上的DataTables,因為以前的版本不支持對HTML元素進行排序。

2)將sType參數添加到該列。 在您的情況下,這將忽略函數排序中的HTML,例如:

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

這是關於此的官方文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM