您是否可以推荐一个JQuery插件来组成一组可映射到SQL查询的条件? [关闭]

问题描述 投票:7回答:3

我已经找到http://redquerybuilder.appspot.com/,但是生成了我想避免的SQL客户端。在帽子页面上,有指向JQuery Query Builder插件的链接,但该链接转到jquery主页。似乎该插件不再存在(有关相同链接,另请参见Simple SQL Query Builder in JQuery)。

我发现http://kindohm.com/posts/2013/09/25/knockout-query-builder/看起来几乎是我想要的,除了我不想添加另一个JavaScript库。

最后有http://devtools.korzh.com/easyquery/javascript/docs/javascript-query-builder-php,看起来非常好。但是他们使用Web服务来生成SQL,并且您必须获得API密钥才能使其运行。现在它是免费的...但是它看起来像是吸引用户的好陷阱,然后当他们无法轻松离开时,它将开始为Web服务收费,或者可以在他们想要的任何时间将其关闭。

因此,在我构建定制的定制查询表单之前,是否存在这样的查询生成器?

jquery sql query-builder
3个回答
25
投票

我需要一个查询生成器,该生成器可以生成一个不错的JSON,我可以使用它创建Java POJO并编写以下代码:http://mistic100.github.io/jQuery-QueryBuilder

编写一个创建SQL查询的解析器很容易。


10
投票

我推荐Mistic's work。这种选择的优点:

  • 如果不使用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;
}

1
投票

这是您的答案。

请从这里下载

https://github.com/gantir/jsexpbuilder

您正在寻找。

© www.soinside.com 2019 - 2024. All rights reserved.