jQuery-QueryBuilder icon indicating copy to clipboard operation
jQuery-QueryBuilder copied to clipboard

Hook dynamic element's change event into query builder?

Open trunker76 opened this issue 6 years ago • 5 comments

Heya,

I have a rule operator that requires one text input, and one select list. The way I'm handling it is manually modifying the rule.$el and replacing the html with my select list (via afterUpdateRuleOperator.queryBuilder):

// Remove datepickers
valueDOM = rule.$el.find('div.rule-value-container input');
valueDOM.datepicker('destroy');
valueDOM.removeClass('hasDatepicker');

// Replace second input with select list
nameAttribute = $(valueDOM[1]).attr('name');
withinValueSelectDOM = withinValueSelectDOM.replace('<select class="form-control">', '<select class="form-control" name="' + nameAttribute + '">');
$(valueDOM[1]).replaceWith(withinValueSelectDOM);

Because it's being dynamically added this way it appears that all of the default querybuilder events tied to it are lost.

Updating rule.value inside the change event appears to create an infinite loop (I've read this is so that you can handle plugins):

$(valueDOM).change(function () {
    rule.value = $(this).val();
});

I have also looked at the documentation for change.queryBuilder, but it's not clear to me how it is intended.

What I would like to do is simply hook the change event for my dynamic input into querybuilder, so it will trigger my valueGetter function just like a standard input. What is the best way to do this?

Any help would be much appreciated!

trunker76 avatar Jul 15 '20 20:07 trunker76

first of all why don't you use the input property of the filter to provide you custom input ? instead of manually replacing the DOM
https://github.com/mistic100/jQuery-QueryBuilder/blob/dev/examples/index.html#L460-L503

mistic100 avatar Jul 20 '20 10:07 mistic100

Thanks Mistic, that was helpful. However it appears that the filter input function is only fired when the number of filter inputs changes.

So if for example I switch between an operator that has 1 input and another that has 1 input, it is not triggered. I can call the input function again through the afterUpdateRuleOperator.queryBuilder event, but the html returned is not rendered to the DOM that way.

What would you suggest as the best way to handle this? Is there an event I can manually trigger to get the input function to fire as normal?

trunker76 avatar Jul 24 '20 14:07 trunker76

In that case afterUpdateRuleOperator.queryBuilder is the way to go. Unfortunately I cannot give a proper answer to your initial question.

mistic100 avatar Jul 24 '20 15:07 mistic100

Check the various issues linked to to #284

mistic100 avatar Jul 24 '20 15:07 mistic100

I guess my other option would be to use an input dropdown to replace my custom operators, similar to the example you gave me. For my purposes that might actually work, since what I'm defining in my project are the source of values to compare against. I'll gave that route a go and close this out.

Thanks again Mistic.

trunker76 avatar Jul 24 '20 16:07 trunker76