CheckBox.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <div class="check-wrap bgques clearfix" 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 {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. defaultPic:icon,
  44. check:checkIcon,
  45. datas:{},
  46. exclusion:999 //互斥
  47. }
  48. },
  49. created(){
  50. // this.datas = JSON.parse(JSON.stringify(this.item));
  51. this.datas = this.item;
  52. this.resetExc();
  53. },
  54. methods:{
  55. getStyle(detail,slide){
  56. if(detail == 1){
  57. if(slide){
  58. return {'display':'block','background-color': '#F9F9F9'}
  59. }else{
  60. return {'display':'none'}
  61. }
  62. }else{
  63. return {'display':'block'}
  64. }
  65. },
  66. handleClick(it,index,flag,flg,sign){
  67. if(flg){
  68. document.activeElement.blur();
  69. document.activeElement.scrollIntoViewIfNeeded(true);
  70. setTimeout(()=>{
  71. document.activeElement.scrollIntoViewIfNeeded(true);
  72. },300)
  73. }
  74. const that = this;
  75. const list = this.datas;
  76. let data = list.questionDetailList&&list.questionDetailList.slice(0);
  77. // 处理互斥
  78. const excluArr = data.filter(it=>it.exclusion==1);
  79. const filArr = data.filter(it=>it.select==1);
  80. if(excluArr.length>0){//有互斥
  81. if(filArr.length>0){//有选中
  82. if(it.exclusion !== filArr[0].exclusion){
  83. return
  84. }
  85. }
  86. this.exclusion = it.exclusion;
  87. }
  88. // 处理选中状态
  89. if(sign){
  90. for(let i=0;i<data.length; i++){
  91. if(i==index){
  92. if(data[i].value){
  93. data[index].select = 1;
  94. this.exclusion = it.exclusion;
  95. }else{
  96. data[i].select = 0
  97. }
  98. }
  99. }
  100. }else{
  101. for(let i=0;i<data.length; i++){
  102. if(i==index){
  103. if(data[i].select == 1){
  104. data[i].select = 0
  105. }else{
  106. data[index].select = 1;
  107. this.exclusion = it.exclusion;
  108. }
  109. }
  110. }
  111. }
  112. // if(flag){//输入框反选
  113. // data[index].select = 1;
  114. // this.exclusion = it.exclusion;
  115. // }else{
  116. // if(data[index].select){
  117. // data[index].select = 0;
  118. // }else{
  119. // data[index].select = 1;
  120. // this.exclusion = it.exclusion;
  121. // }
  122. // }
  123. // 处理取消-互斥
  124. const filArr1 = data.filter(it=>it.select==1);
  125. if(excluArr.length>0){//有互斥
  126. if(filArr1.length==0){//无选中
  127. this.exclusion = 999;
  128. }else{
  129. this.exclusion = filArr1[0].exclusion;
  130. }
  131. }
  132. let temp = concatVal(data,false,this.datas.flag);
  133. const newData = Object.assign({},this.datas,{questionDetailList:data},{value:temp.value?temp.value:''},{valueP:temp.valueP?temp.valueP:''});
  134. this.$emit("updata",newData);
  135. },
  136. inpVal(val,index){//输入框失焦处理
  137. // 输入框回读
  138. let detailList = this.datas.questionDetailList;
  139. let currItem = detailList[index];
  140. currItem.value = val;
  141. // 输入框失焦重新拼接父级的value
  142. let temp = concatVal(detailList,false,this.datas.flag);
  143. this.datas.value = temp.value;
  144. this.datas.valueP = temp.valueP;
  145. this.$emit("updata",this.datas);
  146. },
  147. resetExc(){
  148. // 回读互斥项标识
  149. const arr = this.datas.questionDetailList;
  150. const filArr = arr.filter(it=>it.select==1);
  151. if(filArr.length > 0){
  152. this.exclusion = filArr[0].exclusion;
  153. }else{
  154. this.exclusion = 999;
  155. }
  156. }
  157. },
  158. watch:{
  159. item:{
  160. handler(newVal,oldVal){
  161. this.datas = JSON.parse(JSON.stringify(newVal));
  162. this.resetExc();
  163. },
  164. deep:true
  165. }
  166. },
  167. components:{
  168. MultiLineInput,
  169. OptionInp
  170. }
  171. }
  172. </script>
  173. <style lang="less" scoped>
  174. @import '../less/base.less';
  175. .check-wrap{
  176. .bgques;
  177. img{
  178. width:100%;
  179. }
  180. .block{
  181. width:100%;
  182. }
  183. .list{
  184. color: #colors[text];
  185. margin:0 .05rem;
  186. padding: .05rem;
  187. display: inline-block;
  188. white-space: nowrap;
  189. // overflow-x: hidden;
  190. float: left;
  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. color: #7C828E;
  206. padding:0 .2rem;
  207. box-sizing: border-box;
  208. border-radius: .38rem;
  209. border: 1px solid #dfe0e4;
  210. background-color: #ffffff;
  211. }
  212. .check{
  213. color: #fff;
  214. background-color: #6678FF;
  215. border: 1px solid #6678FF;
  216. }
  217. .exclu{
  218. color:#colors[exclu];
  219. }
  220. .iptCheck {
  221. color: #colors[theme];
  222. }
  223. }
  224. </style>