When I am trying to add jQuery datatable in my ASP.NET Core project I am getting below error "$(..).DataTable is not a function", here is the error image in Google Chrome console.
Although I have added jQuery datatable reference in my code.
<link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<div style="width: 500px">
<table id="tblEmp" cellpadding="0" cellspacing="0" border="1" style="border-collapse:collapse">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#tblEmp").DataTable({
"processing": true,
"serverSide": true, // enabling server side
"filter": true, //set true for enabling searching
"ajax": {
"url": "/home/person",// ajax url to load content
"type": "POST", // type of method to call
"datatype": "json" // return datatype
},
"columns": [
{ "data": "firstName", "name": "First Name", "autoWidth": true }, // columns name and related settings
{ "data": "lastName", "name": "Last Name", "autoWidth": true },
]
});
});
</script>
Thanks.
This error can be due to any one of the following causes:
<script type="text/javascript" src="~/Scripts/jquery.js"></script>
<script type="text/javascript" src="~/Scripts/data-table/jquery.dataTables.js"></script>
<script>$(document).ready(function () {
$.noConflict();
var table = $('#MyTable').DataTable();
});</script>?
In your .NET project, I will suggest you to wrap all Javascript code as inside "Scripts" section, if you are loading JS/CSS bundles, so if you are loading JS files on the master page (_Layout.cshtml
) in .NET MVC project, it works fine.
@section Scripts{
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
// JS code here
}
also, load jQuery datatable, after jQuery library, something like this
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.dataTables.min.js" type="text/javascript"></script>
to get rid of error.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly