SingleSelect.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <div class="single-container">
  3. <el-form>
  4. <div class="operation-row">
  5. <el-checkbox-group size="small" v-for="(it,i) in rows" v-if="focusOn==i||(focusOn==-1&&i==0)" :key="1+i" v-model="rows[i].defaultSelect" @change="emitValues(i,'hasDefault',rows[i].defaultSelect)">
  6. <el-checkbox-button v-if=" !(ascription == 5 && type == 1 || type == 2)" :label="i" :disabled="hasNone==i||hasBan==i|| hasError==i || (hasDefault!=-1&&hasDefault!=i)">默认选中</el-checkbox-button>
  7. </el-checkbox-group>
  8. <el-checkbox-group size="small" v-for="(it,i) in rows" v-if="focusOn==i||(focusOn==-1&&i==0)" :key="2+i" v-model="rows[i].isNone" @change="emitValues(i,'hasNone',rows[i].isNone)">
  9. <el-checkbox-button v-if=" !(ascription == 5 && type == 1)" :label="i" :disabled="hasDefault==i||hasBan==i || hasError==i ||(hasNone!=-1&&hasNone!=i)">同“无”类型</el-checkbox-button>
  10. </el-checkbox-group>
  11. <el-checkbox-group size="small" v-for="(it,i) in rows" v-if="focusOn==i||(focusOn==-1&&i==0)" :key="3+i" v-model="rows[i].isBan" @change="emitValues(i,'hasBan',rows[i].isBan)">
  12. <el-checkbox-button v-if=" !(ascription == 5 && type == 1)" :label="i" :disabled="hasDefault==i||hasNone==i || hasError==i||(hasBan!=-1&&hasBan!=i)">同“伴”类型</el-checkbox-button>
  13. </el-checkbox-group>
  14. <el-checkbox-group size="small" v-for="(it,i) in rows" v-if="focusOn==i||(focusOn==-1&&i==0)" :key="4+i" v-model="rows[i].isError" @change="emitValues(i,'hasError',rows[i].isError)">
  15. <el-checkbox-button v-if="ascription==5 && type == 1" :label="i" :disabled="hasDefault==i||hasBan==i ||hasNone==i ||(hasError!=-1&&hasError==i)">标记异常选项</el-checkbox-button>
  16. </el-checkbox-group>
  17. <el-button type="danger" size="small" class="del" @click="delRow">删除</el-button>
  18. </div>
  19. <div class="main-area">
  20. <el-col v-for="(it,i) in rows" :key="i">
  21. <el-input v-model="rows[i].name"
  22. v-bind:class="{select:focusOn==i}"
  23. @focus="selectRow(i)"
  24. ref = "inputName"
  25. @input="HandleInputName(i, rows[i].name)"
  26. @blur="emitValues"></el-input>
  27. <el-tag type="info" size="mini" v-if="showTag(it)">{{showTag(it)}}</el-tag>
  28. </el-col>
  29. <el-button @click="addRow">+</el-button>
  30. </div>
  31. </el-form>
  32. </div>
  33. </template>
  34. <style lang="less">
  35. @import "../../less/common.less";
  36. .el-checkbox-button--small .el-checkbox-button__inner{
  37. font-size: 14px;
  38. }
  39. .el-checkbox-button.is-disabled:first-child .el-checkbox-button__inner{
  40. border-color: @disableColor;
  41. border-left-color: @disableColor;
  42. color: @disableColor;
  43. }
  44. .el-checkbox-button:last-child .el-checkbox-button__inner{
  45. border-radius: 3px;
  46. border-color: @adminBase;
  47. color: @adminBase;
  48. margin-right: 15px;
  49. }
  50. .el-checkbox-button.is-checked:first-child .el-checkbox-button__inner{
  51. background-color:@adminBase;
  52. border-left-color:@adminBase;
  53. color:#fff;
  54. }
  55. .el-checkbox-group{
  56. display: inline-block;
  57. }
  58. .operation-row{
  59. margin-left:150px;
  60. .del{
  61. margin-left: 150px;
  62. }
  63. }
  64. .main-area{
  65. width: 290px;
  66. margin:20px 150px;
  67. .el-tag{
  68. margin-left: 10px;
  69. }
  70. .el-col .el-input {
  71. width: 200px;
  72. display: inline-block;
  73. &.select{
  74. input{
  75. border-color: @adminBase;
  76. }
  77. }
  78. }
  79. .el-button{
  80. width: 200px;
  81. }
  82. }
  83. </style>
  84. <script>
  85. import utils from '@api/utils.js';
  86. import Vue from 'vue';
  87. export default {
  88. props:['type','options','ascription','sexType'],
  89. data(){
  90. return {
  91. maps:{isBan:'同“伴”',isNone:'同“无”',defaultSelect:'默认选中',isError:'异常选项'},
  92. rows:[{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''}],
  93. focusOn:-1
  94. }
  95. },
  96. mounted(){
  97. const {options} = this.$props;
  98. if(options){
  99. const arr = options.map((it)=>{
  100. return {name:it.name,defaultSelect:it.defaultSelect=='1'?true:false,isBan:it.code==1?true:'',isNone:it.code==2?true:'',isError:it.abnormal==1?true:''};
  101. });
  102. const arrLen = arr.length;
  103. if(arrLen < 4) {
  104. for (let i = 0; i < 4 - arrLen; i++) {
  105. arr.push({name:'',defaultSelect:'',isBan:'',isNone:'',isError:''})
  106. }
  107. }
  108. this.rows = arr;
  109. const items = utils.simpleOptionData(this.rows);
  110. this.$emit('pushValues',items);
  111. }
  112. },
  113. computed:{
  114. showTag:function(){
  115. return function(it){
  116. let name = Object.keys(this.maps).find((item)=>{
  117. return it[item];
  118. });
  119. return this.maps[name];
  120. }
  121. },
  122. hasDefault:function(){ //是否有选项默认选中
  123. let index =this.rows.findIndex((it)=>{
  124. return it.defaultSelect;
  125. });
  126. return index;
  127. },
  128. hasNone:function(){ //是否有选项同无
  129. let index =this.rows.findIndex((it)=>{
  130. return it.isNone;
  131. });
  132. return index;
  133. },
  134. hasBan:function(){ //是否有选项同伴
  135. let index =this.rows.findIndex((it)=>{
  136. return it.isBan;
  137. });
  138. return index;
  139. },
  140. hasError:function(){ //是否为正确选项
  141. let index =this.rows.findIndex((it)=>{
  142. return it.isError;
  143. });
  144. return index;
  145. }
  146. },
  147. watch: {
  148. ascription(newVal, preVal) {
  149. if (newVal != preVal) {
  150. if (JSON.stringify(newVal) != JSON.stringify(preVal)) {
  151. this.rows = [{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''}];
  152. }
  153. }
  154. },
  155. type(newVal, preVal) {
  156. if (newVal != preVal) {
  157. if (JSON.stringify(newVal) != JSON.stringify(preVal)) {
  158. this.rows = [{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''}];
  159. }
  160. }
  161. },
  162. sexType(newVal, preVal) {
  163. if (newVal != preVal) {
  164. if (JSON.stringify(newVal) != JSON.stringify(preVal)) {
  165. this.rows = [{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''}];
  166. }
  167. }
  168. },
  169. },
  170. methods:{
  171. addRow(){
  172. this.rows.push({name:'',defaultSelect:'',isBan:'',isNone:'',isError:''});
  173. },
  174. selectRow(index){
  175. this.focusOn = index;
  176. },
  177. emitValues(i,type,flag){
  178. if(typeof i =='number'&&this.focusOn == -1){
  179. this.$message({
  180. message: '请先选择要操作的行',
  181. type: 'warning',
  182. showClose: true,
  183. });
  184. return;
  185. }
  186. const items = utils.simpleOptionData(this.rows);
  187. this.$emit('pushValues',items);
  188. },
  189. HandleInputName(i, name) {
  190. if(name.length > 30) {
  191. Vue.set(this.rows[i], 'name', this.rows[i].name.slice(0, 30))
  192. this.$refs.inputName[i].currentValue = this.rows[i].namebvcx
  193. this.$message({
  194. message: '最多输入30个字',
  195. type: 'warning',
  196. showClose: true,
  197. });
  198. return
  199. }
  200. },
  201. delRow(){
  202. if(this.focusOn==-1){
  203. this.$message({
  204. message: '请先选择要删除的行',
  205. type: 'warning',
  206. showClose: true,
  207. });
  208. return;
  209. }
  210. this.$alert('确定要删除该行吗?', '提示', {
  211. confirmButtonText: '确定',
  212. type: 'warning'
  213. }).then(() => {
  214. this.rows.splice(this.focusOn,1);
  215. this.focusOn = -1;
  216. this.emitValues();
  217. }).catch(() => {});
  218. }
  219. }
  220. }
  221. </script>