en-checkbox.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view class="box">
  3. <view class="input-box">
  4. <view class="input-box-left" :style="{'letter-spacing':labelWidth}">
  5. {{ label }}
  6. </view>
  7. <view class="input-box-right">
  8. <checkbox-group class="checkbox-data" @change="checkboxChange">
  9. <label class="checkbox-item" v-for="(checkboxItem,index) in checkboxArr">
  10. <checkbox :id="name+index" :value="checkboxItem.id" />
  11. <text class="iconfont icon-ok" v-if="checkboxItem.isChecked"> &#xe609;</text>
  12. <text class="iconfont icon-no" v-else> &#xe626;</text>
  13. {{checkboxItem.name}}
  14. </label>
  15. </checkbox-group>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'en-checkbox',
  23. props: {
  24. type: {
  25. type: String,
  26. default: 'text'
  27. },
  28. label: {
  29. type: String,
  30. default: '标题'
  31. },
  32. checkboxData: {
  33. type: Array,
  34. default: []
  35. },
  36. checkboxKey:{
  37. type:String,
  38. default:'id'
  39. },
  40. checkboxValue:{
  41. type:String,
  42. default:'name'
  43. },
  44. disabled: {
  45. default: false
  46. },
  47. name: {
  48. type: String,
  49. default: 'text'
  50. },
  51. valueType: {
  52. default: '1'
  53. },
  54. value: {
  55. default: []
  56. }
  57. },
  58. data() {
  59. return {
  60. inputValue: [],
  61. checkboxArr:[],
  62. labelWidth:0
  63. }
  64. },
  65. components: {},
  66. mounted() {
  67. this.setValue()
  68. this.setCheckboxData()
  69. this.setLabelWidth()
  70. },
  71. watch: {
  72. 'value': function () {
  73. if (this.inputValue !== this.value) {
  74. this.setValue()
  75. }
  76. },
  77. 'inputValue': function () {
  78. if(this.valueType==='1'){
  79. this.$emit('input', this.inputValue.join(','))
  80. }else {
  81. this.$emit('input', this.inputValue)
  82. }
  83. }
  84. },
  85. methods: {
  86. checkboxChange(e){
  87. this.inputValue=e.detail.value
  88. console.log(this.inputValue)
  89. this.setIsCheckbox()
  90. },
  91. setCheckboxData(){
  92. if(this.checkboxData.length<=0){
  93. return ;
  94. }
  95. this.checkboxData.forEach((item)=>{
  96. if(item[this.checkboxKey] && item[this.checkboxValue]){
  97. this.checkboxArr.push({
  98. 'id':item[this.checkboxKey]+'',
  99. 'name':item[this.checkboxValue],
  100. 'isChecked':false,
  101. })
  102. }
  103. })
  104. this.setIsCheckbox()
  105. },
  106. setValue(){
  107. let newValue=this.value
  108. if(typeof newValue ==='string'){
  109. newValue=newValue.split(',')
  110. }
  111. this.inputValue = []
  112. newValue.forEach((item)=>{
  113. this.inputValue.push(item+'')
  114. })
  115. this.setIsCheckbox()
  116. },
  117. setIsCheckbox(){
  118. this.checkboxArr.forEach((item)=>{
  119. item.isChecked=this.inputValue.indexOf(item.id) >= 0
  120. })
  121. },
  122. setLabelWidth(){
  123. let differenceNum=4- this.label.length;
  124. if(differenceNum===2){
  125. this.labelWidth='2em'
  126. }else if(differenceNum===1){
  127. this.labelWidth='0.5em'
  128. }
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="scss" scoped>
  134. @import url("../../static/css/en-common.css");
  135. .box{
  136. .input-box {
  137. display: flex;
  138. align-items: center;
  139. .input-box-left {
  140. width: 210rpx;
  141. min-width: 210rpx;
  142. color: #333333;
  143. }
  144. .input-box-right{
  145. display: flex;
  146. align-items: center;
  147. .checkbox-data{
  148. display: flex;
  149. flex-wrap: wrap;
  150. .checkbox-item{
  151. margin: 6rpx 20rpx 0 6rpx;
  152. .iconfont{
  153. padding-right: 6rpx;
  154. }
  155. .icon-ok{
  156. color: var(--selected-color);
  157. }
  158. .icon-no{
  159. color: var(--unselected-color);
  160. }
  161. checkbox{
  162. display: none;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. </style>