//查询参数设置 var loadListDefaultSettings = { url: "getList", gridId: "dataGrid", queryWindowId: "queryWindow", queryBoxId: "queryBox", searchFields: "Name", addQueryString: true, prompt: "请输入" }; 5、可能需要使用的工具方法tool.js //判断是否包含地址栏参数 function hasQueryString(key, url) { if (typeof (url) == "undefined") url = window.location.search; var re = new RegExp("[?&]" + key + "=([^\\&]*)", "i"); var a = re.exec(url); if (a == null) return false; return true; } //获取地址栏参数,如果参数不存在则返回空字符串 function getQueryString(key, url) { if (typeof (url) == "undefined") url = window.location.search; var re = new RegExp("[?&]" + key + "=([^\\&]*)", "i"); var a = re.exec(url); if (a == null) return ""; return a[1]; } //将当前地址栏参数加入到url function addUrlSearch(url) { var newParams = []; var paramKeys = window.location.search.replace('?', '').split('&'); for (var i = 0; i < paramKeys.length; i++) { var key = paramKeys[i].split('=')[0]; if (key == "" || key == "_t" || key == "_winid" || key == "isTab") continue; if (!hasQueryString(key, url)) newParams.push(paramKeys[i]); } if (url.indexOf('?') >= 0) return url + "&" + newParams.join('&'); else return url + "?" + newParams.join('&'); } //url增加参数 function addSearch(url, key, value) { if (!hasQueryString(key, url)) { if (url.indexOf('?') >= 0) return url + "&" + key + "=" + value; else return url + "?" + key + "=" + value; } else return url; } //获取数组中对象的某个值,逗号分隔 function getValues(rows, attr) { var fieldValues = []; for (var i = 0; i < rows.length; i++) { if (rows[i] != null) fieldValues.push(rows[i][attr]); } return fieldValues.join(','); }
6、可能需要使用的私有方法private.js,此文件包含的方法专供base.js使用,开发web时禁止使用
//替换掉Url中的{}参数 function replaceUrl(settings, pty) { if (!pty) pty = "url"; if (!settings[pty]) return; var str = settings[pty]; var guid = ""; var result = str.replace(/\{[0-9a-zA-Z_]*\}/g, function (e) { var key = e.substring(1, e.length - 1); if (key == "GUID") { if (!guid) { $.ajax({ url: "getGuid", type: "post", async: false, success: function (text, textStatus) { guid = text; } }); } return guid; } if (hasQueryString(key)) //从地址栏返回 return getQueryString(key); if (settings.currentRow && settings.currentRow[key])//从当前行返回 return settings.currentRow[key]; if (settings.paramFrom) { //从指定控件返回 var ctrl = mini.get(settings.paramFrom); if (ctrl == undefined) { $.messager.alert({ title: 'UI出错', msg: "Id为" + settings.paramFrom + "的控件不存在!", icon: "error" }); return; } else if (ctrl.showCheckBox) { return getValues(ctrl.getCheckedNodes(), key); } else if (ctrl.getSelecteds) return getValues(ctrl.getSelecteds(), key); else if (ctrl.getValue) return ctrl.getValue(); } return e; }); settings[pty] = result; return result; } //转化为全路径 function changeToFullUrl(settings) { var url = settings.url; if (url.indexOf('https://www.jb51.net/') == 0 || url.indexOf("http://") == 0 || url.indexOf('?') == 0 || url == "") return url; currentUrlPathName = window.location.pathname; var currentPathNameParts = currentUrlPathName.split('https://www.jb51.net/'); var pathNameParts = url.split('?')[0].split('https://www.jb51.net/'); if (currentPathNameParts[currentPathNameParts.length - 1] == "") currentPathNameParts.pop(); //去掉一个反斜线 if (pathNameParts[pathNameParts.length - 1] == "") pathNameParts.pop(); //去掉一个反斜线 var index = currentPathNameParts.length - 1; for (var i = 0; i < pathNameParts.length; i++) { if (pathNameParts[i] == "..") { index = index - 1; if (index <= 0) { $.messager.alert({ title: "系统异常", msg: "Url错误:" + url + "!", icon: "error" }); return; } continue; } if (index < currentPathNameParts.length) currentPathNameParts[index] = pathNameParts[i]; else currentPathNameParts.push(pathNameParts[i]); index = index + 1; } var length = currentPathNameParts.length; for (var i = index; i < length; i++) { currentPathNameParts.pop(); } var result = currentPathNameParts.join('https://www.jb51.net/'); if (url.indexOf('?') > 0) result += url.substring(url.indexOf('?')); settings.url = result; }
我们期望简洁的后台代码,如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using formula; using System.Data; namespace demo.Areas.basic.Controllers { public class customerController : BaseController { public JsonResult getList(QueryBuilder qb) { SqlHelper sqlHelper = new SqlHelper("demo"); var data = sqlHelper.ExecuteGridData("select *,id=customerId from customer", qb); return Json(data); } } }
为了这种简洁的代码我们需要:
1、Controller基类BaseController: