fa-array.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view class="fa-array">
  3. <view class="u-flex">
  4. <view class="u-flex-5">
  5. <view class="">{{ faKey }}</view>
  6. </view>
  7. <view class="u-flex-6 u-p-l-10">
  8. <view class="">{{ faVal }}</view>
  9. </view>
  10. </view>
  11. <view class="u-flex u-m-t-15" v-for="(item, index) in list" :key="index">
  12. <view class="u-flex-5"><u-input v-model="item.key" :trim="trim" :border="border" /></view>
  13. <view class="u-m-l-15 u-m-r-15 u-flex-5"><u-input v-model="item.value" :trim="trim" :border="border" /></view>
  14. <view class="u-p-l-15 u-p-r-15 u-text-center close" @click="del(index)"><u-icon name="close" color="#ffffff" size="30"></u-icon></view>
  15. </view>
  16. <view class="u-text-right u-m-t-20">
  17. <u-button
  18. size="mini"
  19. type="primary"
  20. :custom-style="{ backgroundColor: lightColor, color: theme.bgColor, border: '1px solid ' + faBorderColor }"
  21. throttle-time="0"
  22. @click="add"
  23. >
  24. <u-icon name="plus" :color="theme.bgColor" size="25"></u-icon>
  25. <text class="u-m-l-5">追加</text>
  26. </u-button>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import Emitter from '@/uni_modules/uview-ui/libs/util/emitter.js';
  32. export default {
  33. name: 'fa-array',
  34. mixins: [Emitter],
  35. props: {
  36. value:{
  37. type:String,
  38. default:''
  39. },
  40. faKey: {
  41. type: String,
  42. default: '键'
  43. },
  44. faVal: {
  45. type: String,
  46. default: '值'
  47. },
  48. showValue: {
  49. type: [String, Object],
  50. default: ''
  51. }
  52. },
  53. watch: {
  54. list: {
  55. deep: true,
  56. handler: function(newValue) {
  57. let obj = {};
  58. newValue.map(item => {
  59. if (item.key && item.value) {
  60. obj[item.key] = item.value;
  61. }
  62. });
  63. let value = this.$u.test.empty(obj)?'':JSON.stringify(obj);
  64. this.$emit('input', value);
  65. setTimeout(() => {
  66. this.dispatch('u-form-item', 'on-form-blur', value);
  67. }, 50);
  68. }
  69. },
  70. showValue: {
  71. immediate: true,
  72. handler(val) {
  73. if (val) {
  74. if (this.$u.test.object(val)) {
  75. let arr = [];
  76. for (let i in val) {
  77. arr.push({
  78. key: i,
  79. value: val[i]
  80. });
  81. }
  82. if (arr.length > 0) {
  83. this.list = arr;
  84. }
  85. } else {
  86. let o = JSON.parse(val);
  87. let arr = [];
  88. for (let i in o) {
  89. arr.push({
  90. key: i,
  91. value: o[i]
  92. });
  93. }
  94. if (arr.length > 0) {
  95. this.list = arr;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. },
  102. data() {
  103. return {
  104. border: true,
  105. trim: true,
  106. list: [
  107. {
  108. key: '',
  109. value: ''
  110. }
  111. ]
  112. };
  113. },
  114. methods: {
  115. add() {
  116. this.list.push({
  117. key: '',
  118. value: ''
  119. });
  120. },
  121. del(index) {
  122. this.list.splice(index, 1);
  123. },
  124. getData() {
  125. return this.list;
  126. }
  127. }
  128. };
  129. </script>
  130. <style lang="scss" scoped>
  131. .fa-array{
  132. width: 100%;
  133. }
  134. .close {
  135. background: #2c3e50;
  136. border-radius: 10rpx;
  137. }
  138. </style>