textsearch.vue 1.79 KB
<style>

</style>

<template>
	<div :class="class" :style="{overflow:'inherit','width':width}">
		<input type="text" id="cn1" v-model="value" v-on:focus="focus" v-on:blur="blur" v-on:keyup="keyup">
		<ul class="hint-list" v-bind:style="{'width':widthul}" v-show="show " v-on:mouseover="over=1" v-on:mouseout="over=0">
			<li v-for="item in list" v-on:click="check(item)">
				<span>{{item.title}}</span>
			</li>
			<li v-show="list.length==0"><span>暂无结果</span></li>
		</ul>
	</div>

</template>
<script>
	module.exports = {
		props: {
			class: {
				type: String,
					default: ' control control-hint'
			},
			type: {
				type: String,
				default: '1'
			},
			width: {
				type: String,
				default: 'auto'
			},
			widthul: {
				type: String,
				default: '100%'
			},
			value: [String, Number],
			host: {
				type: String,
				default: 'http://localhost:3003/'
			},
			url: {
				type: String,
				default: 'search/'
			},
			ms: {
				type: String,
				default: 300
			}
		},
		data: function() {
			return {
				show: false,
				list: [],
				settime: '',
				over: 0
			}
		},
		methods: {
			getData: function() {
				var that = this;
				Ajax.get(this.url, {
					type: this.type,
					value: this.value
				}).then(function(res) {
					var result = res.data;

					if(result.data) {
						that.$set('list', result.data);
					}
				})
			},
			check: function(item) {

				this.value = item.title;
				this.show = false;
			},
			focus: function() {
				this.show = true;
				this.clear();
			},
			blur: function() {
				if(this.over == false) {
					this.show = false;
				}
			},
			keyup: function() {
				this.clear();

			},
			clear: function() {
				var that = this;
				clearTimeout(this.settime);
				this.settime = setTimeout(function() {
					that.getData();
				}, this.ms)
			}
		}
	}
</script>