CheckBox.vue 4.7 KB

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