CheckBox.vue 5.2 KB

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