CheckBox.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.name.indexOf('${'))==-1" :class="[{'check':it.select==1},{'exclu':exclusion !==999 && it.exclusion !== exclusion}]">{{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} 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. if(data[index].name.indexOf("${") != -1){
  72. console.log("该选项有输入框",data[index])
  73. that.$refs.inp.handleBlur();
  74. }
  75. }
  76. // 处理取消-互斥
  77. const filArr1 = data.filter(it=>it.select==1);
  78. if(excluArr.length>0){//有互斥
  79. if(filArr1.length==0){//无选中
  80. this.exclusion = 999;
  81. }else{
  82. this.exclusion = filArr1[0].exclusion;
  83. }
  84. }
  85. let value = "";
  86. for(let k in data){
  87. if(data[k].select==1){
  88. value += data[k].name + ','
  89. }
  90. }
  91. const newData = Object.assign({},this.datas,{questionDetailList:data},{value:value?value.substring(0,value.length-1):''})
  92. this.$emit("updata",newData);
  93. },
  94. inpVal(val,index){//输入框失焦处理
  95. let valueStr = this.datas.value;
  96. if(valueStr){
  97. // let patt = /\$\{[^\]]+\}/g;
  98. let newVal = val;
  99. const str = valueStr.replace(patt,newVal);
  100. this.datas.value = str;
  101. }
  102. // 输入框回读
  103. let detailList = this.datas.questionDetailList;
  104. let currItem = detailList[index];
  105. currItem.value = val;
  106. this.$emit("updata",this.datas);
  107. },
  108. resetExc(){
  109. // 回读互斥项标识
  110. const arr = this.datas.questionDetailList;
  111. const filArr = arr.filter(it=>it.select==1);
  112. if(filArr.length > 0){
  113. this.exclusion = filArr[0].exclusion;
  114. }else{
  115. this.exclusion = 999;
  116. }
  117. }
  118. },
  119. watch:{
  120. item:{
  121. handler(newVal,oldVal){
  122. this.datas = JSON.parse(JSON.stringify(newVal));
  123. this.resetExc();
  124. },
  125. deep:true
  126. }
  127. },
  128. components:{
  129. MultiLineInput,
  130. OptionInp
  131. }
  132. }
  133. </script>
  134. <style lang="less" scoped>
  135. .check-wrap{
  136. img{
  137. width:100%;
  138. }
  139. .list{
  140. color: #7C828E;
  141. margin:0 .1rem .1rem 0;
  142. padding: .12rem .1rem;
  143. display: inline-block;
  144. white-space: nowrap;
  145. img{
  146. width: .38rem;
  147. vertical-align: middle;
  148. }
  149. }
  150. .check{
  151. color: #4F50FF;
  152. }
  153. .exclu{
  154. color:#e6e7e9;
  155. }
  156. }
  157. </style>