你能推荐一个 JQuery 插件来组成一组可映射到 SQL 查询的条件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22189565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could you recommend a JQuery plugin to compose a set of conditions mappable to a SQL query?
提问by beginner_
I've found http://redquerybuilder.appspot.com/but that generates SQL client side which I want to avoid. On hat page there is a link to JQuery Query Builder plugin but that link goes to jquery home page. Seems that this plugin does nto exist anymore (also see Simple SQL Query Builder in JQueryfor same link).
我找到了http://redquerybuilder.appspot.com/但这会生成我想避免的 SQL 客户端。在帽子页面上有一个指向 JQuery Query Builder 插件的链接,但该链接指向 jquery 主页。似乎这个插件不再存在(另见JQuery 中的 Simple SQL Query Builder 以获取相同的链接)。
I found http://kindohm.com/posts/2013/09/25/knockout-query-builder/which looks pretty much what I want except I do not want to add yet another JavaScript library.
我发现http://kindohm.com/posts/2013/09/25/knockout-query-builder/看起来几乎是我想要的,除了我不想添加另一个 JavaScript 库。
Last there is http://devtools.korzh.com/easyquery/javascript/docs/javascript-query-builder-phpwhich looks very nice. But they use a web service to generate SQL and you have to get an API key for it to work. For now it's free...but looks like a nice trap to lure in users and then when they can't easily get away, the will probably start to charge for the web service or can shut it down any time they want.
最后是http://devtools.korzh.com/easyquery/javascript/docs/javascript-query-builder-php,它看起来很不错。但是他们使用 Web 服务来生成 SQL,您必须获得 API 密钥才能使其工作。目前它是免费的……但看起来是一个很好的陷阱来吸引用户,然后当他们无法轻易逃脱时,他们可能会开始对 Web 服务收费,或者可以随时关闭它。
So before I just build a custom tailored query form, does such a query builder exist at all?
因此,在我构建自定义的定制查询表单之前,是否存在这样的查询构建器?
回答by Mistic
I needed a query builder which generates a nice JSON I could use to create Java POJO and wrote this :
http://mistic100.github.io/jQuery-QueryBuilder
我需要一个查询生成器来生成一个很好的 JSON,我可以用它来创建 Java POJO 并写了这个:http:
//mistic100.github.io/jQuery-QueryBuilder
It would be easy to write a parser which create SQL queries.
编写一个创建 SQL 查询的解析器会很容易。
回答by Chris
I recommend Mistic's work. Pros of this choice:
我推荐Mistic 的作品。这种选择的优点:
- if you don't use Bootstrap, you can always extract the only classes used by the plugin and merge them in query.builder.css, modifing them as you need it.
- I've tested it with other plugins with no problem like jquery MultiSelect and jquery TimePicker
- there's an option to disable subgroups. if you want only a two level structure (no subgroups of subgroups), you can use an event to hide the group button after creating a new group rule.
- You can easily parse JSON in PHP. Put the case you call $('#builder').builder('getRules') in your client code and you assign the result to a variable c, which you'll post as you want:
- 如果你不使用 Bootstrap,你总是可以提取插件使用的唯一类并将它们合并到 query.builder.css 中,根据需要修改它们。
- 我已经用其他插件测试过它,没有问题,比如 jquery MultiSelect 和 jquery TimePicker
- 有一个选项可以禁用子组。如果您只想要一个二级结构(没有子组的子组),您可以在创建新组规则后使用事件隐藏组按钮。
- 您可以轻松地在 PHP 中解析 JSON。将您调用 $('#builder').builder('getRules') 的案例放在客户端代码中,然后将结果分配给变量 c,您将根据需要发布该变量:
$operators = array('equal' => "=",
'not_equal' => "!=",
'in' => "IN (?)",
'not_in' => "NOT IN (_REP_)",
'less' => "<",
'less_or_equal' => "<=",
'greater' => ">",
'greater_or_equal' => ">=",
'begins_with' => "ILIKE",
'not_begins_with' => "NOT ILIKE",
'contains' => "ILIKE",
'not_contains' => "NOT ILIKE",
'ends_with' => "ILIKE",
'not_ends_with' => "NOT ILIKE",
'is_empty' => "=''",
'is_not_empty' => "!=''",
'is_null' => "IS NULL",
'is_not_null' => "IS NOT NULL");
$jsonResult = array("data" => array());
$getAllResults = false;
$conditions = null;
$result = "";
$params = array();
$conditions = json_decode(utf8_encode($_POST['c']), true);
if(!array_key_exists('condition', $conditions)) {
$getAllResults = true;
} else {
$global_bool_operator = $conditions['condition'];
// i contatori servono per evitare di ripetere l'operatore booleano
// alla fine del ciclo se non ci sono più condizioni
$counter = 0;
$total = count($conditions['rules']);
foreach($conditions['rules'] as $index => $rule) {
if(array_key_exists('condition', $rule)) {
$result .= parseGroup($rule, $params);
$total--;
if($counter < $total)
$result .= " $global_bool_operator ";
} else {
$result .= parseRule($rule, $params);
$total--;
if($counter < $total)
$result .= " $global_bool_operator ";
}
}
}
/**
* Parse a group of conditions */
function parseGroup($rule, &$param) {
$parseResult = "(";
$bool_operator = $rule['condition'];
// counters to avoid boolean operator at the end of the cycle
// if there are no more conditions
$counter = 0;
$total = count($rule['rules']);
foreach($rule['rules'] as $i => $r) {
if(array_key_exists('condition', $r)) {
$parseResult .= "\n".parseGroup($r, $param);
} else {
$parseResult .= parseRule($r, $param);
$total--;
if($counter < $total)
$parseResult .= " ".$bool_operator." ";
}
}
return $parseResult.")";
}
/**
* Parsing of a single condition */
function parseRule($rule, &$param) {
global $fields, $operators;
$parseResult = "";
$parseResult .= $fields[$rule['id']]." ";
if(isLikeOp($rule['operator'])) {
$parseResult .= setLike($rule['operator'], $rule['value'], $param);
} else {
$param[] = array($rule['type'][0] => $rule['value']);
$parseResult .= $operators[$rule['operator']]." ?";
}
return $parseResult;
}
回答by Bhavik Chauhan
Here is your answer.
这是你的答案。
Please download from Here
请从这里下载
https://github.com/gantir/jsexpbuilder
https://github.com/gantir/jsexpbuilder
Which you looking for.
你要找的。