默认情况下,View
会显示一个很好的排序表,但不能通过按升序或降序单击表标题来Sort
表列。标题列箭头在点击时上下移动,但数据保持不变。我正在使用jQuery jTable并默认启用排序。
可以通过使用jQuery来实现吗?
这是我的检查代码:
查看:
$(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 ..非常感谢:)这里是代码:编辑: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 });
}
}
逐行使用Step Over
进行调试,似乎这行代码是罪魁祸首:
newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression)).ToList();
因为在此行之后,我在浏览器中的View上收到错误消息:
Object reference not set to an instance of an object.
图片:编辑@CHash_Mile ..感谢很多人:)调试截图:
sortExpression
---------- URL
newList
---------- URL
我很抱歉花了很多时间,非常感谢你为我做的事情!
答案 0 :(得分:0)
检查一下 Sorting using Jtable example
我发现你没有使用变量jtSorting进行排序。这给出了需要进行排序的属性。加载新列表后,请尝试使用以下代码。
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();
}
添加以下方法 -
public static object GetPropertyValue(object obj, string propertyName)
{
return obj == null ? null : obj.GetType().GetProperty(propertyName).GetValue(obj, null);
}
在您的课程中添加以下枚举 -
内部枚举SortDirection { ASC, DESC }