I asked this question on the DataTables forum, but in the interest of getting quick feedback, I am willing to tolerate some abuse, so I cross-post here.
I'm just starting an adventure in ASP.NET MVC5, armed with web development skills firmly rooted in 1995. After some effort, the main CRUD application works, and I continue to dress it. The functionality provided by DataTables seems to fit perfectly.
I understand that JavaScript can fit in a small block of comments, but I can follow well-formed instructions. So when I found the documentation that said: βJust add these three include lines and one JS line, and you are off and running,β I thought it would be simple. I did what I thought was said in the instructions, and nothing has changed. Having done more research, I found someone a project that creates bindings for MVC5 and DataTables 1.10, but the instructions are sparse ("easy" is simple, if you understand what they tell you to do).
Starting with the DataTables instructions ("just add these three lines ..."), here is what I have:
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.css"> <script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script> <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 is missing (here, where my rudimentary understanding comes), WHERE to add it. I tried several places and there were no changes in the table. The first thing I tried was in 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 this a suitable place? Am I defining the target table correctly? I need to figure out how to implement the code found at https://github.com/ALMMa/datatables.mvc ? Do I have to give up all hope until I have mastered JavaScript?
Thanks for any help you can give me.
Greetings.
Jd