CheckBox.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div class="check-wrap" v-if="item">
  3. <!-- <img :src="datas.url.replace('{imageUrlPrefix}',imgUrl)" v-if="datas.url"> -->
  4. <p v-for="(it,index) in datas.questionDetailList" :key="it.id" class="list" @click="handleClick(it,index)">
  5. <img :src="it.select==1?check:defaultPic">
  6. <!-- <span :class="{'check':it.select==1}">{{it.name}}</span> -->
  7. <span v-if="((it.description||it.name).indexOf('${'))==-1" :class="[{'check':it.select==1},{'exclu':exclusion !==999 && it.exclusion !== exclusion}]">{{it.description||it.name}}</span>
  8. <!-- <MultiLineInput v-else
  9. @handleInp="inpVal($event,index)"
  10. :msg="it.name"
  11. :part="it"
  12. :border="false"
  13. :inline="true"
  14. :select="it.select==1"
  15. /> -->
  16. <OptionInp v-else
  17. :item="it"
  18. ref="inp"
  19. @handleInp="inpVal($event,index)"
  20. :exclu="exclusion !==999 && it.exclusion !== exclusion"
  21. />
  22. </p>
  23. </div>
  24. </template>
  25. <script type="text/javascript">
  26. import {imageUrlPrefix,patt,concatVal} from '@utils/tools.js';
  27. import icon from '../images/check-default.png';
  28. import checkIcon from '../images/check.png';
  29. import MultiLineInput from '../common/MultiLineInput.vue';
  30. import OptionInp from '../common/OptionInp.vue';
  31. export default{
  32. name:'CheckBox',
  33. data(){
  34. return{
  35. imgUrl:imageUrlPrefix,
  36. defaultPic:icon,
  37. check:checkIcon,
  38. datas:{},
  39. exclusion:999 //互斥
  40. }
  41. },
  42. props:['item'],
  43. created(){
  44. // this.datas = JSON.parse(JSON.stringify(this.item));
  45. this.datas = this.item;
  46. this.resetExc();
  47. },
  48. methods:{
  49. handleClick(it,index){
  50. const that = this;
  51. const list = this.datas;
  52. let data = list.questionDetailList&&list.questionDetailList.slice(0);
  53. // 处理互斥
  54. const excluArr = data.filter(it=>it.exclusion==1);
  55. const filArr = data.filter(it=>it.select==1);
  56. if(excluArr.length>0){//有互斥
  57. if(filArr.length>0){//有选中
  58. if(it.exclusion !== filArr[0].exclusion){
  59. return
  60. }
  61. }
  62. this.exclusion = it.exclusion;
  63. }
  64. // 处理选中状态
  65. if(data[index].select){
  66. data[index].select = 0;
  67. }else{
  68. data[index].select = 1;
  69. this.exclusion = it.exclusion;
  70. }
  71. // 处理取消-互斥
  72. const filArr1 = data.filter(it=>it.select==1);
  73. if(excluArr.length>0){//有互斥
  74. if(filArr1.length==0){//无选中
  75. this.exclusion = 999;
  76. }else{
  77. this.exclusion = filArr1[0].exclusion;
  78. }
  79. }
  80. let temp = concatVal(data);
  81. const newData = Object.assign({},this.datas,{questionDetailList:data},{value:temp.value?temp.value:''},{valueP:temp.valueP?temp.valueP:''});
  82. this.$emit("updata",newData);
  83. },
  84. inpVal(val,index){//输入框失焦处理
  85. // 输入框回读
  86. let detailList = this.datas.questionDetailList;
  87. let currItem = detailList[index];
  88. currItem.value = val;
  89. // 输入框失焦重新拼接父级的value
  90. let temp = concatVal(detailList);
  91. this.datas.value = temp.value;
  92. this.datas.valueP = temp.valueP;
  93. this.$emit("updata",this.datas);
  94. },
  95. resetExc(){
  96. // 回读互斥项标识
  97. const arr = this.datas.questionDetailList;
  98. const filArr = arr.filter(it=>it.select==1);
  99. if(filArr.length > 0){
  100. this.exclusion = filArr[0].exclusion;
  101. }else{
  102. this.exclusion = 999;
  103. }
  104. }
  105. },
  106. watch:{
  107. item:{
  108. handler(newVal,oldVal){
  109. this.datas = JSON.parse(JSON.stringify(newVal));
  110. this.resetExc();
  111. },
  112. deep:true
  113. }
  114. },
  115. components:{
  116. MultiLineInput,
  117. OptionInp
  118. }
  119. }
  120. </script>
  121. <style lang="less" scoped>
  122. .check-wrap{
  123. img{
  124. width:100%;
  125. }
  126. .list{
  127. color: #7C828E;
  128. margin:0 .1rem .1rem 0;
  129. padding: .12rem .1rem;
  130. display: inline-block;
  131. white-space: nowrap;
  132. img{
  133. width: .38rem;
  134. vertical-align: middle;
  135. }
  136. }
  137. .check{
  138. color: #4F50FF;
  139. }
  140. .exclu{
  141. color:#e6e7e9;
  142. }
  143. }
  144. </style>