Flexigrid系列使用教程(三)增加外部查询
Flexigrid系列使用教程(三)增加外部查询
?????? Flexigrid有内置的内部查询功能,但是很多时候需要将查询条件放到flexigrid控件的外面使用,本文实现的效果如下所示: ?? ?????? 问题所在:本人在调试时使用flexigrid的flexReload方法,但是在提交时不知道为啥flexigrid自动刷新了两次,第一次刷新按自己 设置的条件查询成功,但是紧接着就是第二次刷新使flexigrid又重新加载了一次,导致没有得到想要的查询结果,最后使用session暂时解决了这 个问题,使用session记录了第一次刷新的查询条件,第二次刷新时自动加载session里面保存的条件。(有知道如何解决这个问题的朋友告诉一下 QQ:346933996) 废话不多说具体代码如下: 一、? 前台页面代码 ???<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/QyKwstu.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Kwstu.Model.TALENT_BASEINFO>>" %> ? <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> ??? <script type="text/javascript"> ??????? $(document).ready(function () { ??????????? //按回车键实现按tab键的效果,方便录入查询条件 ??????????? $('#PONAME').focus(); ??????????? var $inp = $('input:text'); ??????????? $inp.bind('keydown',function (e) { ??????????????? var key = e.which; ??????????????? if (key == 13) { ??????????????????? $('#requery').click(); ??????????????????? e.preventDefault(); ??????????????????? var nxtIdx = $inp.index(this) + 1; ??????????????????? $(":input:text:eq(" + nxtIdx + ")").focus(); ??????????????? } ??????????? }); ??????????? //全屏浏览查询结果 ??????????? function toolbarItem_onclick(cmd,pid) { ??????????????? var Height = document.documentElement.clientHeight - 60; ??????????????? var Width = document.documentElement.clientWidth - 40; ??????????????? if (cmd == "FullScreen") { ??????????????????? JqueryDialog.Open('简历查询结果浏览','../FindTalent/FullScreen',Width,Height); ??????????????? } ??????????????? else if (cmd == "Delete") { ??????????????????? alert("cmd Delete is excuted"); ??????????????? } ??????????? } ??????????? //重新定义表格大小 ??????????? $(window).resize(function () { ??????????????? var w = $("#content_right").width() - 5; ??????????????? var gh = $("#content_right").height() - 86; ? ??????????????? $("#grdProList").flexResize(w,gh); ? ??????????? }); ??????????? //时间关系查询暂时用这个(主要方法) ??????????? $("#requery").click(function () { doquery() }); ??????????? function doquery() { ??????????????? var PONAME = $("#PONAME").val(); ??????????????? var JOBTYPE1 = $("#JOBTYPE1X").val(); ??????????????? params = { 'PONAME': PONAME,'JOBTYPE1X': JOBTYPE1 }; ??????????????? $.post( ??????? "/FindTalent/GetByPositionFindFlex", ??????? params, ??????? function () { $('#grdProList').flexReload(); } ??? ); ??????????? } ? ??????????? var maiheight = document.documentElement.clientHeight; ??????????? var w = $("#content_right").width() - 20; ??????????? var gh = $("#content_right").height() - 86; ??????????? $("#grdProList").flexigrid ????????? ({ ????????????? url: '/FindTalent/GetByPositionFindFlex', ????????????? dataType: 'json', ????????????? colModel: [ ?????????????????? { display: '序号',name: '',sortable: true,width: 20,align: 'center' }, ?????????????????? { display: '用户姓名',name: 'REALNAME',width: 60, ?????????????????? { display: '性',name: 'SEX',width: 25, ?????????????????? { display: '年龄',name: 'AGE', ?????????????????? { display: '最高学历',name: 'TOPESTEDU',width: 43, ?????????????????? { display: '最后访问时间',name: 'LASTVT',width: 110,align: 'center' } ?????????????? ???], ????????????? buttons: [ ????????????????? { name: 'FullScreen',displayname: "<span style=color:red;font-weight:bold>点ì?击??全屏查看搜索结果</span>",bclass: 'Add',onpress: toolbarItem_onclick } ????????????????? ], ????????????? ShowToggleCol: true, ????????????? singleSelect: true, ????????????? sortname: 'LASTVT', ????????????? sortorder: 'DESC', ????????????? usepager: true, ????????????? striped: true, ????????????? resizable: true, ????????????? title: '人才简历列表', ????????????? useRp: true, ????????????? rp: 20, ????????????? rpOptions: [10,20,40,100], ????????????? showTableToggleBtn: true, ????????????? showcheckbox: false, ????????????? width: w, ????????????? height: gh, ????????????? pagestat: '显示 {from} 到 {to},共2 {total} 条', ????????????? procmsg: '请等待数据正在加载中…', ????????????? nomsg: '没有数据', ????????????? onError: '查询出错,请刷新' ????????? }); ??????? });??????????????????? ??? </script> ??? <div class="right_title" id="baseinfo"> ??????? 请输入查询条件</div> ??? <div id="baseinfo_content" class="right_table"> ??????? <div class="right_table_td" style="width: 200px"> ??????????? <div class="editor-label" style="width: 100px"> ??????????????? 应聘岗位</div> ??????????? <div class="editor-field" style="width: 100px"> ??????????????? <%=Html.TextBox("PONAME")%></div> ??????? </div> ??????? <div class="right_table_td" style="width: 200px"> ??????????? <div class="editor-label" style="width: 100px"> ??????????????? 职位类别1:</div> ??????????? <div class="editor-field" style="width: 100px"> ??????????????? <%=Html.TextBox("JOBTYPE1X")%></div> ??????? </div> ??? </div> ??? <table id="grdProList" style="display: none;"> ??? </table> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="title" runat="server"> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentLeft" runat="server"> </asp:Content> <asp:Content ID="Content4" ContentPlaceHolderID="topmenu" runat="server"> ??? <input type="submit" id="requery" value="执行查询" class="button" /> ??? <img src="../../Content/Images/new.gif" /> </asp:Content> <asp:Content ID="Content5" ContentPlaceHolderID="bottemmenu" runat="server"> </asp:Content> 二、? 后台处理代码 ????? ??public ActionResult ByPositionFindTalent() ??????? { ??????????? Session["QUERRY"] = " and CVVISIBLE ='是o?' and 1=1 and LASTVT is not null "; ??????????? return View(); ??????? } ??????? public String GetByPositionFindFlex() ??????? { ??????????? int page = 1; ??????????? if (HttpContext.Request.Form["page"] != null) ??????????? { ??????????????? page = int.Parse(HttpContext.Request.Form["page"].ToString()); ??????????? } ??????????? int rp = 1; ??????????? if (HttpContext.Request.Form["rp"] != null) ??????????? { ??????????????? rp = int.Parse(HttpContext.Request.Form["rp"].ToString()); ??????????? } ??????????? string sortname = ""; ??????????? if (HttpContext.Request.Form["sortname"] != null) ??????????? { ??????????????? sortname = HttpContext.Request.Form["sortname"].ToString(); ??????????? } ??????????? //自定义搜索条件(关键实现部分) ??????????? string define = ""; ??????????? string logic = "and"; ??????????? string tmp1 = HttpContext.Request.Form["PONAME"] == null ? "" : HttpContext.Request.Form["PONAME"]; ??????????? string tmp2 = HttpContext.Request.Form["JOBTYPE1X"] == null ? "" : HttpContext.Request.Form["JOBTYPE1X"]; ????????? ??????????? define = (define == "" ? (tmp1 == "" ? "" : (" PONAME like '%" + tmp1 + "%' or JOBTYPE1 like '%" + tmp1 + "%'")) : (tmp1 == "" ? define : (define + " " + logic + "? PONAME like '%" + tmp1 + "%' or JOBTYPE1 like '%"+tmp1+"%'"))); ??????????? define = (define == "" ? (tmp2 == "" ? "" : (" JOBTYPE1 like '%" + tmp2 + "%' or PONAME like '%" + tmp2 + "%'")) : (tmp2 == "" ? define : (define + " " + logic + " JOBTYPE1 like '%" + tmp2 + "%' or PONAME like '%" + tmp2 + "%'"))); ??????? ??????????? if (define != "") ?????????????? ?Session["define"] = " and (" + define + ")"; ??????????? string whereCondition = " and CVVISIBLE ='是' and 1=1 and LASTVT is not null "; ??????????? if (Session["define"] != null) ??????????? { ??????????????? whereCondition += (Session["define"] == null ? "" : Session["define"].ToString()); ??????????? } ??????????? string sortorder = ""; ??????????? if (HttpContext.Request.Form["sortorder"] != null) ??????????? { ??????????????? sortorder = HttpContext.Request.Form["sortorder"].ToString(); ??????????? } ??????????? string tablename = "TALENT_BASEINFO"; ??????????? if (HttpContext.Request.Form["tablename"] != null) ??????????? { ??????????????? tablename = HttpContext.Request.Form["tablename"].ToString(); ??????????? } ??????????? string sortExp = sortname + " " + sortorder; ??????????? int start = ((page - 1) * rp) + 1; ??????????? DataSet ds = new DataSet(); int total = 0; ??????????? TALENT_BASEINFODAL _dal = new TALENT_BASEINFODAL(); ??????????? string fields = "ID,REALNAME,SEX,AGE,TOPESTEDU,FIRSTREAL,SECONDREAL,WORKAGE,PONAME,JOBTYPE1,LASTVT"; ??????????? Session["QUERRY"] = whereCondition; ??????????? ds = _dal.GetListByPage(tablename,whereCondition,fields,sortname,sortorder,page,rp,out total); ??????????? return GetJsonForByPositionFind(ds.Tables[0],total); ??????? } ??????? //生成json数据 ??????? public string GetJsonForByPositionFind(DataTable dt,int page,int total) ??????? { ??????????? StringBuilder jsonBuilder = new StringBuilder(); ??????????? jsonBuilder.Append("{"); ? ??????????? jsonBuilder.Append(""page":" + page.ToString() + ","total":" + total.ToString() + ","rows":["); ??????????? for (int i = 0; i < dt.Rows.Count; i++) ??????????? { ??????????????? jsonBuilder.Append("{"); ??????????????? for (int j = 0; j < dt.Columns.Count; j++) ??????????????? { ??????????????????? if (j == 0) ??????????????????? { ??????????????????????? jsonBuilder.Append(""id":""); ??????????????????????? jsonBuilder.Append(tools.JsonStringFormat(dt.Rows[i][j].ToString())); ??????????????????????? jsonBuilder.Append("","cell":["); ??????????????????????? jsonBuilder.Append("""); ??????????????????????? jsonBuilder.Append(i + 1); ??????????????????????? jsonBuilder.Append("","); ????????????? ??????} ??????????????????? else ??????????????????? { ??????????????????????? if (j == 1) ??????????????????????? { ??????????????????????????? jsonBuilder.Append("""); ??????????????????????????? jsonBuilder.Append("<a onclick="JqueryDialog.Open('浏?¥览¤¨¤简¨°历¤¨2','../CommonInfo/TalentInfo?talentid=" + tools.JsonStringFormat(dt.Rows[i]["ID"].ToString()) + "',1100,580)"? target='_blank'><font color=blue>" + tools.JsonStringFormat(dt.Rows[i][j].ToString()) + "</font></a>"); ???????????????????????? ???jsonBuilder.Append("","); ??????????????????????? } ??????????????????????? else ??????????????????????? { ??????????????????????????? jsonBuilder.Append("""); ??????????????????????????? jsonBuilder.Append(tools.JsonStringFormat(dt.Rows[i][j].ToString())); ??????????????????????????? jsonBuilder.Append("","); ??????????????????????? } ??????????????????? } ??????????????? } ??????????????? jsonBuilder.Remove(jsonBuilder.Length - 1,1); ??????????????? jsonBuilder.Append("],"); ??????????????? jsonBuilder.Remove(jsonBuilder.Length - 1,1); ??????????????? jsonBuilder.Append("},"); ??????????? } ??????????? if (total > 0) ??????????? { ??????????????? jsonBuilder.Remove(jsonBuilder.Length - 1,1); ??????????? } ??????????? jsonBuilder.Append("]"); ??????????? jsonBuilder.Append("}"); ??????????? return jsonBuilder.ToString(); ??????? } 出处:http://www.kwstu.com/ArticleView/lydia_20121031201137406 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |