管理系统PC端
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

112 lines
3.1 KiB

  1. <!-- 单选框组表单组件 zhao -->
  2. <template>
  3. <div>
  4. <van-field
  5. :readonly="true"
  6. :name="name"
  7. :label="label"
  8. :placeholder="placeholder"
  9. input-align="right"
  10. :required="required"
  11. :label-width="labelWidth || 'auto'"
  12. >
  13. <template #right-icon>
  14. <van-radio-group :disabled="readonly" @change="onChanged" v-model="internalValue" direction="horizontal" :rules="rules">
  15. <van-radio v-for="(item, index) in (columns ? columns : remoteColumns)" :name="getValue(item)" :key="index">{{getLabel(item)}}</van-radio>
  16. </van-radio-group>
  17. </template>
  18. </van-field>
  19. </div>
  20. </template>
  21. <script>
  22. import request from "@/utils/request";
  23. export default {
  24. name: "fieldRadio",
  25. props: [
  26. 'name', 'readonly', 'value', 'label', 'placeholder', 'required', 'rules', 'labelWidth',
  27. 'columns', // 列表数据 Array
  28. 'valueKey', // 名称键名 String
  29. 'dataKey', // 值键名 String
  30. 'remoteUrl', // 远程列表加载地址 String
  31. 'onRemoteResponse', // 远程获取到结果的处理回调 String|Function 如果是函数需返回数组, 如果是字符串支持.分割
  32. ],
  33. watch: {
  34. value: function (newVal, oldVal) {
  35. this.internalValue = newVal;
  36. },
  37. columns: function (newVal, oldVal) {
  38. },
  39. remoteUrl: function (newVal, oldVal) {
  40. this.requestRemote();
  41. },
  42. onRemoteResponse: function (newVal, oldVal) {
  43. this.parseRemote();
  44. }
  45. },
  46. created() {
  47. if(this.remoteUrl)
  48. this.requestRemote();
  49. },
  50. data() {
  51. return {
  52. internalValue: this.value,
  53. remoteColumns: null,
  54. remoteResponse: null,
  55. };
  56. },
  57. methods: {
  58. onChanged(data) {
  59. this.$emit("input", this.internalValue);
  60. this.$emit('change', this.internalValue);
  61. },
  62. getValue(data) {
  63. return typeof(data) === 'object' && this.dataKey ? data[this.dataKey] : data;
  64. },
  65. getLabel(data) {
  66. return typeof(data) === 'object' && this.valueKey ? data[this.valueKey] : data;
  67. },
  68. getColumns() {
  69. return this.columns ? this.columns : this.remoteColumns;
  70. },
  71. requestRemote() {
  72. if(!this.remoteUrl)
  73. return;
  74. this.remoteColumns = [];
  75. let promise = typeof(this.remoteUrl) === 'function' ? this.remoteUrl() : (this.remoteUrl instanceof Promise ? this.remoteUrl : request(this.remoteUrl));
  76. promise.then((resp) => {
  77. this.remoteResponse = resp;
  78. this.parseRemote();
  79. }).catch((e) => {
  80. console.error(e);
  81. }).finally(() => {
  82. })
  83. },
  84. parseRemote() {
  85. if(!this.remoteResponse)
  86. return;
  87. let type = typeof(this.onRemoteResponse);
  88. if(type === 'function')
  89. this.remoteColumns = this.onRemoteResponse(this.remoteResponse);
  90. else if(type === 'string')
  91. {
  92. let arr = this.onRemoteResponse.split('.');
  93. let ptr = this.remoteResponse;
  94. for(let i in arr)
  95. {
  96. ptr = this.remoteResponse[arr[i]];
  97. }
  98. this.remoteColumns = ptr;
  99. }
  100. else
  101. this.remoteColumns = this.remoteResponse;
  102. },
  103. },
  104. }
  105. </script>
  106. <style scoped>
  107. </style>