Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,

I am trying to add Sorting, Paging and Filter functionalities to my jQuery DataTable, but none of them are working. I tried mostly everything I could to get this to work but don't know what am I missing here? I am following this example from CodeProject - jQuery DataTables and ASP.NET MVC Integration but for some reason the functionalities are not working. Could you please help me out?

Here's my code for your inspection:
C#
// Pagination
List<TopPlayed> filteredTracks = daa;
var TopPlayed = filteredTracks.Skip(param.iDisplayStart).Take(param.iDisplayLength);

// Sorting
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func<TopPlayed, string> orderingFunction = (c => sortColumnIndex == 1 ? c.TrackName :
sortColumnIndex == 2 ? c.TrackName : c.ArtistName);
var sortDirection = Request["sSortDir_0"]; // asc or desc
if (sortDirection == "asc")
   filteredTracks = daa.OrderBy(orderingFunction).ToList();
else
   filteredTracks = daa.OrderByDescending(orderingFunction).ToList();

// Filter
if (!string.IsNullOrEmpty(param.sSearch))
{
   daa.Where(c => c.TrackName.Contains(param.sSearch)
      ||
      c.ArtistName.Contains(param.sSearch)
      ||
      c.TrackName.Contains(param.sSearch));
}
else
{
   filteredTracks = daa;
}

Here's the data that I'm working with:
C#
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);
}

This is what I am returning:
C#
return Json(new { sEcho = param.sEcho, 
   iTotalRecords = filteredTracks.ToList().Count(), 
   iTotalDisplayRecords = filteredTracks.ToList().Count(),
   aaData = daa }, 
   JsonRequestBehavior.AllowGet);

Any help would be great help.. thanks in advance :)
Posted
Updated 19-May-14 0:40am
v4
Comments
Member 10812800 19-May-14 6:25am    
please anyone?
Sampath Lokuge 19-May-14 8:57am    
Have you added jQuery libraries properly ? And also why don't you download the code and test it ?
Member 10812800 19-May-14 9:52am    
How can I thank you? Man I completely forgot about it. I had been stuck with this for awhile now. Thanks a lot for this. Got everything working :) God Bless You! :)

Is there a way to give you +rep? thanks again :)
Sampath Lokuge 19-May-14 9:55am    
You're warmly welcome. :)
Sampath Lokuge 19-May-14 9:59am    
I have updated the answer. :)

1 solution

Have you added jQuery libraries properly ? And also why don't you download the code and test it ?

You can download the source code from here : Source code
 
Share this answer
 
v2
Comments
Member 10812800 19-May-14 10:02am    
Awesome man, I have accepted the Solution and +5 stars your solution. Thanks again lots :)
Sampath Lokuge 19-May-14 10:03am    
You're warmly welcome. :)
Member 10812800 19-May-14 10:03am    
Very Happy :) Cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900