tm-groupcheckbox.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="tm-groupcheckbox " :class="[customClass]">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * 复选框组
  9. * @description 此组件必须,配合tm-checkbox组件使用,不可单独使用。
  10. * @property {Number} max = [999] 最大选择数量
  11. * @property {String} name = [] 默认:'',提交表单时的的字段名称标识
  12. * @property {String} customClass = [] 默认:'',自定义class
  13. * @property {Function} change 任何一个checkekBox改变将会触发此事件,并携带选中的数组数据。
  14. * @property {Function} error 如果超过最大选择数量将会触发此事件。
  15. * @example <tm-groupcheckbox><tm-checkbox v-model="checked" label="苹果"></tm-checkbox></tm-groupcheckbox>
  16. *
  17. */
  18. export default {
  19. options: {
  20. virtualHost: true,
  21. styleIsolation: 'shared'
  22. },
  23. name:'tm-groupcheckbox',
  24. props:{
  25. // 最大选择数量
  26. max:{
  27. type:Number,
  28. default:9999
  29. },
  30. //提交表单时的的字段名称
  31. name:{
  32. type:String,
  33. default:''
  34. },
  35. customClass:{
  36. type:String,
  37. default:''
  38. }
  39. },
  40. data() {
  41. return {
  42. };
  43. },
  44. mounted() {
  45. this.$nextTick(function(){
  46. this.$emit('change',this.getVal())
  47. })
  48. },
  49. methods: {
  50. change(e) {
  51. this.$emit('change',e)
  52. },
  53. // 获取选中的结果 。
  54. getVal(){
  55. let box = [];
  56. function findchild(p,index){
  57. let preat = p;
  58. if(preat.$options?.name==='tm-checkbox'){
  59. if(preat.changValue){
  60. box.push({index:index,value:preat.name,checked:preat.changValue})
  61. }
  62. }else{
  63. if(preat.$children.length>0){
  64. preat.$children.forEach(item=>{
  65. findchild(item,index++);
  66. })
  67. }
  68. }
  69. };
  70. findchild(this,0);
  71. return box;
  72. },
  73. // 反选。如果一个都没选择。反选就相当于全选。如果是全选,则结果是全部不选。
  74. // 执行完毕后。不能立即使用getVal获取结果需要this.$nextTick
  75. reverseVal(){
  76. function findchild(p,index){
  77. let preat = p;
  78. if(preat.$options?.name==='tm-checkbox'){
  79. preat.changValue = !preat.changValue;
  80. }else{
  81. if(preat.$children.length>0){
  82. preat.$children.forEach(item=>{
  83. findchild(item,index++);
  84. })
  85. }
  86. }
  87. };
  88. findchild(this,0);
  89. },
  90. // 清除选中。
  91. clear(){
  92. function findchild(p,index){
  93. let preat = p;
  94. if(preat.$options?.name==='tm-checkbox'){
  95. preat.changValue = false;
  96. }else{
  97. if(preat.$children.length>0){
  98. preat.$children.forEach(item=>{
  99. findchild(item,index++);
  100. })
  101. }
  102. }
  103. };
  104. findchild(this,0);
  105. },
  106. // 只有当超过最选选项时才会发出错误。
  107. error(){
  108. uni.showToast({
  109. title:"超过选择限制",icon:'none'
  110. })
  111. this.$emit('error',"超过选择限制")
  112. }
  113. },
  114. }
  115. </script>
  116. <style lang="scss">
  117. </style>