I've asked this question on the DataTables forum, but in the interest of getting quick feedback, I'm willing to endure some abuse, so I'm cross-posting here.
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