我正在尝试将日期显示在jQuery数据表上,但我收到以下错误"Requested unknown parameter 'DateTimes' from the data source for row 0
,然后它显示包含数据的所有其他列,但将DateTimes
列留空。
这里是我的View
代码,用于jQuery Datatable:
<script>
$(document).ready(function () {
$('#myDataTable').dataTable({
"bServerSide": true,
"sAjaxSource": "TopPlayedInVenueList2",
"bProcessing": true,
"aoColumns": [
{ "mData": "TrackID" },
{ "mData": "DateTimes", "sType": 'date' },
{ "mData": "TrackName" },
{ "mData": "ArtistName" },
{ "mData": "Times" }
]
});
});
</script>
<table id="myDataTable" class="display">
<thead>
<tr>
<th>ID</th>
<th>Date</th>
<th>Track Name</th>
<th>Artist Name</th>
<th>Times</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
这是我的服务器端Controller
代码,用于转换日期:
var listOrder = daa.Where(i => i.Date >= Convert.ToDateTime(StartDate) && i.Date <= Convert.ToDateTime(EndDate)).ToList();
编辑:TopPlayedInVenueList2操作方法控制器代码:
public ActionResult TopPlayedInVenueList2(jQueryDataTableParamModel param, string StartDate = "", string EndDate = "")
{
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 newlist = daa.OrderBy(i => i.Times).ToList();
if (!string.IsNullOrEmpty(param.sSearch))
{
newlist = daa;
daa.Where(c => c.TrackName.Contains(param.sSearch)
||
c.ArtistName.Contains(param.sSearch)
||
c.TrackName.Contains(param.sSearch));
}
else
{
newlist = daa;
}
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func<Company, string> orderingFunction = (c => sortColumnIndex == 1 ? c.Name :
sortColumnIndex == 2 ? c.Address :
c.Town);
var sortDirection = Request["sSortDir_0"]; // asc or desc
if (sortDirection == "asc")
newlist = daa.OrderBy(i => i.Times).ToList();
else
newlist = daa.OrderByDescending(i => i.Times).ToList();
var displayedCompanies = newlist;
// var listOrder = daa.Where(i => i.Date >= Convert.ToDateTime(StartDate) && i.Date <= Convert.ToDateTime(EndDate)).ToList();
return Json(new { sEcho = param.sEcho,
iTotalRecords = newlist.ToList().Count(),
iTotalDisplayRecords = newlist.ToList().Count(),
aaData = daa
}, JsonRequestBehavior.AllowGet);
}
任何帮助都会有很大的帮助:)谢谢你提前。
答案 0 :(得分:3)
Collection对象没有DateTime属性。它有Date属性。
相应地更改DT配置:
$('#myDataTable').dataTable({
"bServerSide": true,
"sAjaxSource": "TopPlayedInVenueList2",
"bProcessing": true,
"aoColumns": [
{ "mData": "TrackID" },
{ "mData": "Date", "sType": 'date' },
{ "mData": "TrackName" },
{ "mData": "ArtistName" },
{ "mData": "Times" }
]
});
答案 1 :(得分:0)
如何在Datatable js数据表列中以“ dd / MM / yyyy”格式显示日期。
<script>
$('#myDataTable').dataTable({
"bServerSide": true,
"sAjaxSource": "TopPlayedInVenueList2",
"bProcessing": true,
"aoColumns": [
{ "mData": "TrackID" },
{ "mData": "DateTimes", "sType": 'date' },
{ "mData": "TrackName" },
{ "mData": "ArtistName" },
{ "mData": "Times" }
],
"columnDefs": [
{
"render": function (data, type, row) {
debugger;
var pattern = /Date\(([^)]+)\)/;
var results = pattern.exec(data);
var date = new Date(parseFloat(results[1]))
var month = date.getMonth() + 1;
var day = date.getDate();
return (day > 9 ? day : "0" + day) + "/" + (month > 9 ? month : "0" + month) + "/" + date.getFullYear();
},
"targets": 1
}
]
});
</script>
我希望这会有所帮助。