It's been days since I started to study MVC programming and honestly I'm still coping up to new environment of MVC.
In my project, I started to create a data table that display the data in my database using these codes.
This is my codes in view and controller. This part runs very well.
<table id="table1" >
<thead>
<tr>
<th>id</th>
<th>title </th>
<th>
description
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
</tr>
}
</tbody>
</table>
}
var charts = (from p in db.pmTA_ProjectCategory
select new
{
id = p.id,
title = p.title,
description = p.description,
}).ToList()
.Select(x => new pmTA_ProjectCategory()
{
id = x.id,
title = x.title,
description = x.description,
});
return View(charts.ToList());
But I noticed that I need to filter my data using dropdown so I added drop down to my View again.
This is my code in view and controller to display the dropdown and data inside the dropdown.
<div>
@Html.DropDownList("projectcat", ViewBag.proj as SelectList, "Select...", new {
@class = "form-control" })
</div>
var data1 = from p in db.pmTA_ProjectCategory
select new
{
id = p.id,
title = p.title,
description = p.description
};
SelectList list = new SelectList(data1, "id", "title");
ViewBag.proj = list;
when it comes to displaying the data inside the dropdown it runs again smoothly.
My problem is, I need to filter the data of data table using dropdown automatically. I mean, when I selected the value of dropdown the data table must show the data corresponds to the selected value in dropdown
I created codes in javascript to filter the data of datatable using dropdown.
This is the code:
<script>
$(document).ready(function () {
var table = $("#table1").DataTable();
$("#projectcat").change(function () {
table.search(this.value).draw();
});
});
</script>
But the data in my data table is not responding and not functioning, when I selected the data in dropdown the datatable can't filter.