不能按升序或降序表头排序 [英] Cannot sort table headers by ascending or descending

查看:255
本文介绍了不能按升序或降序表头排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,查看显示排序表这是伟大的,但不能排序通过点击表中的列表头按升序或降序排列。标题栏的箭头上升和下降的点击,但该数据保持不变。我使用jQuery的JTable和排序默认情况下启用。

By default the View displays a sorted table which is great but cannot Sort the table columns by clicking on the table headers in ascending or descending order. The header column arrows goes up and down on clicks but the data stays the same. I am using jQuery jTable and sorting is enabled by default.

时可以通过使用一个jQuery做到这一点?

Is is possible to do this by using a jQuery?

下面是我的code供你参考:

Here's my code for your inspection:

查看:

$(document).ready(function () {
    $('#TopPlayedInVenueContainer1').jtable({

        title: 'Top Tracks Played Records',
        paging: true,
        pageSize: 100,
        sorting: true,
        defaultSorting: 'Date ASC',

        actions: {
            listAction: '@Url.Action("TopPlayedInVenueList1")'

        },
        fields: {
            TrackID: {
                title: 'Track ID',
                key: true,
                create: false,
                edit: false,
                resize: false,
                tooltip: 'Track Name',
                sorting: true //This column is not sortable!
            },
            Date: {
                title: 'Date',
                type: 'date',
                displayFormat: 'dd - mm - yy',
                tooltip: 'DD - MM - YY',
                list: true,
                sorting: true //This column is not sortable!
            },
            TrackName: {
                title: 'Track Name',
                key: true,
                create: false,
                edit: false,
                resize: false,
                tooltip: 'Track Name',
                sorting: true //This column is not sortable!
            },
            ArtistName: {
                title: 'Artist Name',
                key: true,
                create: false,
                edit: false,
                resize: false,
                tooltip: 'Track Name',
                sorting: true //This column is not sortable!
            },
            Times: {
                title: 'Times',
                tooltip: 'Artist Name',
                sorting: false //This column is not sortable!
            }
        }
    });

    // All
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    var todayDate = new Date();
    var endDate = todayDate.getDate() + '/' + (todayDate.getMonth() + 1) + '/' + (todayDate.getFullYear() + 100);
    var d = new Date();
    var st = d.setDate(todayDate.getDate() - 111365);
    var startDate = d.getDate() + '/' + (d.getMonth() + 1) + '/' + d.getFullYear();
    $('#allrecordsstart').val(startDate);
    $('#allrecordsend').val(endDate);
    $('#TopPlayedInVenueContainer1').jtable('load', {
        StartDate: startDate,
        EndDate: endDate
    });

    $('#allrecords').click(function (e) {
        e.preventDefault();
        var startDate = $('#allrecordsstart').val();
        var endDate = $('#allrecordsend').val();

        $('#TopPlayedInVenueContainer1').show(0).delay(0).fadeIn(1000).jtable('load', {
            StartDate: startDate,
            EndDate: endDate

        });
    });

控制器:编辑@CHash_Mile ..感谢了很多:)这里的code:修改:15/04/2014

[HttpPost]
    public JsonResult TopPlayedInVenueList1(string StartDate = "", string EndDate = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
    {
        try
        {

            if (Request.IsAuthenticated == true)
            {
                string Path = @"C:\\5Newwithdate-1k.xls";
                OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '" + Path + "';Extended Properties=" + (char)34 + "Excel 8.0;IMEX=1;" + (char)34 + "");
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                con.Close();
                System.Data.DataTable data = new System.Data.DataTable();
                da.Fill(data);

                List<TopPlayed> daa = new List<TopPlayed>();

                foreach (DataRow p in data.Rows)
                {
                    TopPlayed top = new TopPlayed()
                    {
                        TrackID = Convert.ToInt32(p.Field<double>("TrackID")),
                        Date = p.Field<DateTime>("DateTimes"),
                        TrackName = p.Field<string>("TrackName"),
                        ArtistName = p.Field<string>("ArtistName"),
                        Times = Convert.ToInt32(p.Field<double>("Times"))
                    };

                    daa.Add(top);
                }

                var listOrder = daa.Where(i => i.Date >= Convert.ToDateTime(StartDate) && i.Date <= Convert.ToDateTime(EndDate)).ToList();

                if (jtStartIndex + 150 > listOrder.ToList().Count)
                {
                    int val = listOrder.ToList().Count - jtStartIndex;
                    jtPageSize = val;
                }

                var newlist = listOrder.OrderByDescending(i => i.Times).ToList().GetRange(jtStartIndex, jtPageSize);

                if (string.IsNullOrEmpty(jtSorting)) { jtSorting = "Date ASC"; }

                SortDirection sortDirection = jtSorting.ToLower().Contains("desc") ? SortDirection.DESC : SortDirection.ASC;
                string sortExpression = sortDirection == SortDirection.DESC ? jtSorting.ToLower().Replace(" desc", "") : jtSorting.ToLower().Contains(" asc") ? jtSorting.ToLower().Replace(" desc", "") : jtSorting;

                if (sortDirection == SortDirection.ASC)
                {
                 newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression)).ToList();
                }
                else
                {
                 newlist = newlist.OrderByDescending(item => GetPropertyValue(item, sortExpression)).ToList();
                }

                return Json(new { Result = "OK", Records = newlist, TotalRecordCount = listOrder.ToList().Count });
            }
            return Json(new { Result = "ERROR" });
        }
        catch (Exception ex)
        {
            return Json(new { Result = "ERROR", Message = ex.Message });
        }
    }

调试的使用步过一行行,似乎是该行的code是罪魁祸首:

Debugged using Step Over line by line and seems to be this line of the code is the culprit:

newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression)).ToList();

由于此行后,我得到错误信息在浏览器的查看:

Because after this line I get error message on the View in browser:

Object reference not set to an instance of an object.

图片:编辑@CHash_Mile ..感谢很多人:)调试的截屏:

Images: Edit for @CHash_Mile.. thanks a lot man :) screenshots of the debug:

SORTEX pression ----------的网址

sortExpression ---------- URL

newList ----------的网址

newList ---------- URL

我对你的时间洙多,真正AP preciate在你在做什么对不起我!

I'm sorry for taking your time soo much and really appreciate in what you're doing for me!

推荐答案

勾选此
<一href=\"http://www.$c$cproject.com/Articles/277576/AJAX-based-CRUD-tables-using-ASP-NET-MVC-and-jTa#Sorting\"相对=nofollow>使用JTable的例子排序

我看你是不是使用可变jtSorting排序。这给出与分拣需要做属性。试着用装载newlist后低于code。

I see you are not using variable jtSorting for sorting. this gives property with which sorting needs to be done. Try with below code after loading newlist.

        SortDirection sortDirection = jtSorting.ToLower().Contains("desc") ? SortDirection.DESC : SortDirection.ASC;
    string sortExpression = sortDirection == SortDirection.DESC ? jtSorting.ToLower().Replace(" desc", "") : jtSorting.ToLower().Contains(" asc") ? jtSorting.ToLower().Replace(" desc", "") : jtSorting;

    if (sortDirection == SortDirection.ASC)
            {
                newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression))).ToList();
            }
            else
            {
                newlist = newlist.OrderByDescending(item => GetPropertyValue(item, sortExpression))).ToList();
            } 

添加以下方法 -

Add below method -

    public static object GetPropertyValue(object obj, string propertyName)
    {
        return obj == null ? null : obj.GetType().GetProperty(propertyName).GetValue(obj, null);
    }

在下面添加您的枚举类中 -

Add below enum inside your class -

内部枚举SortDirection
    {
        ASC,
        DESC
    }

internal enum SortDirection { ASC, DESC }

这篇关于不能按升序或降序表头排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆