1. Scenario Description I don’t know if you have had this experience before. There are many options in the drop-down box, tens of thousands of options or even more. At this time, if you put all the data into the drop-down box and render it, the browser will freeze and the experience will be particularly bad. Some users may say that element-ui's select has a remote-method that supports remote search. Can't we just ask the server to support it? Of course, this is a solution. But sometimes this method may not be applicable (1) Sometimes the server-side data is returned to us after calculation, and the return may not be very fast, so the experience is not very good. (2) Sometimes there may be only a few thousand pieces of data, and it is not appropriate to render all of them. It is not very good to keep the interface down. (3) Can it be solved only by the front-end? If it can be solved, wouldn’t it reduce the work and pressure on the server? 2. Solution 1) Segmented loading: The drop-down items are not loaded. When you click the drop-down box, they are loaded. At this time, all the options are loaded. This situation is only applicable to slow loading. You need to click to load before you can drop down the options. The experience is average. <el-select v-model="userId" filterable :filter-method="userFilter" clearable> <el-option v-for="item in userList" :key="item.userId" :label="item.username" :value="item.userId" ></el-option> </el-select> userFilter(query = '') { let arr = this.allUserList.filter((item) => { return item.username.includes(query) || item.userId.includes(query) }) if (arr.length > 50) { this.userList = arr.slice(0, 50) } else { this.userList = arr } }, getUserWhiteList() { HttpRequest.post("/api/admin/community/getUserWhiteList").then( response => { this.allUserList = response.data.list; this.userFilter() } ); }, As shown above, we get the user list from the background. After our own filtering, we only render 50 data at a time. No matter how much data there is, it supports a variable for us and takes up memory. Of course, the more data there is, the slower the array traversal will be, but this has little impact. Solution for el-select component with too many options Business ScenarioWhen using the el-select component, if there are too many options, there will be disadvantages: The page renders a large number of el-option nodes, which will cause the page to freeze or even freeze, resulting in a very poor user experience. The scenario I encountered this time was a situation where the number of options was 6-9 thousand. Solution Take out the small option (renderOption) of the fixed item from the total options for page rendering, using the Code implementation Below is the component encapsulation of Vue <template> <el-select class="yt-select" v-model="currValue" filterable v-bind="$attrs" :filter-method="userFilter" :disabled="disabled" :clearable="clearable" @change="change" > <el-option v-for="option in renderOption" :key="option.value" :value="option.value" :label="option.label" >{{ option.label }}</el-option> </el-select> </template> <script> export default { name: 'easy-select', props: { value: { type: [String, Number], default: '' }, max: { type: Number, default: 30 }, disabled: type: Boolean, default: false }, clearable: { type: Boolean, default: true }, options: type: Array, default: () => [] } }, data () { return { renderOption: [] } }, computed: { currValue: { get () { return this.value || '' }, set (value) { this.$emit('input', value) } } }, watch: value () { this.addValueOptions() }, options: handler (V) { this.init() }, deep: true } }, created () { this.init() }, methods: { async init () { this.userFilter() this.addValueOptions() }, addValueOptions () { if (this.currValue) { let target = this.options.find((item) => { // Find the current item from the big option return item.value === this.currValue }) if (target) { // Compare the current item with the small option, if not, add if (this.renderOption.every(item => item.value !== target.value)) { this.renderOption.unshift(target) } } } }, addFilterOptions (label) { // Each time you search for an input, if there is an exact match, make sure that the item is in renderOption let target = this.options.find((item) => { // Find the current item from the big option return item.label === label }) if (target) { // Compare the current item with the small option, if not, add if (this.renderOption.every(item => item.label !== target.label)) { this.renderOption.unshift(target) } } }, userFilter (query = '') { let arr = this.options.filter((item) => { return item.label.includes(query) || item.value.includes(query) }) if (arr.length > this.max) { this.renderOption = arr.slice(0, this.max) this.addFilterOptions(query) } else { this.renderOption = arr } }, change (value) { this.$emit('change', value) if (!value) { // Single-select clear-optons initialize this.userFilter() } } } } </script> Precautions
This is the end of this article about the solution to the problem of too much data in ElementUI el-select. For more information about the problem of too much data in ElementUI el-select, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to install ROS Noetic in Ubuntu 20.04
>>: Solve the problem of MySQL server actively disconnecting when there is no operation timeout
An at-rule is a declaration that provides instruc...
When the DataSource property of a DataGrid control...
Table of contents Preface Installation and Usage ...
Table of contents 1. Simple page example 2.uni-ap...
Table of contents 1. Front-end control 1. In the ...
Sprite Cow download CSS Lint download Prefixr dow...
01. Command Overview dirname - strip non-director...
Table of contents 1. Related configuration Case 1...
Table of contents Container Hierarchy The process...
1. Install Docker yum install docker #Start the s...
1. Advantages and Disadvantages of Indexes Advant...
This article example shares the specific code for...
The essence of a flat website structure is simpli...
I learned a new trick today. I didn’t know it befo...
This article example shares the specific code of ...