官术网_书友最值得收藏!

Filtering our filters

Now we have our filtering in place, we need to only show the radio buttons when the isActive option is selected in our dropdown. Using the knowledge we've learned, this should be relatively straightforward.

Create a new method that checks the select box value and returns true when Active User is selected in our dropdown:

      isActiveFilterSelected() {
return (this.filterField === 'isActive');
}

We can now use v-show for both the input and radio buttons, reversing the effect when on the query box:

      <label for="filterQuery" v-show="!isActiveFilterSelected()">
Query:
<input type="text" id="filterQuery" v-model="filterQuery">
</label>
<span v-show="isActiveFilterSelected()">
Active:
<label for="userStateActive">
Yes:
<input type="radio" v-bind:value="true" id="userStateActive"
v-model="filterUserState">
</label>
<label for="userStateInactive">
No:
<input type="radio" v-bind:value="false" id="userStateInactive" v-
model="filterUserState">
</label>
</span>

Take note of the exclamation point before the method call on the input field. This means not, and effectively reverses the result of the function, for example not true is the same as false and vice versa.

To improve user experience, we can also check that the filtering is active at all before showing either of the inputs. This can be added by including a secondary check in our v-show attribute:

      <label for="filterQuery" v-show="this.filterField &&        
!isActiveFilterSelected()">
Query:
<input type="text" id="filterQuery" v-model="filterQuery">
</label>

This now checks that filterField has a value and that the select box is not set to isActive. Make sure you add this to the radio buttons too.

A further user experience enhancement would be to ensure all the users don't disappear when the isActive option is chosen. This currently happens because the default is set to a string, which does not match with either the true or false values of the field. Before filtering in this field, we should check that the filterUserState variable is either true or false, that is a Boolean. We can do this by using typeof once more:

      if(this.filterField === 'isActive') {
result = (typeof this.filterUserState === 'boolean') ?
(this.filterUserState === person.isActive) : true;

}

We are using a ternary operator to check that the result to filter on is boolean. If it is, then filter as we were; if it is not then simply show the row.

主站蜘蛛池模板: 肥乡县| 陆川县| 昌宁县| 同德县| 武乡县| 西青区| 丰宁| 峨眉山市| 彝良县| 青川县| 舒兰市| 金昌市| 宜昌市| 响水县| 子洲县| 德江县| 丹凤县| 天台县| 乃东县| 肥城市| 柞水县| 天峻县| 海门市| 宣恩县| 怀柔区| 泗洪县| 法库县| 丹阳市| 平湖市| 凉城县| 上杭县| 兴化市| 盱眙县| 桃园市| 定西市| 习水县| 南平市| 平定县| 巴楚县| 大足县| 东乡县|