SingleSelect.vue 8.2 KB

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