Hi I am trying to use the single column search in jqgrid using MVC 2 IN .NET (VS 2008) this is the code I have so far but I need an example to match it with or a tip of what I am missing
jQuery("#list").jqGrid({
url: '/Home/DynamicGridData/',
datatype: 'json',
mtype: 'POST',
search: true,
filters: {
"groupOp":"AND",
"rules": [
{"field":"Message","op":"eq","data":"True"}
]
},
multipleSearch: false,
colNames: [ 'column1', 'column2'],
colModel: [
{ name: 'column1', index: 'column1', sortable: true, search: true,
sorttype: 'text', autoFit: true,stype:'text',
searchoptions: { sopt: ['eq', 'ne', 'cn']} },
{ name: 'column2', index: 'column2', sortable: true,search: false,
sorttype: 'text', align: 'left', autoFit: true}],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 60, 100],
scroll: true,
sortname: 'column2',
sortorder: 'asc',
gridview: true,
autowidth: true,
rownumbers: true,
viewrecords: true,
imgpath: '/scripts/themes/basic/images',
caption: 'my data grid'
});
jQuery("#list").jqGrid('navGrid', '#pager', {add: false, edit: false, del: false},
{}, {}, {}, { multipleSearch: true, overlay: false });
//jQuery("#list").jqGrid('filterToolbar', {stringResult:true, searchOnEnter:true});
jQuery("#list").jqGrid('navButtonAdd', '#pager',
{ caption: "Finding", title: "Toggle Search Bar",
buttonicon: 'ui-icon-pin-s',
onClickButton: function() { $("#list")[0].toggleToolbar() }
});
jQuery("#list").jqGrid = {
search : {
caption: "Search...",
Find: "Find",
Reset: "Reset",
odata : ['equal', 'not equal','contains'],
groupOps: [ { op: "AND", text: "all" }, { op: "OR", text: "any" } ],
matchText: " match",
rulesText: " rules"
}
}
});
two things paging is not coming up and search although I have the search window opening with just hte column1 as an option and when clicking the find it seems like it loads the grid but actually without matching my value that I type in the text box.
UPDATED: as you can see I made an attempt with the serach argument that did not succeed thanks again for your help it is appreciated
//public ActionResult DynamicGridData(string sidx, string sord, int page, int rows,bool search, string fieldname,string fieldvalue)
public ActionResult DynamicGridData(string sidx, string sord, int page, int rows)
{
var context = new AlertsManagementDataContext();
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
int totalRecords = context.Alerts.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
IQueryable<Alert> alerts = null;
try
{
//if (!search)
//{
alerts = context.Alerts.
OrderBy(sidx + " " + sord).
Skip(pageIndex * pageSize).
Take(pageSize);
//}
//else
//{
// alerts = context.Alerts.Where (fieldname +"='"+ fieldvalue +"'").
// Skip(pageIndex * pageSize).
// Take(pageSize);
//}
}
catch (ParseException ex)
{
Response.Write(ex.Position + " " + ex.Message + " " + ex.Data.ToString());
}
//var alerts =
// from a in context.Alerts
// orderby sidx ascending
// select a;
var jsonData = new {
total = totalPages,
page = page,
records = totalRecords,
rows = (
from alert in alerts
select new {
id = alert.AlertId,
cell = new string[] {
"<a href=Home/Edit/"+alert.AlertId +">Edit</a> " +"|"+
"<a href=Home/Details/"+alert.AlertId +">Detail</a> ",
alert.AlertId.ToString() ,
alert.Policy.Name ,
alert.PolicyRule ,
alert.AlertStatus.Status ,
alert.Code.ToString() ,
alert.Message ,
alert.Category.Name}
}).ToArray()
};
return Json(jsonData);
}
Probably you have problem on the server side. Could you append your question with the code of
DynamicGridData
action which you currently use. The action should havefilters
as the parameter.Some parts of your current code are definitively wrong. For example
jqGrid
is the jQuery plugin. So the methods of jQuery will be extended with the mainjqGrid
method which you use asjQuery("#list").jqGrid(...);
. So after the initializing of jqGridjQuery("#list").jqGrid
will be a function. In you code (the last statement) you overwrite thejQuery("#list").jqGrid
method with the object{ search: { ... } }
. What you should do instead islike for example here is described how to overwrite the
emptyrecords
default value. You don't need to include the values which are already the same in the default jqGrid settings.Moreover if you use
searchoptions: { sopt: ['eq', 'ne', 'cn']}
on all searchable columns you don't need to do the change.In the text of your question you don't explained what you want to do. Your current code is so that you use the filter
Message
equal totrue
at the initial grid loading. Strange is that there are no column with the nameMessage
in the grid. If you want just send some additional information to the server you should better usepostData
parameter:I continue to recommend you to remove garbage from the jqGrid definition like
imgpath
andmultipleSearch
parameters of jqGrid andsortable: true, search: true, sorttype: 'text', autoFit: true, stype:'text', align: 'left'
which are either unknown or default.UPDATED: The original code of the Phil Haack demo is very old and it use LINQ to SQL. Like I wrote before (see here) Entity Framework (EF) allows to use sorting, paging and filtering/searching without any AddOns like LINQ Dynamic Query Library in form
System.Linq.Dynamic
. So I made the demo you you which is modification of the the Phil Haack demo to EF.Because you use the old version of Visual Studio (VS2008 with ASP.NET MVC 2.0) I made the demo also in VS2008.
You can download my VS2008 demo from here and VS2010 demo here.
In the code I show (additionally to the usage of Advanced Searching and Toolbar Searching in ASP.NET MVC 2.0) how to return exception information from ASP.NET MVC in JSON format and how to catch the information with the loadError method and display the corresponding error message.
To construct the Where statement from the ObjectQuery represented EF object I define the following helper class:
In the example I use only two datatypes
integer
(Edm.Int32
) andstring
(Edm.String
). You can easy expand the example to use more types based as above on thepropertyInfo.PropertyType.FullName
value.The controller action which provide the data to the jqGrid will be pretty simple:
To send the exception information to the jqGrid in JSON form I replaced the standard
[HandleError]
attribute of the controller (HomeController
) to the[HandleJsonException]
which I defined as the following:On the client side I used the following JavaScript code:
As the result if one types any non-numeric text (like 'ttt') in the searching toolbar one receive exception the controller action code (in
Int32.Parse(rule.data)
). One the client side one will see the following message:I send from the controller to the jqgrid the information about all internal exceptions. So for example, the error in connection to the SQL server will looks like
In the real world one verify the users input and throws exception with application oriented error message. I used in the demo specially no such kind of validation to show that all kind of exception will be cached and display by jqGrid.
UPDATED 2: In the answer you will find the modified VS2010 demo (downloadable from here) which demonstrate the usage of jQuery UI Autocomplete. Another answer extend the code more to export the grid contain in Excel format.
I have made an attempt with search argument that did not succeed
return Json(jsonData); }
It's a lot easier than you think for server side search. Your indexes in your grid will come across in the json call as arguments. Also there is a parameter in the GridSettings argument that will be set to true if it is a search.. It's called IsSearch. There is also a sortorder and column in the GridSettings argument that will help you build dy
So, you'd have something like this..
Check out below links: