2016-07-27 14:15:02 +08:00
|
|
|
<style>
|
|
|
|
.demo-box {
|
|
|
|
.el-autocomplete {
|
|
|
|
width: 180px;
|
|
|
|
}
|
2016-08-05 16:07:54 +08:00
|
|
|
.my-suggestions-item {
|
2016-07-27 14:15:02 +08:00
|
|
|
& .remark {
|
|
|
|
float: right;
|
|
|
|
font-size: 13px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
2016-08-05 16:07:54 +08:00
|
|
|
var Vue = require('vue');
|
|
|
|
Vue.component('my-item', {
|
|
|
|
functional: true,
|
|
|
|
render: function (h, ctx) {
|
|
|
|
var item = ctx.props.item;
|
|
|
|
return h('li', {
|
|
|
|
attrs: { class: 'my-suggestions-item' }
|
|
|
|
}, [
|
|
|
|
h('span', { attrs: { class: 'label' } }, ['选项' + ctx.props.index]),
|
|
|
|
h('span', { attrs: { class: 'remark' } }, [item.display])
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
item: {
|
|
|
|
type: Object,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
index: {
|
|
|
|
type: Number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-07-27 14:15:02 +08:00
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
states: [],
|
|
|
|
state1: '',
|
|
|
|
state2: '',
|
|
|
|
state3: '',
|
|
|
|
state4: '',
|
2016-08-05 16:07:54 +08:00
|
|
|
timeout: null
|
2016-07-27 14:15:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
loadAll() {
|
|
|
|
var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\
|
|
|
|
Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\
|
|
|
|
Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\
|
|
|
|
Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\
|
|
|
|
North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\
|
|
|
|
South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\
|
|
|
|
Wisconsin, Wyoming';
|
|
|
|
var result = [];
|
|
|
|
|
|
|
|
allStates.split(/, +/g).forEach((state) => {
|
|
|
|
if (state) {
|
|
|
|
result.push({
|
|
|
|
value: state.toLowerCase(),
|
|
|
|
display: state
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
querySearch(queryString, cb) {
|
2016-07-27 14:15:02 +08:00
|
|
|
var states = this.states;
|
2016-08-05 16:07:54 +08:00
|
|
|
var results = queryString ? states.filter(this.createStateFilter(queryString)) : states;
|
2016-07-27 14:15:02 +08:00
|
|
|
|
2016-08-05 16:07:54 +08:00
|
|
|
cb(results);
|
2016-07-27 14:15:02 +08:00
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
querySearchAsync(queryString, cb) {
|
2016-07-27 14:15:02 +08:00
|
|
|
var states = this.states;
|
2016-08-05 16:07:54 +08:00
|
|
|
var results = queryString ? states.filter(this.createStateFilter(queryString)) : states;
|
2016-07-27 14:15:02 +08:00
|
|
|
|
2016-08-05 16:07:54 +08:00
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
cb(results);
|
|
|
|
}, 3000 * Math.random());
|
2016-07-27 14:15:02 +08:00
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
createStateFilter(queryString) {
|
2016-07-27 14:15:02 +08:00
|
|
|
return (state) => {
|
2016-08-05 16:07:54 +08:00
|
|
|
return (state.value.indexOf(queryString.toLowerCase()) === 0);
|
2016-07-27 14:15:02 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
mounted() {
|
2016-07-27 14:15:02 +08:00
|
|
|
this.states = this.loadAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
2016-08-05 16:07:54 +08:00
|
|
|
## 基础使用
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
|
|
<div class="demo-box">
|
|
|
|
<el-autocomplete
|
2016-08-05 16:07:54 +08:00
|
|
|
v-model="state1"
|
|
|
|
:fetch-suggestions="querySearch"
|
|
|
|
placeholder="请输入内容"
|
2016-07-27 14:15:02 +08:00
|
|
|
></el-autocomplete>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
```html
|
|
|
|
<template>
|
|
|
|
<el-autocomplete
|
2016-08-05 16:07:54 +08:00
|
|
|
v-model="state1"
|
|
|
|
:fetch-suggestions="querySearch"
|
|
|
|
placeholder="请输入内容"
|
2016-07-27 14:15:02 +08:00
|
|
|
></el-autocomplete>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2016-08-05 16:07:54 +08:00
|
|
|
states: [],
|
|
|
|
state1: ''
|
2016-07-27 14:15:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
loadAll() {
|
|
|
|
var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\
|
|
|
|
Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\
|
|
|
|
Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\
|
|
|
|
Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\
|
|
|
|
North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\
|
|
|
|
South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\
|
|
|
|
Wisconsin, Wyoming';
|
|
|
|
var result = [];
|
|
|
|
|
|
|
|
allStates.split(/, +/g).forEach((state) => {
|
|
|
|
if (state) {
|
|
|
|
result.push({
|
|
|
|
value: state.toLowerCase(),
|
|
|
|
display: state
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
querySearch(queryString, callback) {
|
2016-07-27 14:15:02 +08:00
|
|
|
var states = this.states;
|
2016-08-05 16:07:54 +08:00
|
|
|
var results = queryString ? states.filter(this.createStateFilter(queryString)) : states;
|
2016-07-27 14:15:02 +08:00
|
|
|
|
2016-08-05 16:07:54 +08:00
|
|
|
callback(results);
|
2016-07-27 14:15:02 +08:00
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
createStateFilter(queryString) {
|
2016-07-27 14:15:02 +08:00
|
|
|
return (state) => {
|
2016-08-05 16:07:54 +08:00
|
|
|
return (state.value.indexOf(queryString.toLowerCase()) === 0);
|
2016-07-27 14:15:02 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
mounted() {
|
2016-07-27 14:15:02 +08:00
|
|
|
this.states = this.loadAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
|
|
|
|
## 自定义模板
|
|
|
|
|
|
|
|
<div class="demo-box">
|
|
|
|
<el-autocomplete
|
2016-08-05 16:07:54 +08:00
|
|
|
v-model="state2"
|
|
|
|
:fetch-suggestions="querySearch"
|
|
|
|
custom-item="my-item"
|
|
|
|
placeholder="请输入内容"
|
2016-07-27 14:15:02 +08:00
|
|
|
></el-autocomplete>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
```html
|
|
|
|
<el-autocomplete
|
2016-08-05 16:07:54 +08:00
|
|
|
v-model="state2"
|
|
|
|
:fetch-suggestions="querySearch"
|
|
|
|
custom-item="my-item"
|
|
|
|
placeholder="请输入内容"
|
2016-07-27 14:15:02 +08:00
|
|
|
></el-autocomplete>
|
|
|
|
|
|
|
|
<script>
|
2016-08-05 16:07:54 +08:00
|
|
|
var Vue = require('vue');
|
|
|
|
Vue.component('my-item', {
|
|
|
|
functional: true,
|
|
|
|
render: function (h, ctx) {
|
|
|
|
var item = ctx.props.item;
|
|
|
|
return h('li', {
|
|
|
|
attrs: { class: 'my-suggestions-item' }
|
|
|
|
}, [
|
|
|
|
h('span', { attrs: { class: 'label' } }, ['选项' + ctx.props.index]),
|
|
|
|
h('span', { attrs: { class: 'remark' } }, [item.display])
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
item: {
|
|
|
|
type: Object,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
index: {
|
|
|
|
type: Number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-07-27 14:15:02 +08:00
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2016-08-05 16:07:54 +08:00
|
|
|
states: [],
|
|
|
|
state2: ''
|
2016-07-27 14:15:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
loadAll() {
|
|
|
|
var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\
|
|
|
|
Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\
|
|
|
|
Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\
|
|
|
|
Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\
|
|
|
|
North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\
|
|
|
|
South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\
|
|
|
|
Wisconsin, Wyoming';
|
|
|
|
var result = [];
|
|
|
|
|
|
|
|
allStates.split(/, +/g).forEach((state) => {
|
|
|
|
if (state) {
|
|
|
|
result.push({
|
|
|
|
value: state.toLowerCase(),
|
|
|
|
display: state
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
querySearch(queryString, cb) {
|
2016-07-27 14:15:02 +08:00
|
|
|
var states = this.states;
|
2016-08-05 16:07:54 +08:00
|
|
|
var results = queryString ? states.filter(this.createStateFilter(queryString)) : states;
|
2016-07-27 14:15:02 +08:00
|
|
|
|
2016-08-05 16:07:54 +08:00
|
|
|
cb(results);
|
2016-07-27 14:15:02 +08:00
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
createStateFilter(queryString) {
|
2016-07-27 14:15:02 +08:00
|
|
|
return (state) => {
|
2016-08-05 16:07:54 +08:00
|
|
|
return (state.value.indexOf(queryString.toLowerCase()) === 0);
|
2016-07-27 14:15:02 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
mounted() {
|
2016-07-27 14:15:02 +08:00
|
|
|
this.states = this.loadAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
|
|
|
|
## 服务端数据
|
|
|
|
|
|
|
|
<div class="demo-box">
|
|
|
|
<el-autocomplete
|
2016-08-05 16:07:54 +08:00
|
|
|
v-model="state3"
|
2016-07-27 14:15:02 +08:00
|
|
|
placeholder = "请输入内容"
|
2016-08-05 16:07:54 +08:00
|
|
|
:fetch-Suggestions="querySearchAsync"
|
2016-07-27 14:15:02 +08:00
|
|
|
></el-autocomplete>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
```html
|
2016-08-05 16:07:54 +08:00
|
|
|
<template>
|
|
|
|
<el-autocomplete
|
|
|
|
v-model="state3"
|
|
|
|
placeholder = "请输入内容"
|
|
|
|
:fetch-Suggestions="querySearchAsync"
|
|
|
|
></el-autocomplete>
|
|
|
|
</template>
|
2016-07-27 14:15:02 +08:00
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2016-08-05 16:07:54 +08:00
|
|
|
state3: '',
|
|
|
|
states: []
|
2016-07-27 14:15:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
loadAll() {
|
|
|
|
var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\
|
|
|
|
Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\
|
|
|
|
Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\
|
|
|
|
Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\
|
|
|
|
North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\
|
|
|
|
South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\
|
|
|
|
Wisconsin, Wyoming';
|
|
|
|
var result = [];
|
|
|
|
|
|
|
|
allStates.split(/, +/g).forEach((state) => {
|
|
|
|
if (state) {
|
|
|
|
result.push({
|
|
|
|
value: state.toLowerCase(),
|
|
|
|
display: state
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
2016-08-05 16:07:54 +08:00
|
|
|
querySearchAsync(query, callback) {
|
2016-07-27 14:15:02 +08:00
|
|
|
var states = this.states;
|
2016-08-05 16:07:54 +08:00
|
|
|
var results = query ? states.filter(this.createStateFilter(query)) : states;
|
2016-07-27 14:15:02 +08:00
|
|
|
|
2016-08-05 16:07:54 +08:00
|
|
|
if (!query) { return []; }
|
2016-07-27 14:15:02 +08:00
|
|
|
|
2016-08-05 16:07:54 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
callback(results);
|
|
|
|
}, 3000 * Math.random());
|
2016-07-27 14:15:02 +08:00
|
|
|
},
|
|
|
|
createStateFilter(query) {
|
|
|
|
return (state) => {
|
|
|
|
return (state.value.indexOf(query.toLowerCase()) === 0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ready() {
|
|
|
|
this.states = this.loadAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
|
|
|
|
## API
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|------------- |---------------- |---------------- |---------------------- |-------- |
|
|
|
|
| placeholder | 输入框占位文本 | string | | |
|
|
|
|
| disabled | 禁用 | boolean | true, false | false |
|
2016-08-05 16:07:54 +08:00
|
|
|
| value | 必填值输入绑定值 | string | | |
|
2016-07-27 14:15:02 +08:00
|
|
|
| showOnUpDown | 是否通过键盘上下键控制建议列表 | boolean | | |
|
2016-08-05 16:07:54 +08:00
|
|
|
| fetch-suggestions | 返回输入建议的方法,组件内部通过调用该方法来获得输入建议的数据,在该方法中,仅当你的输入建议数据 resolve 时再通过调用 callback(data:[]) 来返回它 | Function(queryString, callback) | | |
|