lj-select.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!--选择控件,可展示为下拉样式,也可展示为vux的group样式-->
  2. <!--events-->
  3. <!--选项改变change,参数(value)-->
  4. <template>
  5. <selector v-if="cell" :title="title" v-model="currentValue" @on-change="handleChange" :options="optionData"
  6. :value-map="['value','label']" direction="ltr" :readonly="disabled"/>
  7. <mu-select v-else :label="title" v-model="currentValue" @change="handleChange" :full-width="fullWidth"
  8. :disabled="disabled">
  9. <mu-option v-for="option in optionData" :label="option.label" :value="option.value"/>
  10. </mu-select>
  11. </template>
  12. <script>
  13. import {Selector} from 'vux'
  14. export default {
  15. name: "lj-select",
  16. components: {Selector},
  17. props: {
  18. // 标签文本
  19. title: {
  20. type: String,
  21. default: ""
  22. },
  23. // 选项数据数组
  24. options: {
  25. type: Array
  26. },
  27. // 设置键值对映射用以自动转换对象数据, 如 :['id','name']用以转换[{id:2,name:"明仔"}]格式的选项数据
  28. valueMap: {
  29. type: Array,
  30. default: function () {
  31. return ["value", "label"]
  32. }
  33. },
  34. // 用网络请求数据的API名称
  35. optionsApi: {
  36. type: String
  37. },
  38. // 网络请求的附加参数(token已默认封装,无需重复声明)
  39. apiParams: {
  40. type: Object
  41. },
  42. // 额外的数据数组,通常用于网络请求数据,附加“全部”选项用,如[{scid: -1, scname: "全部"}]
  43. extraOptions: {
  44. type: Array
  45. },
  46. // 当前选项的值,支持v-model绑定
  47. value: {},
  48. // 用于传递muse-ui 的mu-select的属性
  49. fullWidth: {
  50. type: Boolean
  51. },
  52. // 是否显示为vux的group的selector的样式
  53. cell: {
  54. type: Boolean,
  55. default: false
  56. },
  57. // 是否不可用
  58. disabled: {
  59. type: Boolean,
  60. default: false
  61. }
  62. },
  63. data() {
  64. return {
  65. apiData: [],
  66. currentValue: this.value,
  67. currentLabel: this.valueMap[this.value]
  68. }
  69. },
  70. created() {
  71. this.refresh();
  72. },
  73. methods:{
  74. refresh(){
  75. if (this.optionsApi) {
  76. let self = this;
  77. let requestBody = this.apiParams || {};
  78. requestBody.token = $lj.getStorage("token");
  79. $lj.postLJRequest(this.optionsApi, requestBody, function (ret) {
  80. if (ret) {
  81. for (let key in ret) {
  82. self.apiData = ret[key];
  83. return;
  84. }
  85. }
  86. })
  87. }
  88. },
  89. handleChange(){
  90. this.$emit('input',this.currentValue);
  91. this.$emit("change",this.currentValue);
  92. for (let option of this.optionData) {
  93. if (option.value === this.currentValue) {
  94. this.$emit("change-get-label", option.label);
  95. break;
  96. }
  97. }
  98. }
  99. },
  100. computed: {
  101. optionData() {
  102. let fullOptions = [];
  103. if (this.extraOptions) {
  104. fullOptions = fullOptions.concat(this.extraOptions);
  105. }
  106. if (this.optionsApi) {
  107. fullOptions = fullOptions.concat(this.apiData);
  108. } else {
  109. fullOptions = fullOptions.concat(this.options);
  110. }
  111. let result = [];
  112. fullOptions.forEach((item) => {
  113. if (typeof item === "object") {
  114. result.push({value: item[this.valueMap[0]], label: item[this.valueMap[1]]});
  115. } else {
  116. result.push({value: item, label: item})
  117. }
  118. });
  119. if (typeof this.currentValue === "undefined") {
  120. if (result.length){
  121. this.currentValue = result[0].value;
  122. this.handleChange();
  123. }
  124. }
  125. for (let option of result) {
  126. if (option.value === this.currentValue) {
  127. this.$emit("change-get-label", option.label);
  128. break;
  129. }
  130. }
  131. return result;
  132. }
  133. },
  134. watch: {
  135. value: function (newVal) {
  136. this.currentValue = newVal;
  137. }
  138. }
  139. }
  140. </script>