A Textbox with Dropdown allows users to select an input from a drop-down list or type the input value freely. This is a relatively common UI element. It can provide users with alternative options to save operation time, and it can also provide adaptation possibilities for possible minority situations. I originally thought that this component is quite common and there should be many ready-made examples that can be directly applied. However, after searching around, I found that many similar components have too many functions, such as search, multiple selection, etc. (In short: too complicated!). So I thought I should write a simple and easy-to-use one myself. I would like to thank Mr. Fei for his great help when I was confused. This UI element will be used in the Common Bar Width App. Registering ComponentsRegister the global component by copying and pasting the encapsulated component code. When designing, it was considered that there may be different types of input boxes, such as text input boxes, numeric input boxes, percentage input boxes, etc. Therefore, the <script type="text/x-template" id="dropdown"> <div class="dropdown" v-if="options"> <!-- Dropdown Input --> <input :type="type" :disabled="disabled" v-model="input_value" @focus="showOptions()" @blur="exit()" @keyup="keyMonitor" @input="input_value = inputRule(type)" /> ... </script> <script> Vue.component('dropdown', { template: '#dropdown', props: { type: String, options: Array, disabled: Boolean, value: String }, ... methods: { inputRule:function(type){ var value; switch(type){ case 'text': value = this.input_value.replace(/[^a-zA-Z0-9]/g,''); break; case 'number': value = this.input_value.replace(/^(?![+-]?\d+(\.\d+)?$)/g,''); break; case 'percentage': value = this.input_value.replace(/[^\d]/g,''); value = value > 100 ? '100' : value; value = value < 0 ? '0' : value; break; default: console.log("no limitation"); } return value; }, ... </script> Calling ComponentsAdd a custom label calling component. <dropdown type = "text" :options = "text_options" :value = "text_value" :disabled = "text_disabled" @on_change_input_value = "onTextChange"> </dropdown> Passing DataFinally, dynamically bind the data to the parent component in props:
In addition, we also need to define things in the parent instance to update the value of the input box on_change_input_value: Update value data: function () { return { text_value: 'ccc', text_disabled: false, text_options: [ { id: 1, name: 'a' }, { id: 2, name: 'bb' }, { id: 3, name: 'ccc' }, { id: 4, name: 'dddd' }, { id: 5, name: 'eeeee' }, { id: 6, name: 'fffff ' }, { id: 7, name: 'gggggg' }, { id: 8, name: 'hhhhhhh' }, { id: 9, name: 'iiiiiiii' }, ], ... } }, ... methods: { onTextChange: function (new_text_value) { this.text_value = new_text_value; }, ... }, source code GitHub This is the end of this article about the Vue.js Textbox with Dropdown component. For more information about the Vue.js Textbox with Dropdown component, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Centos 7.4 server time synchronization configuration method [based on NTP service]
>>: In-depth understanding of the matching logic of Server and Location in Nginx
Generally, during Qingming Festival, the National...
Exposing network ports In fact, there are two par...
The following functions are implemented: 1. Usern...
Table of contents Vue life cycle introduction and...
CSS3 achieves cool 3D rotation perspective 3D ani...
Table of contents 1. Some points to remember 1. V...
Many friends who have just started to make web pag...
mysql permissions and indexes The highest user of...
Today I found that a program inserted an incorrec...
Since I need to learn how to build servers and da...
This article shares the specific code of Javascri...
background The Agile model is widely used, and te...
The complete code is as follows : HTML code: Copy ...
Preface This article mainly introduces the releva...
Linux grep command The Linux grep command is used...