CheckBox.vue 4.9 KB

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