<user-select :value.sync="formPeopleData.securityLiableUserId" :default-user="securityLiableRealName" />
<!-- userSelect 子组件 -->
<template>
<div>
<!--
获取焦点时第一次执行查询 -->
<el-select
ref="mylazy"
v-model="itemCode"
v-select-loadmore="loadmore"
filterable
remote
:remote-method="searchUser"
:loading="loading"
clearable
placeholder="请选择"
style="width: 100%;"
@focus="mygetItem"
>
<el-option
v-for="item in userList"
:key="item.id"
:label="item.realName"
:value="item.id"
/>
</el-select>
</div>
</template>
<script>
import debounce from 'lodash.debounce'
export default {
name: 'DeviceUserInfo',
directives: {
'select-loadmore': {
bind (el, binding) {
const SELECTWRAP_DOM = el.querySelector(
'.el-select-dropdown .el-select-dropdown__wrap'
)
SELECTWRAP_DOM.addEventListener('scroll', function () {
const condition = this.scrollHeight - this.scrollTop <= this.clientHeight
if (condition) {
binding.value()
}
})
}
}
},
props: {
value: {
type: String,
default: ''
},
defaultUser: {
type: Array,
default () {
return []
}
}
},
data () {
return {
currentVal: '',
loading: false,
itemCode: '',
userList: [],
stopLoading: false,
pageData: {
pageNumber: 1,
pageSize: 20
}
}
},
watch: {
itemCode (val) {
this.$emit('update:value', val)
},
value (val) {
this.itemCode = val
},
defaultUser: {
handler (val) {
if (Array.isArray(val) && val.length > 0) {
this.userList.push(...val)
}
},
deep: true
}
},
created () {},
mounted () {},
methods: {
loadmore () {
if (!this.stopLoading) {
this.pageData.pageNumber++
this.mygetItem(this.$refs.mylazy.query, true)
}
},
mygetItem (val, lazy = false) {
this.loading = true
const param = { pageNo: this.pageData.pageNumber, pageSize: this.pageData.pageSize }
if (lazy === false) {
this.userList = []
param.pageNo = 1
param.pageSize = 20
}
if (typeof (val) === 'string') {
param.searchKeyword = val
param.searchName = 'real_name'
this.$axios.post('/ams/platform/user/listPage', param).then((res) => {
this.loading = false
const totalCount = res.data.totalCount
if (!totalCount) {
this.stopLoading = true
}
this.userList = [...this.userList, ...res.data.list]
}).catch((err) => {
this.$message({
message: err,
type: 'warning'
})
})
} else {
param.pageNo = 1
param.pageSize = 20
this.stopLoading = false
this.$axios.post('/ams/platform/user/listPage', param).then((res) => {
this.loading = false
this.userList = res.data.list
}).catch((err) => {
this.$message({
message: err,
type: 'warning'
})
})
}
},
searchUser: debounce(function (val) {
this.mygetItem(val)
}, 1000)
}
}
</script>
<style scoped></style>