This is a migrated thread and some comments may be shown as answers.

data-bind visible - multiple conditions

3 Answers 3261 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Gregg
Top achievements
Rank 1
Gregg asked on 17 Jan 2018, 12:26 PM

Is it possible to data-bind the visible property in MVVM to multiple conditions, either by using logical operators (&&) or calling a function that accepts parameters?

<div id='test' data-bind='visible: condition1 && condition2'>...</div>

or

<div id='test' data-bind='visible: myShowFunction('a', 'b')'>...</div>

 

 

 

3 Answers, 1 is accepted

Sort by
1
Veselin Tsvetanov
Telerik team
answered on 19 Jan 2018, 07:49 AM
Hello Gregg,

There are two approaches, that might be applicable in the described scenario. First, if you need to pass parameters from the markup to the â€‹myShowFunction, you should pass them concatenated in a single string
<div id="test" data-bind="visible: myShowFunction('falseqtrue')">Test visibility</div>

Then you could split and evaluate them:
var viewModel = kendo.observable({
  myShowFunction: function(values) {
    var array = values.split('q');
    var a = (array[0] == 'true');
    var b = (array[1] == 'true');
    return a && b
  }
});
 
kendo.bind('body', viewModel);

Alternatively, if the conditions are part of the ViewModel, you could evaluate them in simple function bound to the visible option:
<div id="test" data-bind="visible: testConditions">Test visibility</div>

and:
var viewModel = kendo.observable({
  myShowFunction: function(values) {
    var array = values.split('q');
    var a = (array[0] == 'true');
    var b = (array[1] == 'true');
    return a && b
  }
});
 
kendo.bind('body', viewModel);

Regards,
Veselin Tsvetanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Clint
Top achievements
Rank 1
answered on 11 Feb 2018, 04:50 PM
Did you mean to post the "myShowFunction" example twice?  I'm guessing your "testConditions" binding is different.  Would you mind posting that example?
0
Dimitar
Telerik team
answered on 13 Feb 2018, 02:07 PM
Hello Clint,

The second approach that my colleague Veselin suggested is to use a simple function to evaluate ViewModel fields:
<div data-bind="visible: visibleTextBox">
  <label for="amount">Textbox 1: </label>
  // ...
</div>
 
<script>
  var viewModel = kendo.observable({   
    selectedOption: 1,
    visibleTextBox: function() {
      var selectedOptionValue = this.get('selectedOption');

      if(selectedOptionValue == 3) {
        return true;
      } else {           
        return false;
      }
    }
  });
 
  kendo.bind($("body"), viewModel);
</script>

You can review the following Dojo example, where the above approach is demonstrated.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
MVVM
Asked by
Gregg
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Clint
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or