renvrant / conditionize.js

Small jQuery plugins for handling conditional form fields via data attributes. Unmaintained.
MIT License
53 stars 30 forks source link

Multiple values data-cond-value #5

Closed nannione closed 6 years ago

nannione commented 8 years ago

Hello, is there a way to handle multiple values like

data-cond-value="color1;color2;color3"

? If use a select field and want to execute a condition with any of those values. So if data-cond-value is "color1" OR "color2" OR "color3" the conditionize function is executed.

I tried

var arrayData = $(this).data('cond-value');
var arr = arrayData.split(';');
for (i = 0; i < arr.length; i++) {
// console.log(arr[i]);
var listenFor = (arr[i]);
}

but then only the last value of the array is considered.

christianmagill commented 8 years ago

You need something more along these lines...

However, there's a lot of other decisions that have to be made such as AND/OR scenarios and operators when you get into multiple values.

   $.fn.eval = function(valueIs, valueShould, operator) {
      var valuesShould = valueShould.split(';');
      var i;
      switch(operator) {
        case '==':
            for(i = 0; i < valuesShould.length; i++){
              if(valueIs == valuesShould[i]){
                return true;
              }
            }
            return false;
christianmagill commented 8 years ago

That being said, I'd really like to see this plugin fleshed out.

jasondavis commented 8 years ago

I was about to ask this same question and then saw this post. I also require some way to make a conditional field show if any field in a set of fields is true.

I will be playing around with this, if I come up with anything solid I will post it back here or do a pull request. For now I will start with what @christianmagill posted above, thanks for that!

Also if anyone else has modified in such a manner already, please do share!

I'm loving the simplicity this library brings to conditionals, it's great

christianmagill commented 8 years ago

@jasondavis No worries, I've since moved on to Visibly that has handled my needs well.

https://github.com/DanielRivers/jQuery-Visibly

jasondavis commented 8 years ago

@christianmagill THanks I actually posted too soon as I just realized this is opossite of what I need right now. THis is for multiple values and I need to make it where a field will show when any of a set of other fields is shown.

For example

.conditional should be shown when checkbox name foo or foo3 is checked/on

So I need to be able to set multiple items in this part data-cond-option="foo" like data-cond-option="foo;foo2"

<label for="foo"><input name="foo" type="checkbox"> Foo</label>
<label for="foo2"><input name="foo2" type="checkbox"> Foo2</label>
<label for="foo3"><input name="foo3" type="checkbox"> Foo3</label>

<div class="conditional" data-cond-option="foo" data-cond-value="on">
  Bar
</div>  

Would you happen to have any ideas on a quick solution for that similar to the other one you posted on the values?

I will check out the library you linked as well thanks

jasondavis commented 8 years ago

@christianmagill I think the library you linked is what I will use, regex and multiple options, thats awesome and its also light-weight thanks for sharing I actually already had it bookmarked but forgot I ever seen it even so very glad you shared!

rguliev commented 7 years ago

Hi, Please take a look at flexible version of the plugin: conditionize.flexible.jquery.js.
For your case it must look like this:

<div class="conditional" data-condition="foo == 'on' || foo2 == 'on' || foo3 == 'on'">...</div>

The rest is the same.

rguliev commented 6 years ago

@nannione , can we close the issue?