1. 后台代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using Contract.Domain;
using WebApp.Common;
namespace HraWeb.Common
{
/// <summary>
/// EntityJsonList 的摘要说明
/// </summary>
public class EntityJsonList : BasePage, IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public PropertyInfo GetProperyInfo(string Name, PropertyInfo[] ps)
{
PropertyInfo p1 = null;
ps.ToList().ForEach(p =>
{
if (p.Name == Name)
{
p1 = p;
}
});
return p1;
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
switch (context.Request["_method"])
{
case "entity":
string entityType = context.Request["entityType"];
info = new Framework.QueryInfo();
info.QueryObject = entityType;
if (!string.IsNullOrEmpty(context.Request["where"]))
{
info.Where.Add("kk", " and " + context.Request["where"]);
}
var keyword = context.Request["keyword"] ?? string.Empty;
if (!string.IsNullOrEmpty(keyword))
{
Hashtable aa=new Hashtable();
aa.Add(context.Request["queryTextFild"].ToString()+"_LK",keyword);
info.AddParam(aa);
//info.Where.Add("kk2",string.Format(" and {0} like '%:{1}%'", context.Request["queryTextFild"],keyword));
}
info.PageSize = int.Parse(HttpContext.Current.Request["rows"]);
int pageIndex = int.Parse(HttpContext.Current.Request["page"]);
info.StartRecord = (pageIndex - 1) * info.PageSize;
info.TotalCount = 1;
if (!string.IsNullOrEmpty(context.Request.QueryString["queryFild"]))
{
string[] field = context.Request.QueryString["queryFild"].Split(new char[] { '|' });
foreach (string q in field)
{
info.AddParam(q, context.Request.QueryString["q"], string.Format(" and {0} like '%:{0}%'", q));
}
}
info = Dao.FindByQueryInfo(info);
Type type = null;
PropertyInfo[] ps = null;
if (info.List != null && info.List.Count > 0)
{
type = info.List[0].GetType();
ps = type.GetProperties();
}
bool idBoolean = false;
foreach (var b in info.List)
{
PropertyInfo idFieldInfo = GetProperyInfo(context.Request["idFild"], ps);
string IdFieldValue = Convert.ToString(idFieldInfo.GetValue(b, null));
if (!string.IsNullOrEmpty(context.Request["defaultValue"]))
{
if (IdFieldValue.Equals(context.Request["defaultValue"]))
{
idBoolean = true;
break;
}
}
}
//if (!string.IsNullOrEmpty(context.Request["defaultValue"]))
{
Framework.QueryInfo tmpInfo = new Framework.QueryInfo();
tmpInfo.QueryObject = entityType;
if (!string.IsNullOrEmpty(context.Request["idFild"]))
tmpInfo.AddParam(context.Request["idFild"], context.Request["defaultValue"]);
var list = Dao.FindList(tmpInfo);
foreach (object obj in list)
{
if (!idBoolean)
{
info.List.Add(obj);
}
}
}
DataGridJson d = new DataGridJson(info.TotalCount, info.List);
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(d));
break;
case "dict":
var t = GetDictType(context.Request["dictType"]);
IList<SysDict> dictList= Holworth.Utility.Utility.ListToT<SysDict>(t);
//根据关键词使用linq进行二次过滤
var keyword2 = context.Request["keyword"] ?? string.Empty;
if (!string.IsNullOrEmpty(keyword2))
{
dictList = dictList.Where(x => x.Name.Contains(keyword2)).ToList();
}
string ss = Newtonsoft.Json.JsonConvert.SerializeObject(dictList);
context.Response.Write(ss);
break;
case "aa":
var ts = GetDictType(context.Request["dictType"]);
string sss = Newtonsoft.Json.JsonConvert.SerializeObject(ts);
context.Response.Write(sss);
break;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
2.下拉框 实体前台代码
this.SetEntityCombo = function (obj, entityType, textField, columns, where, idFild, queryFild) {
var url = '/common/EntityJsonList.ashx?entityType=' + entityType + '&_method=entity&tmp=' + Math.random() + '&queryTextFild=' + textField;
if (where) {
url += "&where=" + where;
}
if (obj.val() != "") {
url += "&defaultValue=" + obj.val();
}
var id = 'Id';
if (idFild) {
id = idFild;
}
url += "&idFild=" + id;
// if (queryFild) {
// url += "&queryFild=" + queryFild;
// }
obj.combogrid({
panelWidth: 500,
idField: id,
textField: textField,
url: url,
method: 'get',
delay:1000,
pagination: true, //是否分页
rownumbers: true, //序号
pageSize: 10, //每页显示的记录条数,默认为10
pageList: [10], //可以设置每页记录条数的列表
columns: columns,
striped: true,
editable: true,
collapsible: false, //是否可折叠的
fit: true, //自动大小
keyHandler: {
up: function () { //【向上键】押下处理
//取得选中行
var selected = obj.combogrid('grid').datagrid('getSelected');
if (selected) {
//取得选中行的rowIndex
var index = obj.combogrid('grid').datagrid('getRowIndex', selected);
//向上移动到第一行为止
if (index > 0) {
obj.combogrid('grid').datagrid('selectRow', index - 1);
}
} else {
var rows = obj.combogrid('grid').datagrid('getRows');
obj.combogrid('grid').datagrid('selectRow', rows.length - 1);
}
},
down: function () { //【向下键】押下处理
//取得选中行
var selected = obj.combogrid('grid').datagrid('getSelected');
if (selected) {
//取得选中行的rowIndex
var index = obj.combogrid('grid').datagrid('getRowIndex', selected);
//向下移动到当页最后一行为止
if (index < obj.combogrid('grid').datagrid('getData').rows.length - 1) {
obj.combogrid('grid').datagrid('selectRow', index + 1);
}
} else {
obj.combogrid('grid').datagrid('selectRow', 0);
}
},
enter: function () { //【回车键】押下处理
obj.combogrid('hidePanel');
},
query: function (keyword) { //【动态搜索】处理
//设置查询参数
var queryParams = obj.combogrid("grid").datagrid('options').queryParams;
queryParams.keyword = keyword;
obj.combogrid("grid").datagrid('options').queryParams = queryParams;
//重新加载
obj.combogrid("grid").datagrid("reload");
obj.combogrid("setValue", keyword);
}
},
mode: 'remote',
fitColumns: true
});
}
数据字典下拉框前台代码
this.SetDict = function (obj, dicType) {
obj.combogrid({
panelWidth: 250,
idField: 'Code',
textField: 'Name',
url: '/Common/EntityJsonList.ashx?_method=dict&dictType=' + dicType + "&tmp=" + Math.random()+'&queryTextFild=Name',
method: 'get',
delay: 1000,
columns: [[
{ field: 'Name', title: '名称', width: 100 },
{ field: 'Code', title: '编码', width: 100 }
]],
striped: true,
editable: true,
collapsible: false, //是否可折叠的
fit: true, //自动大小
keyHandler: {
up: function () { //【向上键】押下处理
//取得选中行
var selected = obj.combogrid('grid').datagrid('getSelected');
if (selected) {
//取得选中行的rowIndex
var index = obj.combogrid('grid').datagrid('getRowIndex', selected);
//向上移动到第一行为止
if (index > 0) {
obj.combogrid('grid').datagrid('selectRow', index - 1);
}
} else {
var rows = obj.combogrid('grid').datagrid('getRows');
obj.combogrid('grid').datagrid('selectRow', rows.length - 1);
}
},
down: function () { //【向下键】押下处理
//取得选中行
var selected = obj.combogrid('grid').datagrid('getSelected');
if (selected) {
//取得选中行的rowIndex
var index = obj.combogrid('grid').datagrid('getRowIndex', selected);
//向下移动到当页最后一行为止
if (index < obj.combogrid('grid').datagrid('getData').rows.length - 1) {
obj.combogrid('grid').datagrid('selectRow', index + 1);
}
} else {
obj.combogrid('grid').datagrid('selectRow', 0);
}
},
enter: function () { //【回车键】押下处理
obj.combogrid('hidePanel');
},
query: function (keyword) { //【动态搜索】处理
//设置查询参数
var queryParams = obj.combogrid("grid").datagrid('options').queryParams;
queryParams.keyword = keyword;
obj.combogrid("grid").datagrid('options').queryParams = queryParams;
//重新加载
obj.combogrid("grid").datagrid("reload");
obj.combogrid("setValue", keyword);
}
},
mode: 'remote',
fitColumns: true
});
}