123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <!--选择控件,可展示为下拉样式,也可展示为vux的group样式-->
- <!--events-->
- <!--选项改变change,参数(value)-->
- <template>
- <selector v-if="cell" :title="title" v-model="currentValue" @on-change="handleChange" :options="optionData"
- :value-map="['value','label']" direction="ltr" :readonly="disabled"/>
- <mu-select v-else :label="title" v-model="currentValue" @change="handleChange" :full-width="fullWidth"
- :disabled="disabled">
- <mu-option v-for="option in optionData" :label="option.label" :value="option.value"/>
- </mu-select>
- </template>
- <script>
- import {Selector} from 'vux'
- export default {
- name: "lj-select",
- components: {Selector},
- props: {
- // 标签文本
- title: {
- type: String,
- default: ""
- },
- // 选项数据数组
- options: {
- type: Array
- },
- // 设置键值对映射用以自动转换对象数据, 如 :['id','name']用以转换[{id:2,name:"明仔"}]格式的选项数据
- valueMap: {
- type: Array,
- default: function () {
- return ["value", "label"]
- }
- },
- // 用网络请求数据的API名称
- optionsApi: {
- type: String
- },
- // 网络请求的附加参数(token已默认封装,无需重复声明)
- apiParams: {
- type: Object
- },
- // 额外的数据数组,通常用于网络请求数据,附加“全部”选项用,如[{scid: -1, scname: "全部"}]
- extraOptions: {
- type: Array
- },
- // 当前选项的值,支持v-model绑定
- value: {},
- // 用于传递muse-ui 的mu-select的属性
- fullWidth: {
- type: Boolean
- },
- // 是否显示为vux的group的selector的样式
- cell: {
- type: Boolean,
- default: false
- },
- // 是否不可用
- disabled: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- apiData: [],
- currentValue: this.value,
- currentLabel: this.valueMap[this.value]
- }
- },
- created() {
- this.refresh();
- },
- methods:{
- refresh(){
- if (this.optionsApi) {
- let self = this;
- let requestBody = this.apiParams || {};
- requestBody.token = $lj.getStorage("token");
- $lj.postLJRequest(this.optionsApi, requestBody, function (ret) {
- if (ret) {
- for (let key in ret) {
- self.apiData = ret[key];
- return;
- }
- }
- })
- }
- },
- handleChange(){
- this.$emit('input',this.currentValue);
- this.$emit("change",this.currentValue);
- for (let option of this.optionData) {
- if (option.value === this.currentValue) {
- this.$emit("change-get-label", option.label);
- break;
- }
- }
-
- }
- },
- computed: {
- optionData() {
- let fullOptions = [];
- if (this.extraOptions) {
- fullOptions = fullOptions.concat(this.extraOptions);
- }
- if (this.optionsApi) {
- fullOptions = fullOptions.concat(this.apiData);
- } else {
- fullOptions = fullOptions.concat(this.options);
- }
- let result = [];
- fullOptions.forEach((item) => {
- if (typeof item === "object") {
- result.push({value: item[this.valueMap[0]], label: item[this.valueMap[1]]});
- } else {
- result.push({value: item, label: item})
- }
- });
- if (typeof this.currentValue === "undefined") {
- if (result.length){
- this.currentValue = result[0].value;
- this.handleChange();
- }
- }
- for (let option of result) {
- if (option.value === this.currentValue) {
- this.$emit("change-get-label", option.label);
- break;
- }
- }
- return result;
- }
- },
- watch: {
- value: function (newVal) {
- this.currentValue = newVal;
- }
- }
- }
- </script>
|