SingleSelect.vue 9.2 KB

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