JQGrid 导出Excel 获取筛选条件

需求描述:页面加载后,进行相关数据搜索,要求点击导出按钮后  下载Excel文件。

思路:希望在点击【导出Excel】按钮时,获取到表格搜索时的filters内容。

在百度、api、jqgrid.js中翻阅后,找到解决属性postData

解决办法:$(window.frames[0].document).find("#grid-table").jqGrid("getGridParam","postData").filters

 

后台对filters转义的方法

namespace Common
{
    /// <summary>
    /// 公共方法类
    /// </summary>
    public class PublicFun
    {
        /// <summary>
        /// 解析查询条件拼接成sql
        /// </summary>
        /// <param name="feilterArr">条件对象数组</param>
        /// <returns></returns>
        public static string GetFileterStr(string feilterArr)
        {
            //动态条件
            string searchCase = string.Empty;
            if (feilterArr==null||feilterArr.Equals(""))
            {
                return "";    
            }  
            var mStream = new MemoryStream(Encoding.UTF8.GetBytes(feilterArr));     //使用Encoding.Default中文时将会乱码
            DataContractJsonSerializer dcjson = new DataContractJsonSerializer(typeof(jqGridFilter));
            jqGridFilter filters = (jqGridFilter)dcjson.ReadObject(mStream);
            if (filters.GroupOp == "AND")
            {
                searchCase += " 1=1 ";
                foreach (jqGridFilterRules rules in filters.JqGridFilterRulesList)
                {
                    switch (rules.Op)
                    {
                        case "cn":
                            searchCase += " and " + rules.Field + " like '%" + rules.Data + "%'";
                            break;
                        case "nc":
                            searchCase += " and " + rules.Field + " not like '%" + rules.Data + "%'";
                            break;
                        case "gt":
                            searchCase += " and " + rules.Field + ">'" + rules.Data + "'";
                            break;
                        case "ge":
                            searchCase += " and " + rules.Field + ">='" + rules.Data + "'";
                            break;
                        case "lt":
                            searchCase += " and " + rules.Field + "<'" + rules.Data + "'";
                            break;
                        case "le":
                            searchCase += " and " + rules.Field + "<='" + rules.Data + "'";
                            break;
                        case "eq":
                            searchCase += " and " + rules.Field + "='" + rules.Data + "'";
                            break;
                        case "true":
                            searchCase += " and " + rules.Field + " like '%" + rules.Data + "%'";
                            break;
                        case "ne":
                            searchCase += " and " + rules.Field + "!='" + rules.Data + "'";
                            break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                searchCase += " and (1=1";
                foreach (jqGridFilterRules rules in filters.JqGridFilterRulesList)
                {
                    switch (rules.Op)
                    {
                        case "cn":
                            searchCase += " or " + rules.Field + " like '%" + rules.Data + "%'";
                            break;
                        case "nc":
                            searchCase += " or " + rules.Field + " not like '%" + rules.Data + "%'";
                            break;
                        case "gt":
                            searchCase += " or " + rules.Field + ">'" + rules.Data + "'";
                            break;
                        case "ge":
                            searchCase += " or " + rules.Field + ">='" + rules.Data + "'";
                            break;
                        case "lt":
                            searchCase += " or " + rules.Field + "<'" + rules.Data + "'";
                            break;
                        case "le":
                            searchCase += " or " + rules.Field + "<='" + rules.Data + "'";
                            break;
                        case "eq":
                            searchCase += " or " + rules.Field + "='" + rules.Data + "'";
                            break;
                        case "true":
                            searchCase += " or " + rules.Field + "='" + rules.Data + "'";
                            break;
                        case "ne":
                            searchCase += " or " + rules.Field + "!='" + rules.Data + "'";
                            break;
                        default:
                            break;
                    }
                }
                searchCase += ")";
            }
            return searchCase;
        }

      
    }
    [DataContract]
    class jqGridFilter
    {
        //    {"groupOp":"AND","rules":[{"field":"email","op":"cn","data":"1"},{"field":"orderno","op":"ge","data":"2"}]}
        private string groupOp = "AND";
        private List<jqGridFilterRules> jqGridFilterRulesList;
        [DataMember(Name = "groupOp")]
        public string GroupOp
        {
            get { return groupOp; }
            set { groupOp = value; }
        }
        [DataMember(Name = "rules")]
        public List<jqGridFilterRules> JqGridFilterRulesList
        {
            get { return jqGridFilterRulesList; }
            set { jqGridFilterRulesList = value; }
        }
    }

    [DataContract]
    class jqGridFilterRules
    {
        private string field;
        private string op;
        private string data;
        [DataMember(Name = "field")]
        public string Field
        {
            get { return field; }
            set { field = value; }
        }
        [DataMember(Name = "op")]
        public string Op
        {
            get { return op; }
            set { op = value; }
        }
        [DataMember(Name = "data")]
        public string Data
        {
            get { return data; }
            set { data = value; }
        }
    }
}
View Code

注:若使用上述方法解析filters

需将filters拼装成      {"groupOp":"AND","rules":[{"field":"ID","op":"true","data":"20170907"}]} 

拼装的字符串中必须使用    英文的双引号   "

加一个js拼装的方法

        var filList = JSON.parse($("#grid-table").jqGrid("getGridParam", "postData").filters).rules;
        var fil = '{"groupOp":"AND","rules":[';
        for (var i = 0, len = filList.length; i < len; i++) {
            if (i != 0) { fil += ','; }
            fil += '{"field":"' + filList[i].field + '","op":"' + filList[i].op + '","data":"' + filList[i].data + '"}';
        }
        fil += ']}';
View Code

 

本人使用的是C#,至于后台的Excel导出可查看我的相关博客

posted @ 2017-09-25 17:49  听雨的人  阅读(1225)  评论(0编辑  收藏  举报