In-depth explanation of Vue multi-select list component

In-depth explanation of Vue multi-select list component

A Multi-Select is a UI element that lists all options and allows users to make multiple selections using the Ctrl/Shift keys. This is a common design element. Sometimes, in order to save space, we will collapse the options into a Combo Box. To facilitate user operation, this component will also add two buttons, Select All and Clear All, allowing users to quickly select or clear the selection. This UI element was previously used in the Correlation Plot App.

Registering Components

Registering the Multi-Select component is as simple as copying and pasting the encapsulated code. It is recommended to register global components here.

This element was designed with two modes in mind: The first is that each click on a new option will keep the old options and add the new one, as shown in the example graphic above. This is a more common method. The second is that each time you click a new option, the old option is cleared and only the new option is retained. If you use this method to make multiple selections, you need to use the Ctrl/Shift key. This design makes it easy for users to eliminate old options with each click. If you use the second method, you need to replace the event @click.exact="AddToOrDeleteFromSelectedColumns" with @click.exact="ClickOnColumnListItem"

<script type="text/x-template" id="multi-select-template">
…
   <tr v-for = "(item,index) in columns" 
     @click.exact="AddToOrDeleteFromSelectedColumns"
     @click.shift.exact="AddMultipleToSelectedColumns"
     @click.ctrl.exact="AddToOrDeleteFromSelectedColumns"
     :title="function(item){if(item.longname){return 'Short Name: ' + item.name + '\n----------------\n' + item.longname}else{return item.name}}(item)"
     class="column-list-entry">
   ...
   </tr>
</script>
<script>
Vue.component("multi-select", {
  template: "#multi-select-template",
…
</script>

Calling Components

Add custom tags directly Call the component.

<multi-select :legend_name="legend_name"
       :columns="columns"
       :selected_columns="selected_columns"
       @update_selected_columns="onSelectedColumnsChange">
</multi-select>

Passing Data

Finally, data needs to be passed to the component. We can use v-bind to dynamically bind data to the data of the parent component.

In props, legend_name is bound to the name that Multi-Select needs to display, columns is bound to the options of Multi-Select, and selected_columns is bound to the currently selected item. In addition, we also need to define the event "onSelectedColumnsChange" in the parent instance to refresh the display of the Multi-Select component.

data: function(){
   return {
     legend_name: "Input Columns",
     columns: 
     [
       {"name":"A","longname":"Copper"},
       {"name":"B","longname":"Aluminum Aluminum"},
       {"name":"C","longname":"Calcium"},
       {"name":"D","longname":"Calcium"},
   ],
     selected_columns: [],
   }
 },
... 
methods:{ 
   onSelectedColumnsChange:function(new_columns) {
     this.selected_columns = new_columns;
   },
 },
...

This concludes this article on the in-depth explanation of the Vue multi-select list component. For more relevant Vue multi-select list component content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue Element front-end application development tree list component
  • Sample code of the tab-list category switching product list component based on Vue
  • Vue Element UI custom description list component

<<:  How to set Tomcat as an automatically started service? The quickest way

>>:  CentOS 6.5 i386 installation MySQL 5.7.18 detailed tutorial

Recommend

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

Teach you how to get the pointer position in javascript

The method of obtaining the position of the point...

Html/Css (the first must-read guide for beginners)

1. Understanding the meaning of web standards-Why...

HTML Basics: HTML Content Details

Let's start with the body: When viewing a web ...

Invalid solution when defining multiple class attributes in HTML

In the process of writing HTML, we often define mu...

Centos6.9 installation Mysql5.7.18 step record

Installation sequence rpm -ivh mysql-community-co...

MySQL EXPLAIN statement usage examples

Table of contents 1. Usage 2. Output results 1.id...

Why do we need Map when we already have Object in JavaScript?

Table of contents 1. Don’t treat objects as Maps ...

XHTML introductory tutorial: Use of list tags

Lists are used to list a series of similar or rela...

Nginx uses the Gzip algorithm to compress messages

What is HTTP Compression Sometimes, relatively la...

Use HTML and CSS to create your own warm man "Dabai"

The final result is like this, isn’t it cute… PS:...

Vue implements a complete process record of a single file component

Table of contents Preface Single file components ...

Docker-compose image release process analysis of springboot project

Introduction The Docker-Compose project is an off...

Example of how to implement value transfer between WeChat mini program pages

Passing values ​​between mini program pages Good ...

Docker exec executes multiple commands

The docker exec command can execute commands in a...