CheckBox.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <div class="check-wrap bgques" v-if="item" :style="getStyle(detail,slide)">
  3. <p v-for="(it,index) in datas.questionDetailList"
  4. :key="it.id"
  5. :class="['list',{'block':((it.description||it.name).indexOf('${'))!=-1}]" >
  6. <span v-if="((it.description||it.name).indexOf('${'))==-1"
  7. :class="['radioCheck',{'check':it.select==1},{'exclu':exclusion !==999 && it.exclusion !== exclusion}]"
  8. @click="handleClick(it,index,'',true,false)"
  9. >{{it.description||it.name}}</span>
  10. <OptionInp v-else
  11. :item="it"
  12. ref="inp"
  13. @handleInp="inpVal($event,index)"
  14. @handleSelec="handleClick(it,index,true,false,true)"
  15. :exclu="exclusion !==999 && it.exclusion !== exclusion"
  16. />
  17. </p>
  18. </div>
  19. </template>
  20. <script type="text/javascript">
  21. import {imageUrlPrefix,patt,concatVal} from '@utils/tools.js';
  22. import icon from '../images/check-default.png';
  23. import checkIcon from '../images/check.png';
  24. import MultiLineInput from '../common/MultiLineInput.vue';
  25. import OptionInp from '../common/OptionInp.vue';
  26. export default{
  27. name:'CheckBox',
  28. props:{
  29. item:{
  30. default:''
  31. },
  32. slide:{
  33. default:false,
  34. type:Boolean
  35. },
  36. detail:{
  37. default:2,
  38. type:Number||String
  39. }
  40. },
  41. data(){
  42. return{
  43. imgUrl:imageUrlPrefix,
  44. defaultPic:icon,
  45. check:checkIcon,
  46. datas:{},
  47. exclusion:999 //互斥
  48. }
  49. },
  50. created(){
  51. // this.datas = JSON.parse(JSON.stringify(this.item));
  52. this.datas = this.item;
  53. this.resetExc();
  54. },
  55. methods:{
  56. getStyle(detail,slide){
  57. if(detail == 1){
  58. if(slide){
  59. return {'display':'block','background-color': '#F9F9F9'}
  60. }else{
  61. return {'display':'none'}
  62. }
  63. }else{
  64. return {'display':'block'}
  65. }
  66. },
  67. handleClick(it,index,flag,flg,sign){
  68. if(flg){
  69. document.activeElement.blur();
  70. document.activeElement.scrollIntoViewIfNeeded(true);
  71. setTimeout(()=>{
  72. document.activeElement.scrollIntoViewIfNeeded(true);
  73. },300)
  74. }
  75. const that = this;
  76. const list = this.datas;
  77. let data = list.questionDetailList&&list.questionDetailList.slice(0);
  78. // 处理互斥
  79. const excluArr = data.filter(it=>it.exclusion==1);
  80. const filArr = data.filter(it=>it.select==1);
  81. if(excluArr.length>0){//有互斥
  82. if(filArr.length>0){//有选中
  83. if(it.exclusion !== filArr[0].exclusion){
  84. return
  85. }
  86. }
  87. this.exclusion = it.exclusion;
  88. }
  89. // 处理选中状态
  90. if(sign){
  91. for(let i=0;i<data.length; i++){
  92. if(i==index){
  93. if(data[i].value){
  94. data[index].select = 1;
  95. this.exclusion = it.exclusion;
  96. }else{
  97. data[i].select = 0
  98. }
  99. }
  100. }
  101. }else{
  102. for(let i=0;i<data.length; i++){
  103. if(i==index){
  104. if(data[i].select == 1){
  105. data[i].select = 0
  106. }else{
  107. data[index].select = 1;
  108. this.exclusion = it.exclusion;
  109. }
  110. }
  111. }
  112. }
  113. // if(flag){//输入框反选
  114. // data[index].select = 1;
  115. // this.exclusion = it.exclusion;
  116. // }else{
  117. // if(data[index].select){
  118. // data[index].select = 0;
  119. // }else{
  120. // data[index].select = 1;
  121. // this.exclusion = it.exclusion;
  122. // }
  123. // }
  124. // 处理取消-互斥
  125. const filArr1 = data.filter(it=>it.select==1);
  126. if(excluArr.length>0){//有互斥
  127. if(filArr1.length==0){//无选中
  128. this.exclusion = 999;
  129. }else{
  130. this.exclusion = filArr1[0].exclusion;
  131. }
  132. }
  133. let temp = concatVal(data,false,this.datas.flag);
  134. const newData = Object.assign({},this.datas,{questionDetailList:data},{value:temp.value?temp.value:''},{valueP:temp.valueP?temp.valueP:''});
  135. this.$emit("updata",newData);
  136. },
  137. inpVal(val,index){//输入框失焦处理
  138. // 输入框回读
  139. let detailList = this.datas.questionDetailList;
  140. let currItem = detailList[index];
  141. currItem.value = val;
  142. // 输入框失焦重新拼接父级的value
  143. let temp = concatVal(detailList,false,this.datas.flag);
  144. this.datas.value = temp.value;
  145. this.datas.valueP = temp.valueP;
  146. this.$emit("updata",this.datas);
  147. },
  148. resetExc(){
  149. // 回读互斥项标识
  150. const arr = this.datas.questionDetailList;
  151. const filArr = arr.filter(it=>it.select==1);
  152. if(filArr.length > 0){
  153. this.exclusion = filArr[0].exclusion;
  154. }else{
  155. this.exclusion = 999;
  156. }
  157. }
  158. },
  159. watch:{
  160. item:{
  161. handler(newVal,oldVal){
  162. this.datas = JSON.parse(JSON.stringify(newVal));
  163. this.resetExc();
  164. },
  165. deep:true
  166. }
  167. },
  168. components:{
  169. MultiLineInput,
  170. OptionInp
  171. }
  172. }
  173. </script>
  174. <style lang="less" scoped>
  175. @import '../less/base.less';
  176. .check-wrap{
  177. .bgques;
  178. img{
  179. width:100%;
  180. }
  181. .block{
  182. width:100%;
  183. }
  184. .list{
  185. color: #colors[text];
  186. margin:0 .1rem .07rem 0;
  187. padding: .1rem .1rem .1rem 0;
  188. display: inline-block;
  189. white-space: nowrap;
  190. overflow-x: hidden;
  191. img{
  192. width: .38rem;
  193. vertical-align: middle;
  194. }
  195. >span{
  196. white-space: normal;
  197. vertical-align: middle;
  198. }
  199. }
  200. .radioCheck {
  201. display: inline-block;
  202. line-height: .66rem;
  203. // min-width: 2rem;
  204. // text-align: center;
  205. padding:0 .2rem;
  206. box-sizing: border-box;
  207. border-radius: .38rem;
  208. border: 1px solid #DFE0E4;
  209. }
  210. .check{
  211. color: #colors[theme];
  212. border: 1px solid #colors[theme];
  213. }
  214. .exclu{
  215. color:#colors[exclu];
  216. }
  217. .iptCheck {
  218. color: #colors[theme];
  219. }
  220. }
  221. </style>