Home › Forums › Guriddo jqGrid JS › Discussion › Filter Toolbar not passing the search parameters to MVC controller
Tagged: filtertoolbar, search
Hi,
Here goes my grid code:
function LoadGrid() {
$(function () {
$(“#mailGrid”).jqGrid({
url: “/Home/GetAllTaggedMails”,
datatype: ‘json’,
cache: true,
styleUI: ‘Bootstrap’,
ajaxGridOptions: { contentType: “application/json” },
mtype: ‘Get’,
colNames: [‘ID’, ‘HasAttachments’, ‘ItemId’, ”, ‘Subject’, ‘Date/Time Sent’, ‘Recipients’, ‘Group Name’, ‘Sent/Rec’],
colModel: [
{ key: true, name: ‘ID’, index: ‘ID’, editable: false, search: false, sortable: false, hidden: true },//, width: ‘5%’
{ key: false, name: ‘HasAttachments’, search: false, editable: false, sortable: false, hidden: true },//index: ‘HasAttachments’,
{ key: false, name: ‘ItemId’, index: ‘ItemId’, search: false, editable: false, sortable: false, hidden: true },
{ key: false, name: ‘RowID’, formatter: imgFormatter, valign: ‘top’, search: false, sortable: false, align: ‘center’, defaultvalue: “”, width: ‘2%’ },
{ key: false, name: ‘Subject’, index: ‘Subject’, formatter: urlFormatter, editable: false, search: true, sortable: false, width: ‘33%’, searchoptions: { sopt: [‘cn’, ‘eq’], ignoreCase: true } },
{ key: false, name: ‘SentDate’, index: ‘SentDate’, formatter: ‘date’, sorttype: ‘date’, width: ‘15%’, editable: false, search: false, sortable: false, formatoptions: { srcformat: ‘Y-m-d H:i:s’, newformat: ‘Y-m-d H:i:s’, defaultvalue: ” } },
{ key: false, name: ‘Recipient’, index: ‘Recipient’, search: true, searchoptions: { sopt: [‘cn’, ‘eq’], ignoreCase: true }, editable: false, width: ‘20%’, sortable: false },
{ key: false, name: ‘GroupName’, index: ‘GroupName’, search: true, searchoptions: { sopt: [‘cn’, ‘eq’], ignoreCase: true }, editable: false, sortable: false, width: ‘22%’, searchoptions: { sopt: [‘cn’, ‘eq’], ignoreCase: true } },
{ key: false, name: ‘Direction’, search: true,stype:’select’, editable: false, width: ‘8%’, sortable: false, defaultvalue: “Sent”, formatter: boundFormatter, valign: ‘middle’, align: ‘center’ }
],
sortorder: ‘desc’,
grouping: true,
groupingView: {
groupField: [‘GroupName’],
groupDataSorted: true,
//plusicon: ‘ui-icon-circle-plus’,
//minusicon: ‘ui-icon-circle-minus’
},
sortname: ‘SentDate’,
fluid: true,
//sortable: true,
rowNum: 50,
forceFit: true,
gridview: true,
page: 1,
headertitles: true,
//rowList: [25, 50, 75, 100],
height: ‘400px’,
width: ‘100%’,
scroll: 1,
scrollOffset: 0,
forceClientSorting: true,
viewrecords: true,
search: true,
shrinkToFit: true,
pager: jQuery(‘#mailGridControls’),
emptyrecords: ‘There is no tagged emails found for previous week.’,
jsonReader: {
root: “rows”,
page: “page”,
total: “total”,
records: “records”,
Id: ‘Id’,
repeatitems: false,
IsLastPage: “IsLastPage”
},
autowidth: true,
multiselect: false,
loadComplete: function () {
$(“.input-elm”).val(“”);
},
beforeSelectRow: function (rowid, e) {
var $link = $(‘a’, e.target);
if (e.target.tagName.toUpperCase() === “A” || e.target.tagName.toUpperCase() === “IMG” || $link.length > 0) {
// link exist in the item which is clicked
return false;
}
return true;
},
onPaging: function (pageButton) {
}
}).navGrid(‘#mailGridControls’, {
edit: false, add: false, del: false, search: false,
searchtext: “Search Mails”, refresh: false
}, {}, {}, {},
{/*
zIndex: 999,
caption: “Search Tagged Email”,
overlay: false,
multipleSearch: false,
Find: “Search”,
ignoreCase: true,
sopt: [‘cn’, ‘eq’],
defaultSearch: ‘cn’,
closeAfterSearch: true,
beforeShowSearch: function ($form) {
//$(“.searchFilter table td .input-elm”).attr(‘style’, ‘width:200px !important;’);
setModalWidth(‘.ui-jqdialog’);
return true;
},
afterRedraw: function ($form) {
//$(“.searchFilter table td .input-elm”).attr(‘style’, ‘width:250px !important;’);
setModalWidth(‘.ui-jqdialog’);
return true;
}*/
});
$(“#mailGrid”).jqGrid(‘filterToolbar’, { searchOnEnter: false, ignoreCase: true, defaultSearch: “cn” });//autosearch:true, stringResult: false,
//var myGrid = $(“#mailGrid”);
//myGrid[0].triggerToolbar();
$(“#mailGrid”).navButtonAdd(‘#mailGridControls’, {
position: “left”,
caption: “Expand/Collapse All”,
title: “Expand/Collapse All”,
cursor: “pointer”,
buttonicon: “ui-icon-triangle-2-n-s”,
onClickButton: function () {
$(“#mailGrid”).find(“.treeclick”).trigger(‘click’);
}
});
if ($.session.get(‘IsInitial_load’) === “true”)
$(‘#mailGrid’).navButtonAdd(‘#mailGridControls’,
{
buttonicon: “ui-icon-plus”,
title: “More”,
caption: “More”,
position: “right”,
onClickButton: moreClicked
});
});
imgFormatter = function (cellvalue, options, rowObject) {
if (rowObject[“HasAttachments”] === “TRUE”)
return ““;
else
return “”;
};
boundFormatter = function (cellvalue, options, rowObject) {
if (cellvalue !== null && cellvalue !== undefined && cellvalue !== ‘null’ && cellvalue !== ‘undefined’) {
if (cellvalue.toUpperCase() === “SENT”)
return ““;
else
return ““;
}
else
return ““;
};
urlFormatter = function (cellvalue, options, rowObject) {
var sub = cellvalue;
var oId = rowObject[“ItemId”];
var uId = rowObject[“ID”];
var webaccesssiteURL = $(“#webaccessURL”).val().toString().replace(“[ItemID]”, oId.replace(‘+’, ‘%2B’));
var retString = “” + sub + ““;
return retString;
};
if ($.session.get(‘CurrentPage’) !== ‘undefined’ || $.session.get(‘CurrentPage’) !== undefined)
$(“#mailGrid”).trigger(“reloadGrid”, [{ page: $.session.get(‘CurrentPage’) }]);
}
And my search code in the controller:
public JsonResult GetAllTaggedMails(string sord, string sidx, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
{
try
{
int pageIndex = Convert.ToInt32(page) – 1;
int pageSize = rows;
#region Get the e-mail details with client matter code from the mailbox of the user
var topMails = Session[“AllTaggedMails”] == null ? GetTopMails() : Session[“AllTaggedMails”] as List<MailEntity>;
//if (Session[“AllTaggedMails”] == null)
Session[“AllTaggedMails”] = topMails as List<MailEntity>;
var allTaggedMails = topMails.OrderByDescending(y => y.SentDate).Select(
c => new
{
c.RowID,
ID = c.UniqueID.ToString(),
c.Subject,
SentDate = Convert.ToDateTime(c.SentDate),
c.Recipient,
HasAttachments = c.HasAttachments == true ? “TRUE” : “FALSE”,
GroupCode = c.GroupCode == null ? “” : c.GroupCode,
GroupName = c.GroupName == null ? “” : c.GroupName,
c.ItemId,
c.Direction
});
#endregion
#region Search
if (_search)
{
switch (searchField == null ? searchField : searchField.ToUpper())
{
case “SUBJECT”:
if (searchOper == “eq”)
allTaggedMails = allTaggedMails.Where(s => s.Subject.Equals(searchString));
else
allTaggedMails = allTaggedMails.Where(s => s.Subject.ToLower().Contains(searchString.ToLower()));
break;
case “RECIPIENT”:
if (searchOper == “eq”)
allTaggedMails = allTaggedMails.Where(s => s.Recipient.Equals(searchString));
else
allTaggedMails = allTaggedMails.Where(s => s.Recipient.ToLower().Contains(searchString.ToLower()));
break;
case “GROUPCODE”:
if (searchOper == “eq”)
allTaggedMails = allTaggedMails.Where(s => s.GroupCode.Equals(searchString));
else
allTaggedMails = allTaggedMails.Where(s => s.GroupCode.ToLower().Contains(searchString.ToLower()));
break;
case “GroupNAME”:
if (searchOper == “eq”)
allTaggedMails = allTaggedMails.Where(s => s.GroupName.Equals(searchString));
else
allTaggedMails = allTaggedMails.Where(s => s.GroupName.ToLower().Contains(searchString.ToLower()));
break;
}
}
#endregion
int totalRecords = allTaggedMails.Count();
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
#region Sort
string sortIndex = string.Empty;
if (sidx != null)
{
if (sidx.IndexOf(‘,’) >= 0)
{
sortIndex = sidx.Split(new char[] { ‘,’ }, StringSplitOptions.RemoveEmptyEntries)[1].ToString().Trim();
}
}
else
sortIndex = “SENTDATE”;
//switch (sidx == null ? sidx : sidx.ToUpper())
switch (sortIndex.ToUpper())
{
case “SENTDATE”:
if (sord.ToUpper() == “DESC”)
allTaggedMails = allTaggedMails.OrderByDescending(s => s.SentDate);
else
allTaggedMails = allTaggedMails.OrderBy(s => s.SentDate);
break;
case “SUBJECT”:
if (sord.ToUpper() == “DESC”)
allTaggedMails = allTaggedMails.OrderByDescending(s => s.Subject);
else
allTaggedMails = allTaggedMails.OrderBy(s => s.Subject);
break;
case “GroupCODE”:
if (sord.ToUpper() == “DESC”)
allTaggedMails = allTaggedMails.OrderByDescending(s => s.GroupCode);
else
allTaggedMails = allTaggedMails.OrderBy(s => s.GroupCode);
break;
case “GroupNAME”:
if (sord.ToUpper() == “DESC”)
allTaggedMails = allTaggedMails.OrderByDescending(s => s.GroupName);
else
allTaggedMails = allTaggedMails.OrderBy(s => s.GroupName);
break;
case “ROWID”:
if (sord.ToUpper() == “DESC”)
allTaggedMails = allTaggedMails.OrderByDescending(s => s.HasAttachments);
else
allTaggedMails = allTaggedMails.OrderBy(s => s.HasAttachments);
break;
case “RECIPIENT”:
if (sord.ToUpper() == “DESC”)
allTaggedMails = allTaggedMails.OrderByDescending(s => s.Recipient);
else
allTaggedMails = allTaggedMails.OrderBy(s => s.Recipient);
break;
case “DIRECTION”:
if (sord.ToUpper() == “DESC”)
allTaggedMails = allTaggedMails.OrderByDescending(s => s.Direction);
else
allTaggedMails = allTaggedMails.OrderBy(s => s.Direction);
break;
default:
allTaggedMails = allTaggedMails.OrderByDescending(s => s.SentDate);
break;
}
#endregion
allTaggedMails = allTaggedMails.Skip(pageIndex * pageSize).Take(pageSize);
if (Session[“IsLastPage”] != null)
isLastPage = Convert.ToBoolean(Session[“IsLastPage”].ToString().ToLower());
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = allTaggedMails.ToList(),
IsLastPage = isLastPage
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
ExReporter.Report(ex, MethodInfo.GetCurrentMethod().Name.ToString(), MethodInfo.GetCurrentMethod().DeclaringType.ToString(), Session[“User”].ToString());
return null;
}
}
When I type the text in the filter columns, the controller method receives, the searchField, searchOper, and searchString field values as null but the _search is passed as true.
Please let me know where in the code I am making the mistake?
Thanks in advance.
Senthil Kumar M.
Hello,
The searchField, searchOper, and searchString field values are not present in filterToolbar method. These are valid only in certain cases in form searching (this is a old behaviour)
Instead of this use the filters property and accept it in the server. I recommend you to read this docs Here we describe what is posted to the server when searching
Kind Regards
Will
Guriddo Support Team
Copyright 2014 TriRand LtdAll Rights ReservedRSS
Back to Top