ASP.NET MVC5 and DataTables for a beginner: what goes where?
ASP.NET MVC5 and DataTables for a beginner: what goes where?
I'm just starting an adventure into ASP.NET MVC5, armed with web development skills firmly rooted in 1995. After some effort, the basic CRUD application is working, and I'm proceeding to dress it up some. The functionality that DataTables delivers seems to be a perfect fit.
What I understand about JavaScript could fit in a small comment block, but I can follow well formed instructions. So when I found the documentation that said, "just add these three include lines and this one line of JS, and you're off and running," I thought it would be straightforward. I did what I thought the instructions said, and nothing changed. Doing more research, I found someone's project that creates bindings for MVC5 and DataTables 1.10, but the instructions are sparse ("easy" is only easy if you understand what they're telling you to do).
Beginning with the DataTables instructions ("just add these three lines..."), here's what I have:
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.css">
<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.js">
</script>
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table" id="products" >
<tr>
<th> @Html.DisplayName("Code")</th>
<th>@Html.DisplayNameFor(model => model.Name)</th>
<th>@Html.DisplayNameFor(model => model.Category)</th>
<th>@Html.DisplayName("Active")</th>
<th>@Html.DisplayName("Published")</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.ProductCode)</td>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Category)</td>
<td>@Html.DisplayFor(modelItem => item.Active.Value)</td>
<td>@Html.DisplayFor(modelItem => item.Published.Value)</td>
<td>@Html.ActionLink("Details", "Details", new { id=item.ID })</td>
</tr>
}
</table>
Theoretically, all I need to add is one line:
$(document).ready( function () {
$('#products').DataTable();
} );
What's missing (here's where my rudimentary understanding comes in) is WHERE to add it. I've tried several places, and no change to the table is made. The first place I tried was within the script
tag for the third block:
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.js">
$(document).ready( function () {
$('#products').DataTable();
} );</script>
Is that the right place? Am I identifying the target table correctly? Do I need to figure out how to implement the code found at https://github.com/ALMMa/datatables.mvc ? Should I abandon all hope until I've mastered JavaScript?
Thanks for whatever help you can give me.
Cheers.
JD
Answers
Per the request for a JSFiddle, here: http://jsfiddle.net/JDRay/dLjsgmyq/
Hi,
It looks like you are missing the
thead
andtbody
elements. See the HTML requirements.Also:
You would think that would work! But is doesn't... At least not consistently - see John Resig's (author of jQuery) investigation on the topic. You need to use a script tab for the loading of the script and another for the execution of the function.
Allan
I added the
thead
andtbody
tags (thank you for that), but am still getting this error:I've tried both
dataTable()
andDataTable()
, with the same results. Here's my current code:Per a recommendation from StackOverflow, I've made a few other changes with the same results (
Object doesn't support property or method
). Here's the current code:After a lot of help from here and StackOverflow, I've arrived at the conclusion that it's not the implementation of the code itself, which works fine in JSFiddle, but that something else is awry. I'll pursue implementation of the ALMMa binding project to see if that helps. In the meantime, here's a working Fiddle: http://jsfiddle.net/JDRay/dLjsgmyq/
There were a few issues with the fiddle linked to above:
I've updated the fiddle here just for reference should you find it useful: http://jsfiddle.net/dLjsgmyq/1/ .
Allan
In the fiddle you link to, I don't see any initialization code at all in any of the three panes. FWIW, I had gotten my table working in JSFiddle yesterday (http://jsfiddle.net/JDRay/dLjsgmyq/), but in VS it's still throwing an error saying that the object doesn't contain a method for DataTables(), which leads me to believe that, as you suggest, there's an error in the initialization code. But, as I mentioned, I don't see it in your fiddle. ???
You don't see this?
That part I see, however it's identical to what I have that's failing. What I don't see are these tags:
Through research, I'm finding that copying locally and adding these files through BundleConfig.cs is recommended, though it's not yet working. Progress, though, I think.
I got it working. I've documented the final solution on StackOverflow:
http://stackoverflow.com/questions/28442960/asp-net-mvc5-and-datatables-for-a-beginner-what-goes-where