【问题标题】:Unable to use the sorting function of DataTable无法使用DataTable的排序功能
【发布时间】:2016-10-17 12:51:48
【问题描述】:

我目前正在尝试学习和使用 ASP.NET 中的 DataTable。但是,我无法完成排序功能。我做了代码,我把图标去掉了,但我不能点击它或触发任何事件。我错过了一些javascript还是我应该自己完成这个功能?

我指的是这个网站https://datatables.net/examples/basic_init/table_sorting.html

这是我尝试过的代码

母版页(仅限脚本)

  <link href="Content/bootstrap.min.css" rel="stylesheet" />

        <!-- Custom CSS -->
        <link href="Content/sb-admin.css" rel="stylesheet" />
        <link href="Content/customCSS.css" rel="stylesheet" />
        <link href="Content/DataTables-1.10.12/media/css/dataTables.bootstrap.min.css" rel="stylesheet" />
        <link href="Content/DataTables-1.10.12/media/css/jquery.dataTables.min.css" rel="stylesheet" />

        <!-- Morris Charts CSS -->
        <link href="Content/plugins/morris.css" rel="stylesheet" />

        <!-- Custom Fonts -->
        <link href="fonts/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

        <!-- Bootstrap Core JavaScript -->
        <script src="Scripts/jquery-1.9.1.js"></script>
        <script src="Scripts/bootstrap.min.js"></script>
        <script src="http://code.jquery.com/jquery-1.12.3.js"></script>
        <script src="Content/DataTables-1.10.12/media/js/jquery.dataTables.min.js"></script>
        <script src="Content/DataTables-1.10.12/media/js/dataTables.bootstrap.min.js"></script>

<!-- Morris Charts JavaScript -->
<script src="Scripts/plugins/morris/raphael.min.js"></script>
<script src="Scripts/plugins/morris/morris.min.js"></script>
<script src="Scripts/plugins/morris/morris-data.js"></script>

ASPX

<script>
        function onPageLoad() {
            $(document).on("dblclick", "#tableCourseStructure tbody tr", function () {
                var $this = $(this);
                var row = $this.closest("tr");
                row.find('td:eq(1)');
                var courseCode = row.find('td:first').text();
                window.location.href = "UpdateCourse.aspx?CourseCode=" + courseCode;
            });
        }
    </script>

<table id="tableCourseStructure" class="table table-striped table-bordered dataTable no-footer hover display" role="grid">
                        <thead>
                            <tr role="row">
                                <th>Course Code</th>
                                <th>Course Version</th>
                                <th>Course Title</th>
                                <th>Last Modified Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <%=getCourseData()%>
                        </tbody>
                    </table>

ASPX.CS

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                retrieveBAL retrieveBAL = new retrieveBAL();
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "onPageLoad();", true);
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "sortingTable();", true);
            }

        }

        protected void ddlCourseCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "onPageLoad();", true);
        }

        public string getCourseData()
        {
            string data = "";
            retrieveBAL retrieveBAL = new retrieveBAL();

            foreach (DataRow row in retrieveBAL.retrieveCourseInfoByCategory(ddlCourseCategory.SelectedValue).Tables[0].Rows)
            {
                data += "<tr><td>" + row["courseCode"].ToString() + "</td><td>" + row["courseVersion"].ToString() + "</td><td>" + row["courseTitle"].ToString() + "</td><td>" + row["LastModifiedDate"].ToString() + "</td></tr>";
            }

        return data;

    }

【问题讨论】:

    标签: javascript c# jquery asp.net datatable


    【解决方案1】:

    您似乎缺少实际的 DataTable() 函数调用,该函数调用会将您的 html table 转换为 DataTable

    $('#tableCourseStructure').DataTable();
    

    您的 css 可能使它看起来像 DataTable,但如果没有上述内容,您将无法获得任何功能。

    【讨论】:

    • 哇。你打败了我。
    • 我这样做了,但是我的图标是重叠的。你知道为什么吗?
    • @Alvin - 是的,您的 css 要么有问题,要么与数据表 css 重叠。打开浏览器的开发人员工具并更改一些内容,看看是什么原因造成的。
    • 好的,非常感谢您的帮助!非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多