en-checkbox.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 @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 class="box-solid"></view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'en-checkbox',
  24. props: {
  25. type: {
  26. type: String,
  27. default: 'text'
  28. },
  29. label: {
  30. type: String,
  31. default: '标题'
  32. },
  33. checkboxData: {
  34. type: Array,
  35. default: []
  36. },
  37. checkboxKey:{
  38. type:String,
  39. default:'id'
  40. },
  41. checkboxValue:{
  42. type:String,
  43. default:'name'
  44. },
  45. disabled: {
  46. default: false
  47. },
  48. name: {
  49. type: String,
  50. default: 'text'
  51. },
  52. valueType: {
  53. default: '1'
  54. },
  55. value: {
  56. default: []
  57. }
  58. },
  59. data() {
  60. return {
  61. inputValue: [],
  62. checkboxArr:[],
  63. }
  64. },
  65. components: {},
  66. mounted() {
  67. this.setValue()
  68. this.setCheckboxData()
  69. },
  70. watch: {
  71. 'value': function () {
  72. if (this.inputValue !== this.value) {
  73. this.setValue()
  74. }
  75. },
  76. 'inputValue': function () {
  77. if(this.valueType==='1'){
  78. this.$emit('input', this.inputValue.join(','))
  79. }else {
  80. this.$emit('input', this.inputValue)
  81. }
  82. }
  83. },
  84. methods: {
  85. checkboxChange(e){
  86. this.inputValue=e.detail.value
  87. console.log(this.inputValue)
  88. this.setIsCheckbox()
  89. },
  90. setCheckboxData(){
  91. if(this.checkboxData.length<=0){
  92. return ;
  93. }
  94. this.checkboxData.forEach((item)=>{
  95. if(item[this.checkboxKey] && item[this.checkboxValue]){
  96. this.checkboxArr.push({
  97. 'id':item[this.checkboxKey]+'',
  98. 'name':item[this.checkboxValue],
  99. 'isChecked':false,
  100. })
  101. }
  102. })
  103. this.setIsCheckbox()
  104. },
  105. setValue(){
  106. let newValue=this.value
  107. if(typeof newValue ==='string'){
  108. newValue=newValue.split(',')
  109. }
  110. this.inputValue = []
  111. newValue.forEach((item)=>{
  112. this.inputValue.push(item+'')
  113. })
  114. this.setIsCheckbox()
  115. },
  116. setIsCheckbox(){
  117. this.checkboxArr.forEach((item)=>{
  118. item.isChecked=this.inputValue.indexOf(item.id) >= 0
  119. })
  120. }
  121. }
  122. }
  123. </script>
  124. <style lang="scss" scoped>
  125. @font-face {
  126. font-family: 'iconfont';
  127. src: url('iconfont.ttf?t=1662432727120') format('truetype');
  128. }
  129. .iconfont {
  130. font-family: "iconfont" !important;
  131. font-size: 36rpx;
  132. color: #3169FA;
  133. font-style: normal;
  134. -webkit-font-smoothing: antialiased;
  135. -moz-osx-font-smoothing: grayscale;
  136. }
  137. .box{
  138. background-color: #ffffff;
  139. padding: 34rpx 0 12rpx 0;
  140. .input-box {
  141. display: flex;
  142. align-items: center;
  143. .input-box-left {
  144. width: 210rpx;
  145. min-width: 210rpx;
  146. font-size: 32rpx;
  147. color: #333333;
  148. }
  149. .input-box-right{
  150. display: flex;
  151. align-items: center;
  152. .checkbox-item{
  153. padding-right: 20rpx;
  154. margin-bottom: 20rpx;
  155. checkbox{
  156. display: none;
  157. }
  158. }
  159. }
  160. }
  161. .box-solid{
  162. border-bottom: 1px solid #F0F0F0;
  163. margin-top:22rpx;
  164. }
  165. }
  166. </style>