en-checkbox.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view class="box">
  3. <view class="input-box">
  4. <view class="input-box-left">
  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. }
  63. },
  64. components: {},
  65. mounted() {
  66. this.setValue()
  67. this.setCheckboxData()
  68. },
  69. watch: {
  70. 'value': function () {
  71. if (this.inputValue !== this.value) {
  72. this.setValue()
  73. }
  74. },
  75. 'inputValue': function () {
  76. if(this.valueType==='1'){
  77. this.$emit('input', this.inputValue.join(','))
  78. }else {
  79. this.$emit('input', this.inputValue)
  80. }
  81. }
  82. },
  83. methods: {
  84. checkboxChange(e){
  85. this.inputValue=e.detail.value
  86. console.log(this.inputValue)
  87. this.setIsCheckbox()
  88. },
  89. setCheckboxData(){
  90. if(this.checkboxData.length<=0){
  91. return ;
  92. }
  93. this.checkboxData.forEach((item)=>{
  94. if(item[this.checkboxKey] && item[this.checkboxValue]){
  95. this.checkboxArr.push({
  96. 'id':item[this.checkboxKey]+'',
  97. 'name':item[this.checkboxValue],
  98. 'isChecked':false,
  99. })
  100. }
  101. })
  102. this.setIsCheckbox()
  103. },
  104. setValue(){
  105. let newValue=this.value
  106. if(typeof newValue ==='string'){
  107. newValue=newValue.split(',')
  108. }
  109. this.inputValue = []
  110. newValue.forEach((item)=>{
  111. this.inputValue.push(item+'')
  112. })
  113. this.setIsCheckbox()
  114. },
  115. setIsCheckbox(){
  116. this.checkboxArr.forEach((item)=>{
  117. item.isChecked=this.inputValue.indexOf(item.id) >= 0
  118. })
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="scss" scoped>
  124. @import url("../static/css/en-common.css");
  125. .box{
  126. .input-box {
  127. display: flex;
  128. align-items: center;
  129. .input-box-left {
  130. width: 210rpx;
  131. min-width: 210rpx;
  132. color: #333333;
  133. }
  134. .input-box-right{
  135. display: flex;
  136. align-items: center;
  137. .checkbox-data{
  138. display: flex;
  139. flex-wrap: wrap;
  140. .checkbox-item{
  141. margin: 6rpx 20rpx 0 6rpx;
  142. .iconfont{
  143. color: #333333;
  144. padding-right: 6rpx;
  145. }
  146. .icon-ok{
  147. color: #3169FA;
  148. }
  149. checkbox{
  150. display: none;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. </style>