QueryBuilder 前端构造SQL条件的插件使用方法
页面引入JS等:
<script type="text/javascript" src="/qysds-jx/pages/gzrw/js/jquery.js"></script>
<script type="text/javascript" src="/qysds-jx/pages/gzrw/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/qysds-jx/pages/gzrw/js/sql-parser.js"></script>
<script type="text/javascript" src="/qysds-jx/pages/gzrw/js/doT.js"></script>
<script type="text/javascript" src="/qysds-jx/pages/gzrw/js/jQuery.extendext.min.js"></script>
<script type="text/javascript" src="/qysds-jx/pages/gzrw/js/query-builder.js" charset="UTF-8"></script>
除query-builder.js外 其他均为依赖js
一般我们只需要使用两个按钮就好了。获取sql,重置。
<button class="btn btn-danger reset">Reset</button> <button class="btn btn-primary parse-sql" data-stmt="false">SQL</button>
在自己的js里面给这俩按钮绑定事件:
//绑定重置事件
$('.reset').on('click', function() {
$('#builder').queryBuilder('reset');
$('#result').addClass('hide').find('pre').empty();
}); //绑定生成sql事件
$('.parse-sql').on('click', function() {
var res = $('#builder').queryBuilder('getSQL', $(this).data('stmt'), false);
var SQL = res.sql + (res.params ? '\n\n' + JSON.stringify(res.params, undefined, 2) : '');
this.conditionSql = SQL;
$('#result').removeClass('hide').find('pre').html(SQL);
});
下面是以前做过的一个使用插件的Demo,看下就知道怎么使用了
/**
* 构造查询条件的入口在 insertFileds(array) 这个方法;
*
* 根据前面的数据格式 可改变参数的形式,具体数据对象的属性也可以变,
*
*/
$(document).ready(function() {
demo.init();
}); demo = {
el: null, //dom对象
builder: null, // sql解析器
conditionSql: null,
init: function() {
this.el = $('#builder');
if (Query.Builder) {
this.builder = new Query.Builder(this.el);
} demo.bindEvents();
demo.insertFileds();
/**
* 除上面的方法 还可以
*
* var option = this.builder.getOption();
*
* ... 对这个option的filter进行注入
*
* 然后 this.builder.render(option);
*/
},
/**
* 注入可选择的列
* @param {[type]} array [description]
* @return {[type]} [description]
*/
insertFileds: function(array) {
//临时造的假数据
var array = [{
"table": "wd_swjg",
"tableName": "税务机关表",
"field": "swjg_dm",
"mc": "税务机关代码",
"type": "string"
}, {
"table": "wd_yf",
"tableName": "维度月份表",
"field": "yf_id",
"mc": "月份代码",
"type": "string"
}, {
"table": "wd_yf",
"tableName": "维度月份表",
"field": "scjlrq",
"mc": "生成记录日期",
"type": "date"
}, {
"table": "qysds_szsb_2014hy",
"tableName": "企税年报主表",
"field": "zs",
"mc": "总数",
"type": "number"
}];
this.builder.addFilters(array);
this.builder.render();
},
/**
* 绑定页面事件
* @return {[type]} [description]
*/
bindEvents: function() { //绑定重置事件
$('.reset').on('click', function() {
$('#builder').queryBuilder('reset');
$('#result').addClass('hide').find('pre').empty();
}); //绑定生成sql事件
$('.parse-sql').on('click', function() {
var res = $('#builder').queryBuilder('getSQL', $(this).data('stmt'), false);
var SQL = res.sql + (res.params ? '\n\n' + JSON.stringify(res.params, undefined, 2) : '');
this.conditionSql = SQL;
$('#result').removeClass('hide').find('pre').html(SQL);
});
},
/**
* 获取查询条件sql
* @return {[type]} [description]
*/
getConditionSql: function() { return this.conditionSql;
} }
Query = {}; /**
* sql解析器
* @param {[type]} obj [description]
*/
Query.Builder = function(obj) {
this.el = obj;
this.init();
} Query.Builder.prototype = {
el: null,
option: null,
/**
* 目前支持的sql计算方式
* @type {Array}
*/
operators: [{
type: 'equal',
optgroup: 'basic'
}, {
type: 'not_equal',
optgroup: 'basic'
}, {
type: 'in',
optgroup: 'basic'
}, {
type: 'not_in',
optgroup: 'basic'
}, {
type: 'less',
optgroup: 'numbers'
}, {
type: 'less_or_equal',
optgroup: 'numbers'
}, {
type: 'greater',
optgroup: 'numbers'
}, {
type: 'greater_or_equal',
optgroup: 'numbers'
}, {
type: 'between',
optgroup: 'numbers'
}, {
type: 'not_between',
optgroup: 'numbers'
}, {
type: 'begins_with',
optgroup: 'strings'
}, {
type: 'not_begins_with',
optgroup: 'strings'
}, {
type: 'contains',
optgroup: 'strings'
}, {
type: 'not_contains',
optgroup: 'strings'
}, {
type: 'ends_with',
optgroup: 'strings'
}, {
type: 'not_ends_with',
optgroup: 'strings'
}, {
type: 'is_empty'
}, {
type: 'is_not_empty'
}, {
type: 'is_null'
}, {
type: 'is_not_null'
}],
init: function() {
this.initOption();
//this.render();
},
getOption: function() {
return this.option;
},
setOption: function(data) {
this.option = data;
return this;
},
/**
* 构造filter 根据具体的数据格式 改写此方法
* @param {[type]} array [description]
*/
addFilters: function(array) {
var tempArray = [];
for (var i = 0, j = array.length; i < j; i++) {
var obj = array[i];
var template = new Query.filter();
//这块可以前面构造的数据格式跟后面的一直就不需要挨个赋值了
template.id = obj.table + "." + obj.field;
template.label = obj.tableName + "." + obj.mc;
template.type = obj.type == "number" ? "double" : "string";
tempArray.push(template);
delete template;
}
if (tempArray.length > 0) {
this.option.filters = tempArray;
} },
/**
* 展现页面
* 支持自定义 option
* @param {[type]} option [description]
* @return {[type]} [description]
*/
render: function(option) {
//var option = option || this.option;
var option = $.extend({}, this.option, option);
this.el.queryBuilder(option);
},
/**
* 初始化 option 只有基本的属性
* @return {[type]} [description]
*/
initOption: function() {
this.option = {
allow_empty: true,
sort_filters: true,
plugins: {
'bt-tooltip-errors': {
delay: 100
},
'filter-description': {
mode: 'inline'
} //关于规则的描述插件 删除旁边蓝色的按钮 mode是显示的方式 },
operators: this.operators,
filters: [{
id: 'name'
}]
};
}
} /**
* 过滤器抽象出来的对象 后续属性可增加
* @type {Object}
*/
Query.filter = function() {
var filter = {
id: "", //不配置field 这个就是字段名字, 建议拼成 table.field
label: "", //下拉列表展现 建议拼成 表名-字段名(中文)
type: "", //目前就double 和 string 好了
value_separator: ',',
description: function(rule) {
if (rule.operator && ['in', 'not_in'].indexOf(rule.operator.type) !== -1) {
return '如果多个值,请使用\', \'分隔。';
}
}
}
return filter;
}
注: querybuilder 是不支持ie8的。 但是我们可以下下载谷歌内核,来安装到ie8
然后在页面指定使用谷歌内核来渲染页面即可。
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=IE8">
QueryBuilder 前端构造SQL条件的插件使用方法的更多相关文章
- PerfMap – 显示前端网站性能的热力图插件
PerfMap 是用来分析前端性能,基于资源定时 API 生成网页资源加载的前端性能热图.热图的颜色和第一个毫秒值指示在什么时候页面的图像加载完毕,这是用户体验的一个很好的指标.括号中的第二个值是浏览 ...
- Web前端开发实用的Chrome插件
Web前端开发实用的Chrome插件 越来越多的前端开发人员喜欢在Chrome里开发调试代码,Chrome有许多优秀的插件可以帮助前端开发人员极大的提高工作效率.尤其Chrome本身是可以登录的,登录 ...
- 帝国CMS系统标签e:loop调用的附加SQL条件和排序参数
帝国CMS6.5以上版本在原来所有信息调用标签基础上增加了两个标签参数:“附加SQL条件”和“显示排序”.支持这两个参数的标签有如下:ecmsinfo.灵动标签.phomenews.phomenews ...
- FastAdmin selectPage 前端传递查询条件
★夕狱-东莞 2018/2/2 16:19:33 selectpage 怎么在前端传递查询条件,看了下源码,好像有个custom,怎么用来的,比如我要下拉的时候,只显示id=1的数据 Karson-深 ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 根据List<SqlParameter>返回sql条件(where后)
/// <summary> /// 根据参数列表返回sql条件(where后) /// </summary> /// <param name="list&quo ...
- mysqls,为node.js而编写的sql语句生成插件 (crud for mysql).
It is written in JavaScript,crud for mysql.You can also use transactions very easily. mysqls 一款专为nod ...
- Web前端开发人员实用Chrome插件收集
越来越多的前端开发人员喜欢在Chrome里开发调试代码,Chrome有许多优秀的插件可以帮助前端开发人员极大的提高工作效率.尤其Chrome本身是可以登录的,登录后你所有的插件都会自动同步到每一个登录 ...
- 一个类搞定SQL条件映射解析,实现轻量简单实用ORM功能
个人觉得轻简级的ORM既要支持强类型编码,又要有执行效率,还要通俗易懂给开发者友好提示,结合Expression可轻松定制自己所需要功能. Orm成品开源项目地址https://github.com/ ...
随机推荐
- 安装STS报错(三)
安装STS报错 1.具体报错如下 Failure to transfer org.codehaus.plexus:plexus-archiver:jar:1.2 from http://repo.ma ...
- Java中的“&”和“&&”的区别
Java中的"&"和"&&"的区别 1."&"是位运算符,"&&"是逻辑 ...
- Caused by: java.lang.ClassNotFoundException: org.jbpm.pvm.internal.processengine.SpringHelper
1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...
- ORA-00936: missing expression
1.错误描述 Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as scott@ORC ...
- .Net Core从命令行读取配置文件
最近在学习博客园腾飞(jesse)的.Net Core视频教程,收益匪浅,在此作推荐 : http://video.jessetalk.cn/ 言归正传,.Net Core应用程序中如何通过命令行读取 ...
- jQuery.proxy()的用法
一:参考范文一 第一次接触jQuery.proxy()时感觉这个方法不实用,不明白它到底是个什么意思.今天来将jQuery官网上的解释进行一下翻译,顺便添加自己的理解和一些示例.proxy也可称为代理 ...
- httpclient案例二(调用百度地图的接口)
一,class T package com.http.test; import org.junit.Test; import com.http.BaiduMapProxyService; import ...
- Windows DLL资料整理
1.使用Visual C++ 6.0创建dll 2. 函数的调用规则(__cdecl,__stdcall,__fastcall,__pascal) 要点: 1. 如果你的程序中没有涉及可变参数,最好使 ...
- C#多线程编程(1)--线程,线程池和Task
新开了一个多线程编程系列,该系列主要讲解C#中的多线程编程. 利用多线程的目的有2个: 一是防止UI线程被耗时的程序占用,导致界面卡顿:二是能够利用多核CPU的资源,提高运行效率. 我没有进行很 ...
- XCTF(77777-2)
题目链接:http://47.52.137.90:20000 这道题目和前面的那道题目大致一样,只不过是过滤的函数不一样 检查过滤函数的方式就不写了,直接来解题 检查函数发现过滤了ord ascii ...