사용자가 양식과 컨트롤을 검색하여 jqGrid에 결과를 표시한다고 가정합니다. 대부분의 솔루션은 온라인에서 jqGrid 자체의 검색 컨트롤을 사용하고있어 문제에 대한 선호 옵션이 아닐 수 있습니다. 필요에 따라)
1 검색 양식을 작성 :
난이 acomplish하는 방법에 대한 간단한 예를 보여 드리겠습니다
@using (Html.BeginForm("Index", "Campaign", FormMethod.Post, new { id = "searchCampaigns" }))
{
<table class="DetailsTable" cellpadding="2" cellspacing="1">
<tr>
<td>Title</td>
<td>@Html.TextBoxFor(A => A.Titulo, new { Id = "search_title", })</td>
<td>Created by</td>
<td>@Html.DropDownListFor(A => A.CreatedByUserId, Model.UserList, new { Id = "search_createdBy" })
</td>
</tr>
<tr>
<td> </td>
<td colspan="3"><button type="button" onclick="javascript:search();">
Search !</button>
</td>
</tr>
</table>
2)
가 순서대로 검색 기능을 구현 검색 입력란을 읽으십시오.
<script type="text/javascript">
function search()
{
var searchValue_title = document.getElementById("search_title");
var searchValue_createdBy = document.getElementById("search_createdBy");
var extraQueryParameters = "";
extraQueryParameters = "?title=" + searchValue_title.value;
extraQueryParameters = extraQueryParameters + "&createdBy=" + searchValue_createdBy.value;
jQuery("#SearchResults").jqGrid().setGridParam({url : '@Url.Action("GridData")' + extraQueryParameters}).trigger("reloadGrid")
}
</script>
실제로 @ HTML.TextBoxFor (...)를 사용하여 입력 요소를 만듭니다. MVC 3의 dataAnnotation을 사용하기를 원하지 않는 한 간단한 요소로 처리 할 수 있습니다.
검색 기능은 모든 검색 매개 변수를 연결하고 GridData 작업에 추가합니다. URL은 http://mySite/Controller/GridData?title=hello&createdBy=3과 같이되어야합니다. 이것은 그리드에 공급됩니다.
3 )이 라인을 따라 MVC 컨트롤러 기능을 구현 :
public JsonResult GridData(string sidx, string sord, int? page, int? rows, int? createdBy, string title)
{
using (MyDataContext ddc = new MyDataContext())
{
var baseQuery = ddc.MyCampaign.AsQueryable();
string gridCaption = "Search Results";
if (!string.IsNullOrEmpty(titulo))
baseQuery = baseQuery.Where(A => A.Title.Contains(title));
if(createdBy.HasValue())
baseQuery = baseQuery.Where(A=>A.idCreationUser = createdBy.Value);
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows.HasValue ? rows.Value : 10;
int totalRecords = baseQuery.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize);
var ds = (from A in baseQuery
select new
{
ID = A.ID,
Title = A.Title,
}).OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize).ToList();
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = from A in ds
select new
{
id = A.ID,
cell = new[] { A.ID.ToString(), A.Title }
},
caption = gridCaption
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
}
4) 다음과 같은 문제에 주목하시기 바랍니다 :는 C# 기능에 대한
의 매개 변수 이름을 가 있다는 주장과 일치해야 검색 단추를 클릭하면 쿼리 문자열 빌드가 전달됩니다. 이것은 버그가 http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx 하지만, 대부분의 경우, 작동 :)
크레이그, 당신의 기사에서 다음 .OrderBy (sidx + ""+ sord) 방법을 사용하면 동적 Linq에 DLL에서 사용할 수를 사용해야합니다 당신은 '시각적으로이 테마에 많은 변형이 있습니다'라고 언급했습니다. 더 많은 빛을 낼 수 있습니까? – user38230
검색은 팝업 대화 상자에서 위와 같거나 그리드 (filterToolbar)의 각 열 위에 다른 입력으로 표시 될 수 있습니다. –