- Vue.js 2.x by Example
- Mike Street
- 385字
- 2021-07-02 20:00:27
Binding the inputs
To bind inputs to a variable that can be accessed through your Vue instance requires an HTML attribute to be added to the fields and a corresponding key added to the data object. Create a variable in the data object for each of the fields so we can bind the form elements to them:
data: {
people: [...],
currency: '$',
filterField: '',
filterQuery: '',
filterUserState: ''
}
The data object now has three additional keys: filterField, which will be used for storing the value of the dropdown; filterQuery, the placeholder for data entered into the textbox; and filterUserState, which allows us to store the radio button checkboxes.
Now there are data keys to utilize, we are able to bind form elements to them. Apply a v-model="" attribute to each form field, with the value of the data key.
Here's an example:
<input type="text" id="filterQuery" v-model="filterQuery">
Make sure the two radio buttons have exactly the same v-model="" attribute: this is so they can update the same value. To verify that it has worked, you can now output the data variables and get the value of the fields.
Try outputting filterField or filterQuery and changing the fields.
{{ filterField }}
One thing you may notice if you were to output the filterUserState variable is it appears to be in working, But, it is not getting the actual results desired. The output of the variable would be true and falseas set in the value attributes.
On closer inspection, the values are actually strings, rather than a Boolean value. A Boolean value is a hard true or false, 1 or 0, which you can easily compare against, whereas a string would require exact checking on a hardcoded string. This can be verified by outputting the typeof variable that it is:
{{ typeof filterUserState }}
This can be resolved by binding the values of the radio buttons with the v-bind:value attribute. This attribute allows you to specify the value for Vue to interpret and can take Boolean, string, or object values. For now, we'll pass it true and false, as we were already doing with the standard value attribute, but Vue will know to interpret it as Boolean:
<span>
Active:
<label for="userStateActive">
Yes:
<input type="radio" v-bind:value="true" id="userStateActive"
v-model="filterUserState" selected>
</label>
<label for="userStateInactive">
No:
<input type="radio" v-bind:value="false"
id="userStateInactive" v-model="filterUserState">
</label>
</span>
The next step is to show and hide the table rows based on these filters.
- Puppet 4 Essentials(Second Edition)
- Getting Started with React
- CentOS 7 Server Deployment Cookbook
- Learning AWS Lumberyard Game Development
- 基于差分進(jìn)化的優(yōu)化方法及應(yīng)用
- C語言程序設(shè)計(jì)立體化案例教程
- 精通Scrapy網(wǎng)絡(luò)爬蟲
- Windows Phone 7.5:Building Location-aware Applications
- C#應(yīng)用程序設(shè)計(jì)教程
- OpenCV 3計(jì)算機(jī)視覺:Python語言實(shí)現(xiàn)(原書第2版)
- 零基礎(chǔ)學(xué)C++(升級(jí)版)
- R的極客理想:量化投資篇
- Python機(jī)器學(xué)習(xí)與量化投資
- SQL Server 2012 數(shù)據(jù)庫應(yīng)用教程(第3版)
- Puppet 5 Beginner's Guide(Third Edition)