@model IEnumerable<Customer>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, canPage: false, canSort: false);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
.Grid
{
border: 1px solid #ccc;
border-collapse: collapse;
}
.Grid th
{
background-color: #F7F7F7;
font-weight: bold;
}
.Grid th, .Grid td
{
padding: 5px;
border: 1px solid #ccc;
}
.Grid, .Grid table td
{
border: 0px solid #ccc;
}
</style>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
columns: webGrid.Columns(
webGrid.Column("CustomerID", "Customer Id"),
webGrid.Column("ContactName", "Customer Name"),
webGrid.Column("City", "City"),
webGrid.Column("Country", "Country")))
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="~/scripts/quicksearch.js"></script>
<script type="text/javascript">
$(function () {
//Reference the Header Row and append another Row.
var row = $("#WebGrid thead").append("<tr />");
//Loop through all Header Cells.
$($("#WebGrid thead th")).each(function () {
//Clone the Header Cell.
var th = $(this).clone();
//Copy the Cell Text.
var text = th.html();
//Clear the Cell Text.
th.html('');
//Create dynamic TextBox. Set the Cell Text in PlaceHolder.
var textBox = $("<input type='text' class = 'search_textbox' placeholder = '" + text + "' />");
//Append the TextBox to Header Cell.
th.append(textBox);
//Append the Cloned Cell to the newly created Header Row.
row.append(th);
});
//Loop through all Search TextBoxes and apply QuickSearch plugin.
$('.search_textbox').each(function (i) {
$(this).quicksearch("#WebGrid tr:not(:has(th))", {
'testQuery': function (query, txt, row) {
return $(row).children(":eq(" + i + ")").text().toLowerCase().indexOf(query[0].toLowerCase()) != -1;
}
});
});
});
</script>
</body>
</html>