JQGrid 导出Excel 获取筛选条件

Posted 听雨的人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

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

以上是关于JQGrid 导出Excel 获取筛选条件的主要内容,如果未能解决你的问题,请参考以下文章

Excel 如何将筛选功能下拉框里的数据导出来

导出数据到Excel表格

excel表中,怎样筛选出多个条件的数据

jqgrid 导出到 Excel 路线图?

jqgrid 表格中筛选条件的多选下拉,树形下拉 ;文本框清除插件;高级查询多条件动态筛选插件

导出到excel时如何从jqGrid中排除summaryType