getRules does not return number value
I implemented a comparison variable-to-variable starting with code from https://codepen.io/harveyl888/pen/mOejgN
I am almost completed but I am currently stuck on a weird case where calling
conditions = JSON.stringify($('#Qbuilder').queryBuilder('getRules', { get_flags: true }), undefined, 2);
misses a value.
For example I have a condition with 2 rules: var1=1 AND var2=var3 It displays QueryBuilder fine. If don't do any change and submit the change, the code above returns:
"{
"condition": "AND",
"rules": [
{
"id": "var1",
"field": "var1",
"type": "double",
"operator": "equal",
"value": ""
},
{
"id": "var2",
"field": "var2",
"type": "boolean",
"operator": "equal_",
"value": "var3"
}
],
"not": false,
"valid": true
}"
Now if I display the same condition but I change the value 1 to 2 and then back to 1 and submit the changes I get this:
"{
"condition": "AND",
"rules": [
{
"id": "var1",
"field": "var1",
"type": "double",
"operator": "equal",
"value": "1"
},
{
"id": "var2",
"field": "var2",
"type": "boolean",
"operator": "equal_",
"value": "var3"
}
],
"not": false,
"valid": true
}"
I checked the DOM between these 2 cases and I cannot find any difference. If DOM is the same, where is value 1 coming from in the second case? Why don't I get the same on the first case?
Full disclosure: Here is the important part of the code I use. I know the use of timeouts is awful but I did not time yet to correctly fix these parts. QueryBuilder is displayed in a jQuery modal dialog, if it makes any difference.
<head>
<style>
.radio-Green.ui-state-active {
background: #5cb85c;
border: 0;
}
.radio-Green.ui-state-active:hover {
background: #449c44;
border: 0;
}
.radio-Red.ui-state-active {
background: #d9534f;
border: 0;
}
.radio-Red.ui-state-active:hover {
background: #c9302c;
border: 0;
}
.Action-container {
width: 100%;
background: rgba(250,240,210,.5);
border: 1px solid #DCC896;
padding: 10px 10px 6px;
}
.btn.btn-xs.btn-default {
font-size: 9pt;
}
.table tbody > tr > td {
border-top: 0;
}
.form-control {
max-width:200px;
}
</style>
</head>
<div id="Trigger-dialog" title="Trigger" class="widget" style="display: none;">
<form id="Triggerfrm" name="Triggerfrm" method="post">
<input type="hidden" id="TrigAction" value="" />
<input type="hidden" id="TriggerKey" value="" />
<fieldset id="FieldCondition" style="margin-top: 10px;">
<label for="Qbuilder">Condition</label>
<div id="Qbuilder"></div>
<span style="font-weight: bold">Formula:</span> <span id="sql"></span>
</fieldset>
<table style="width:100%; margin-top: 10px;">
<tr><td style="text-align:center"><input type="button" value="OK" id="TriggerOKBtn" onclick="ProcessTrigger();" class="btnOK" /> <input type="button" value="Cancel" onclick="CloseTriggerDialog();" style="padding-right: 20px; padding-left: 20px;" /></td></tr>
</table>
</form>
</div>
<script type="text/javascript">
var options = {
valueNames: ['name', 'vars']
};
var userList = new List('triggers', options);
var VarList = GetVariables();
var TryingToSave = false;
var timeout;
var boolinit = false;
$(document).ready(function () {
$.fn.bootstrapBtn = $.fn.button.noConflict();
});
function refreshFormula()
{
if (boolinit)
{
var formula = GetFormula();
$("#sql").text(formula);
}
}
function GetFormula()
{
var result = $('#Qbuilder').queryBuilder('getRules');
var conditions = JSON.stringify($('#Qbuilder').queryBuilder('getRules', { get_flags: true }), undefined, 2);
var formula = '';
$.ajax({
type: "POST",
url: "/Trigger/GetFormula",
async: false,
data: {
'Conditions': conditions
},
success: function (data) {
formula = data;
}
});
return formula;
}
function GetVariables()
{
var vars = '';
$.ajax({
type: "POST",
url: "/Variable/GetVariables",
async: false,
success: function (data) {
vars = data;
$.each(vars, function (e, v) {
v.valueGetter = function (rule) { return QueryGetValue(rule); };
v.validation.callback = function (value, rule) { return QueryValidation(value, rule); };
if (v.type == 'double')
{
v.input = function (rule, name) { return QueryInput(rule, name, 'double') };
}
else if (v.type == 'boolean')
{
v.input = function (rule, name) { return QueryInput(rule, name, 'boolean') };
}
else if (v.type == 'string')
{
v.input = function (rule, name) { return QueryInput(rule, name, 'string') };
}
});
}
});
return vars;
}
function QBuilderInit()
{
boolinit = false;
$('#Qbuilder').queryBuilder({
plugins: [
'sortable',
'bt-tooltip-errors',
'not-group'
],
filters: [{
id: 'name',
label: 'Name',
type: 'string'
}],
operators: [
{ type: 'equal', optgroup: 'Scalar', apply_to: ['boolean', 'number', 'string'] },
{ type: 'not_equal', optgroup: 'Scalar', apply_to: ['number', 'string'] },
{ type: 'less', optgroup: 'Scalar', apply_to: ['number'] },
{ type: 'less_or_equal', optgroup: 'Scalar', apply_to: ['number'] },
{ type: 'greater', optgroup: 'Scalar', apply_to: ['number'] },
{ type: 'greater_or_equal', optgroup: 'Scalar', apply_to: ['number'] },
{ type: 'between', optgroup: 'Scalar', apply_to: ['number'] },
{ type: 'not_between', optgroup: 'Scalar', apply_to: ['number'] },
{ type: 'begins_with', optgroup: 'Scalar', apply_to: ['string'] },
{ type: 'not_begins_with', optgroup: 'Scalar', apply_to: ['string'] },
{ type: 'ends_with', optgroup: 'Scalar', apply_to: ['string'] },
{ type: 'not_ends_with', optgroup: 'Scalar', apply_to: ['string'] },
{ type: 'contains', optgroup: 'Scalar', apply_to: ['string'] },
{ type: 'not_contains', optgroup: 'Scalar', apply_to: ['string'] },
{ type: 'is_empty', optgroup: 'Scalar', apply_to: ['string'] },
{ type: 'is_not_empty', optgroup: 'Scalar', apply_to: ['string'] },
{ type: 'equal_', optgroup: 'Variable', nb_inputs: 1, apply_to: ['boolean', 'number', 'string'] },
{ type: 'not_equal_', optgroup: 'Variable', nb_inputs: 1, apply_to: ['boolean', 'number', 'string'] },
{ type: 'greater_', optgroup: 'Variable', nb_inputs: 1, apply_to: ['number'] },
{ type: 'less_', optgroup: 'Variable', nb_inputs: 1, apply_to: ['number'] },
{ type: 'less_or_equal_', optgroup: 'Variable', nb_inputs: 1, apply_to: ['number'] },
{ type: 'greater_or_equal_', optgroup: 'Variable', nb_inputs: 1, apply_to: ['number'] }
],
lang: {
operators: {
equal_: 'equal',
not_equal_: 'not equal',
greater_: 'greater than',
greater_or_equal_: 'greater or equal',
less_: 'less than',
less_or_equal_: 'less or equal'
}
}
});
if (VarList.length > 0)
$('#Qbuilder').queryBuilder('setFilters', VarList);
$('#Qbuilder').on('afterUpdateRuleOperator.queryBuilder', function (e, rule) {
// If we can have GroupOps then we need to determine the type of operator and display the correct input
if (rule.$el.find('.rule-operator-container .form-control optgroup[label=Variable]').html() != null)
{
var myOptGroup = rule.$el.find('.rule-operator-container .form-control option:selected').closest('optgroup').attr('label');
var prevOptGroup = rule.$el.find('.rule-value-container').attr('data-Group');
if (myOptGroup == 'Variable')
{
rule.$el.find('.rule-value-container .value-select').css("display", "inline");
rule.$el.find('.rule-value-container .value-text').css("display", "none");
if (prevOptGroup == 'Variable')
rule.$el.find('.rule-value-container').find('.value-text')[0].value = '';
}
else
{
rule.$el.find('.rule-value-container .value-select').css("display", "none");
rule.$el.find('.rule-value-container .value-text').css("display", "inline");
setTimeout(function () {
if (prevOptGroup == 'Variable')
{
rule.$el.find('.rule-value-container').find('.value-text')[0].value = '';
rule.$el.find('.rule-value-container').attr('data-value', '');
$("#sql").text('');
}
rule.$el.find('.rule-value-container').find('.value-select').val('');
}, 0);
}
rule.$el.find('.rule-value-container').attr('data-Group', myOptGroup);
}
setTimeout(function () {
refreshFormula();
}, 700);
})
$('#Qbuilder').on('validationError.queryBuilder', function (e, rule, error, value) {
if (!TryingToSave)
e.preventDefault();
});
$('#Qbuilder').on('afterUpdateRuleValue.queryBuilder afterDeleteRule.queryBuilder afterUpdateRuleFilter.queryBuilder afterUpdateGroupCondition.queryBuilder afterInvert.queryBuilder afterUpdateRuleOperator.queryBuilder afterUpdateRuleFilter.queryBuilder afterApplyRuleFlags.queryBuilder afterUpdateGroupNot.queryBuilder afterMove.queryBuilder',
function (e, rule, error, value) {
});
}
function SetQBuilderEvents(formula)
{
setTimeout(function () {
$('#Qbuilder').on('afterUpdateRuleValue.queryBuilder afterDeleteRule.queryBuilder afterUpdateRuleFilter.queryBuilder afterUpdateGroupCondition.queryBuilder afterInvert.queryBuilder afterUpdateRuleFilter.queryBuilder afterApplyRuleFlags.queryBuilder afterUpdateGroupNot.queryBuilder afterMove.queryBuilder', function (e, rule, error, value) {
setTimeout(function () {
refreshFormula();
}, 200);
});
$("#sql").text(formula);
$("input[type=radio][name^=Qbuilder_rule_]").checkboxradio();
}, 500);
$('#Qbuilder').on('afterSetRules.queryBuilder', function (e, rule, error, value) {
$("input[type=radio][name^=Qbuilder_rule_]").checkboxradio();
});
setTimeout(function () {
boolinit = true;
}, 1000);
}
function EditTrigger(triggerKey)
{
$('#TriggerOKBtn')[0].value = 'Update';
$('#TriggerKey')[0].value = triggerKey;
$('#TrigAction')[0].value = 'Update';
$("#Trigger-dialog").dialog({
height: "auto",
width: "850",
resizable: false,
modal: true
}).bind('dialogclose', function () {
CloseTriggerDialog();
}).dialog('option', 'title', 'Edit Trigger');
$("#sql").text('');
QBuilderInit();
$("#sql").text('');
TryingToSave = false;
$.ajax({
type: "POST",
url: "/Trigger/GetInfo",
data: { TriggerKey: triggerKey },
async: true,
success: function (data) {
if (data != "")
{
$('#Qbuilder').queryBuilder('setRules', jQuery.parseJSON(data.conditions));
$("input[type=radio][name^=Qbuilder_rule_]").checkboxradio();
}
else // probably a session timeout. Reload the page should redirect to the login page
{
window.location.reload(false);
}
SetQBuilderEvents(data.Formula);
}
});
}
function CloseTriggerDialog()
{
$('#Qbuilder').queryBuilder('destroy');
$('#Trigger-dialog').closest('.ui-dialog-content').dialog('close');
}
function ProcessTrigger()
{
conditions = JSON.stringify($('#Qbuilder').queryBuilder('getRules', { get_flags: true }), undefined, 2);
$.ajax({
type: "POST",
url: "/Trigger/Update",
data: {
'TriggerKey': $('#TriggerKey')[0].value,
'Conditions': conditions
},
datatype: "json",
traditional: true,
async: true,
success: function (data) {
if (data.length == 2 && data[0] != '')
alert(data[0]);
else if (data.length == 2)
{
$('#Trigger-dialog').closest('.ui-dialog-content').dialog('close');
}
}
});
}
function QueryInput(rule, name, type)
{
var optionValues = '<option disabled selected value>---</option>';
VarList.forEach(function (i) {
if (i.type == type)
optionValues += '<option value="' + i.variableKey + '">' + i.id + '</option>';
});
$operator_container = rule.$el.find('.rule-operator-container');
var myOptGroup = rule.$el.find('.rule-operator-container .form-control option:selected').closest('optgroup').attr('label');
var show_select = (myOptGroup == 'Variable');
var show_text = !show_select
var htmlOut = '';
htmlOut += '<select name=\"' + name + '\" class="form-control value-select"' + (show_text ? ' style="display: none;"' : '') + ' onchange="data_from_select(this)"' + '>';
htmlOut += optionValues;
htmlOut += '</select>';
if (type == 'boolean')
{
htmlOut += '<label for=\"' + name + '-True\" class="form-control value-text radio-Green"><input type="radio" name=\"' + name + '\" id=\"' + name + '-True\"' + (show_select ? ' style="display: none;"' : '') + ' value="True" onclick="data_from_radio(this);"> True</label> <label for=\"' + name + '-False\" class="form-control value-text radio-Red"><input type="radio" name=\"' + name + '\" id=\"' + name + '-False\"' + (show_select ? ' style="display: none;"' : '') + ' value="False" onclick="data_from_radio(this);"> False</label>';
setTimeout(function () {
$("input[type=radio][name^=Qbuilder_rule_]").checkboxradio();
}, 500);
}
else if (type == 'string')
htmlOut += '<input name=\"' + name + '\" class="form-control value-text" type="text"' + (show_select ? ' style="display: none;"' : '') + ' onchange="data_from_text(this);" onkeyup="data_from_text(this);"' + ' />';
else
htmlOut += '<input name=\"' + name + '\" class="form-control value-text" type="number"' + (show_select ? ' style="display: none;"' : '') + ' onchange="data_from_text(this);" onkeyup="data_from_text(this);"' + ' />';
return htmlOut;
}
function QueryValidation(value, rule)
{
return true;
}
function QueryGetValue(rule)
{
return rule.$el.find('.rule-value-container').attr('data-value');
}
function data_from_radio(id)
{
if ($(id)[0].id.substring($(id)[0].id.length-4) == 'True')
$(id).parent().parent().attr('data-value', 'True');
else
$(id).parent().parent().attr('data-value', 'False');
}
function data_from_select(id)
{
$(id).parent().attr('data-value', $(id).find(":selected").val());
}
function data_from_text(id)
{
$(id).parent().attr('data-value', $(id).val());
}
</script>
There is too much code tied with your backend for debuging. Setup a minimal standalone example that reproduces the problem.
You're right. May be that's too intense. I'll try to make a more condensed example.
In the mean time, I'm trying to understand the mechanics of getRules and how with the same DOM I get a different result. It must be something I don't set.
In my code function QueryInput is responsible of displaying the content of rule-value-container, which it seems to do correctly. Visually it's OK, until I call getRules.
Can you try to point me in a direction where to look at? Why would getRules not get the correct value when it appears in the DOM. See attached file.

Thank you!
Getrules does not export the content of the Dom but the content of the internal model. The model is updated on change events on each select/input. If you change a value without triggering a change event, the model is outdated.
Got it: fixed by triggering a change event on the input right after it's displayed. Not sure it's the best solution but it works.
Merci!
Got it: fixed by triggering a change event on the input right after it's displayed. Not sure it's the best solution but it works.
Merci!
Did you use jQuery to trigger the change event?
Yes: jQuery. It's been a long time. If I'm not mistaken, the code below helped fix my problem. It's defined right after creating the queryBuilder.
$('#Qbuilder').on('afterCreateRuleFilters.queryBuilder', function
(e, rule) { filterCreated(e, rule); })
$('#Qbuilder').on('afterUpdateRuleOperator.queryBuilder', function
(e, rule) { ruleUpdated(e, rule); })
Paul
https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Garanti sans virus. www.avast.com https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
On Tue, Jun 9, 2020 at 7:19 AM PIlepRusel [email protected] wrote:
Got it: fixed by triggering a change event on the input right after it's displayed. Not sure it's the best solution but it works.
Merci!
Did you use jQuery to trigger the change event?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mistic100/jQuery-QueryBuilder/issues/503#issuecomment-641224106, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG257RAO4HADFAPVDEZZRATRVYLARANCNFSM4DNL5QAQ .
Yes: jQuery. It's been a long time. If I'm not mistaken, the code below helped fix my problem. It's defined right after creating the queryBuilder. $('#Qbuilder').on('afterCreateRuleFilters.queryBuilder', function (e, rule) { filterCreated(e, rule); }) $('#Qbuilder').on('afterUpdateRuleOperator.queryBuilder', function (e, rule) { ruleUpdated(e, rule); }) Paul https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Garanti sans virus. www.avast.com https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> … On Tue, Jun 9, 2020 at 7:19 AM PIlepRusel @.***> wrote: Got it: fixed by triggering a change event on the input right after it's displayed. Not sure it's the best solution but it works. Merci! Did you use jQuery to trigger the change event? — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <#503 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG257RAO4HADFAPVDEZZRATRVYLARANCNFSM4DNL5QAQ .
Thanks.