DatatableJS is a helper to create a grid with Jquery Datatable and provides an extension to retrive data generically from Entity Framework context. It possible to use many datatable.js features with Html helper. It gives serverside or client side options. There's more: Wiki Documentation
Install DatatableJS for .Net Core, .Net 5, .Net 6 and use tag helpers.
PM> Install-Package DatatableJS
<datatable name="PersonGrid">
<columns>
<column field="Id" visible="false" />
<column field="Name" width="50" title="Full Name" />
<column field="Age" />
<command-item field="Id" on-click="onClick" btn-class="btn btn-info" text="Edit" icon-class="fa fa-edit"/>
<commands field="Id" text="Actions" items='new [] { new Command("Update", "onClick"), new Command("Delete", "onClick") }'/>
</columns>
<data-source url="@Url.Action("GetDataResult")" method="POST" server-side="true" data="addParam"/>
<language url="//cdn.datatables.net/plug-ins/1.10.22/i18n/Turkish.json"/>
<filters>
<add field="Id" value="1" operand="GreaterThanOrEqual"/>
</filters>
<captions top="top caption here" bottom="bottom caption here"/>
<render />
</datatable>
Or, Install DatatableJS.Net for .Net Frameworks (4.5 ... 4.8) from the package manager console:
PM> Install-Package DatatableJS.Net
@(Html.JS().Datatable<Person>()
.Name("PersonGrid")
.Columns(col =>
{
col.Field(a => a.Id).Visible(false);
col.Field(a => a.Name).Title("First Name").Class("text-danger");
col.Field(a => a.Age).Title("Age").Searchable(false);
col.Field(a => a.BirthDate).Title("Birth Date").Format("DD-MMM-Y");
col.Command(a => a.Id, "onClick", text: "Click").Title("");
})
.Filters(filter =>
{
filter.Add(a => a.Id).GreaterThanOrEqual(1);
})
.URL(Url.Action("GetDataResult"), "POST")
.ServerSide(true)
.Render()
)
Using .ToDataResult(request)
extension function with IQueryable collection, provides data can get with server side pagination very simply. To use this feature
install DatatableJS.Data from the package manager console:
PM> Install-Package DatatableJS.Data
using DatatableJS.Data
public JsonResult GetDataResult(DataRequest request)
{
DataResult result = context.People.ToDataResult(request);
return Json(result);
}
Then add datatables.net Javascript and CSS files or links to your project.
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>