jqgrid deom:http://www.trirand.com/blog/jqgrid/jqgrid.html
jQuery(function($){
//search
$("input[name='datepicker']").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd',
regional: ['zh-CN']
});
$('#respondButtonSubmit').click(function(){
loadRespondList();
});
//Ajax
loadRespondList();
//End
});
//Functions
function loadRespondList() {
if (!$("#respondList").getGridParam("url")) {
initRespondList();
return;
}
var params = getRespondParams();
$("#respondList").appendPostData(params);
$("#respondList").trigger("reloadGrid");
}
function getRespondParams(){
var searchName = $('#respondInputName').val();
var searchURL = $('#respondInputURL').val();
var searchType = $('#respondSelectType').val();
var searchTimeStart = $('#respondSelectTimeStart').val();
var searchTimeEnd = $('#respondSelectTimeEnd').val();
var params = {
name : searchName,
url : searchURL,
type : searchType,
timeStart : searchTimeStart,
timeEnd : searchTimeEnd
};
return params;
}
function initRespondList(){
$("#respondList").jqGrid({
url: 'GetRespondList',
datatype: 'json',
mtype: 'GET',
colNames: ['提交人', '问题发生页面', '反馈类型', '反馈内容', '反馈时间'],
colModel: [{ name: 'name', index: 'name', sortable: false , '32px' },
{ name: 'url', index: 'url', sortable: false , '80px' },
{ name: 'type', index: 'type', sortable: false , '20px' },
{ name: 'content', index: 'content', sortable: false },
{ name: 'time', index: 'time', sortable: false , '36px' }
],
pager: $("#respondListPage"),
auto true,
shrinkToFit: true,
forceFit: true,
height: '100%',
rowNum: 15,
rowList: [15, 30, 45],
altRows: true,
formEdit: true,
//scroll:true,
modal: false,
drag: true,
sortname: '',
sortorder: 'desc',
viewrecords: true,
multiselect: false,
imgpath: GRID_IMG_PATH,
postData: {
name: "",
url: "",
type: "",
timeStart: "",
timeEnd:""
},
beforeRequest: function() {
//beforeRequest
},
loadComplete: function() {
$('#respondList td').wrapInner('<div class="respondList-td"></div>');
},
ondblClickRow: function(id) {
//双击
},
onSelectRow: function(id) {
//选择
},
caption:"用户反馈信息"
}).navGrid("#respondListPage", { edit: false, add: false, del: false, search: false });
//End
}
Controller:
public JsonResult GetRespondList(string sidx, string sord, int page, int rows, string name, string url, string type, string timeStart, string timeEnd)
{
DateTime dtTimeStart;
DateTime dtTimeEnd;
name = HttpUtility.HtmlEncode(name);
url = HttpUtility.HtmlEncode(url);
type = HttpUtility.HtmlEncode(type);
if (!DateTime.TryParse(timeStart, out dtTimeStart))
{
dtTimeStart = DateTime.Parse("1900-01-01");
}
if (!DateTime.TryParse(timeEnd, out dtTimeEnd))
{
dtTimeEnd = DateTime.Parse("2900-01-01");
}
int totalRecords=0;
int totalPages=0;
List<SYS_RESPOND_LIST> detailData;
//查询数据库
detailData = mainRepository.GetSysRespondList(page, rows, name, url, type, dtTimeStart, dtTimeEnd, out totalRecords, out totalPages);
//组织jsGridJSON数据
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (from item in detailData
select new
{
cell = new string[]{
ConvUtils.ToString(item.name),
ConvUtils.ToString(item.url),
ConvUtils.ToString(ConvertType(item.type)),
ConvUtils.ToString(item.content),
ConvUtils.ToString(item.time.ToString("yyyy-MM-dd HH:mm"))
}
}).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
Model:
public List<SYS_RESPOND_LIST> GetSysRespondList(int pageIndex, int pageSize, string name, string url, string type, DateTime timeStart, DateTime timeEnd, out int totalRecords, out int totalPages)
{
pageIndex--;
timeEnd = timeEnd.AddDays(1);
totalRecords = context.SYS_RESPOND_LIST.Where( x => (string.IsNullOrEmpty(name) || x.name.ToUpper().Contains(name.ToUpper()))
&& (string.IsNullOrEmpty(url) || x.url.ToUpper().Contains(url.ToUpper()))
&& (string.IsNullOrEmpty(type) || x.type.ToUpper().Contains(type.ToUpper()))
&& x.time >= timeStart
&& x.time <= timeEnd
).Count();
totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
var list = context.SYS_RESPOND_LIST.Where(s => (string.IsNullOrEmpty(name) || s.name.ToUpper().Contains(name.ToUpper()))
&& (string.IsNullOrEmpty(url) || s.url.ToUpper().Contains(url.ToUpper()))
&& (string.IsNullOrEmpty(type) || s.type.ToUpper().Contains(type.ToUpper()))
&& s.time >= timeStart
&& s.time <= timeEnd).
OrderByDescending(s => s.time).
Skip(pageIndex * pageSize).
Take(pageSize).ToList();
return list.ToList();
}