SingleSelect.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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="rows[i].isBan || 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="rows[i].isNone || 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 ||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. <div class="buttonBox">
  22. <div class="bottomPartMid bottomPartMidss fl">
  23. <p><span class="el-icon-arrow-up" @click="toggleTopDownList(1)"></span></p>
  24. <p><span class="el-icon-arrow-down" @click="toggleTopDownList(2)"></span></p>
  25. </div>
  26. </div>
  27. <el-col v-for="(it,i) in rows" :key="i">
  28. <el-input v-model="rows[i].name"
  29. v-bind:class="{select:focusOn==i}"
  30. @focus="selectRow(i)"
  31. ref = "inputName"
  32. @input="HandleInputName(i, rows[i].name)"
  33. @blur="emitValues"></el-input>
  34. <el-tag type="info" size="mini" v-if="showTag(it)">{{showTag(it)}}</el-tag>
  35. </el-col>
  36. <el-button @click="addRow">+</el-button>
  37. </div>
  38. </el-form>
  39. </div>
  40. </template>
  41. <style lang="less">
  42. @import "../../less/common.less";
  43. .el-checkbox-button--small .el-checkbox-button__inner{
  44. font-size: 14px;
  45. }
  46. .el-checkbox-button.is-disabled:first-child .el-checkbox-button__inner{
  47. border-color: @disableColor;
  48. border-left-color: @disableColor;
  49. color: @disableColor;
  50. }
  51. .el-checkbox-button:last-child .el-checkbox-button__inner{
  52. border-radius: 3px;
  53. border-color: @adminBase;
  54. color: @adminBase;
  55. margin-right: 15px;
  56. }
  57. .el-checkbox-button.is-checked:first-child .el-checkbox-button__inner{
  58. background-color:@adminBase;
  59. border-left-color:@adminBase;
  60. color:#fff;
  61. }
  62. .el-checkbox-group{
  63. display: inline-block;
  64. }
  65. .operation-row{
  66. margin-left:150px;
  67. .del{
  68. margin-left: 150px;
  69. }
  70. .tip {
  71. color: #22ccc8;
  72. }
  73. }
  74. .main-area{
  75. width: 290px;
  76. margin:20px 150px;
  77. .el-tag{
  78. margin-left: 10px;
  79. }
  80. .el-col .el-input {
  81. width: 200px;
  82. display: inline-block;
  83. &.select{
  84. input{
  85. border-color: @adminBase;
  86. }
  87. }
  88. }
  89. .buttonBox {
  90. width: 10%;
  91. position: absolute;
  92. right: 170px;
  93. margin-top: 20px;
  94. }
  95. .bottomPartMid {
  96. width: 8%;
  97. p {
  98. width: 100%;
  99. text-align: center;
  100. span {
  101. cursor: pointer;
  102. display: inline-block;
  103. width: 30px;
  104. height: 40px;
  105. line-height: 40px;
  106. margin: 0 auto;
  107. border: 1px solid @icssBorder;
  108. margin-bottom: 15px;
  109. font-size: 18px;
  110. }
  111. }
  112. }
  113. .el-button{
  114. width: 200px;
  115. }
  116. }
  117. </style>
  118. <script>
  119. import utils from '@api/utils.js';
  120. import Vue from 'vue';
  121. export default {
  122. props:['type','options','ascription','sexType','isEditOrCopy'],
  123. data(){
  124. return {
  125. maps:{isBan:'同“伴”',isNone:'同“无”',defaultSelect:'默认选中',isError:'异常选项'},
  126. rows:[{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''}],
  127. focusOn:-1
  128. }
  129. },
  130. mounted(){
  131. const {options} = this.$props;
  132. if(options){
  133. const arr = options.map((it)=>{
  134. 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:''};
  135. });
  136. const arrLen = arr.length;
  137. if(arrLen < 4) {
  138. for (let i = 0; i < 4 - arrLen; i++) {
  139. arr.push({name:'',defaultSelect:'',isBan:'',isNone:'',isError:''})
  140. }
  141. }
  142. this.rows = arr;
  143. const items = utils.simpleOptionData(this.rows);
  144. this.$emit('pushValues',items);
  145. }
  146. },
  147. computed:{
  148. showTag:function(){
  149. return function(it){
  150. let name = Object.keys(this.maps).find((item)=>{
  151. return it[item];
  152. });
  153. return this.maps[name];
  154. }
  155. },
  156. hasDefault:function(){ //是否有选项默认选中
  157. let index =this.rows.findIndex((it)=>{
  158. return it.defaultSelect;
  159. });
  160. return index;
  161. },
  162. hasNone:function(){ //是否有选项同无
  163. let index =this.rows.findIndex((it)=>{
  164. return it.isNone;
  165. });
  166. return index;
  167. },
  168. hasBan:function(){ //是否有选项同伴
  169. let index =this.rows.findIndex((it)=>{
  170. return it.isBan;
  171. });
  172. return index;
  173. },
  174. hasError:function(){ //是否为正确选项
  175. let index =this.rows.findIndex((it)=>{
  176. return it.isError;
  177. });
  178. return index;
  179. }
  180. },
  181. watch: {
  182. ascription(newVal, preVal) {
  183. if (newVal != preVal) {
  184. if (JSON.stringify(newVal) != JSON.stringify(preVal)) {
  185. this.rows = [{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''}];
  186. }
  187. }
  188. },
  189. type(newVal, preVal) {
  190. if (newVal != preVal) {
  191. if (JSON.stringify(newVal) != JSON.stringify(preVal)) {
  192. if(this.isEditOrCopy) {
  193. for(let i = 0, len = this.rows.length; i < len; i++) {
  194. this.rows[i].defaultSelect = ''
  195. this.rows[i].isBan = ''
  196. this.rows[i].isNone = ''
  197. this.rows[i].isError = ''
  198. }
  199. } else {
  200. this.rows = [{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''}];
  201. }
  202. }
  203. }
  204. },
  205. sexType(newVal, preVal) {
  206. if (newVal != preVal) {
  207. if (JSON.stringify(newVal) != JSON.stringify(preVal)) {
  208. this.rows = [{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''},{name:'',defaultSelect:'',isBan:'',isNone:'',isError:''}];
  209. }
  210. }
  211. },
  212. },
  213. methods:{
  214. addRow(){
  215. this.rows.push({name:'',defaultSelect:'',isBan:'',isNone:'',isError:''});
  216. },
  217. selectRow(index){
  218. this.focusOn = index;
  219. },
  220. emitValues(i,type,flag){
  221. if(typeof i =='number'&&this.focusOn == -1){
  222. this.$message({
  223. message: '请先选择要操作的行',
  224. type: 'warning',
  225. showClose: true,
  226. });
  227. return;
  228. }
  229. const items = utils.simpleOptionData(this.rows);
  230. this.$emit('pushValues',items);
  231. },
  232. HandleInputName(i, name) {
  233. if(name.length > 30) {
  234. Vue.set(this.rows[i], 'name', this.rows[i].name.slice(0, 30))
  235. this.$refs.inputName[i].currentValue = this.rows[i].name
  236. this.$message({
  237. message: '最多输入30个字',
  238. type: 'warning',
  239. showClose: true,
  240. });
  241. return
  242. }
  243. },
  244. toggleTopDownList(type){
  245. if(this.focusOn == '-1') {
  246. return
  247. }
  248. const rows = JSON.parse(JSON.stringify(this.rows))
  249. if(type == '1') {
  250. if(this.focusOn == '0') {
  251. return
  252. } else {
  253. let i = this.focusOn
  254. let tmp1 = rows[i]
  255. let tmp2 = rows[i-1]
  256. rows.splice(i-1,1,tmp1)
  257. rows.splice(i,1,tmp2)
  258. this.rows = [...rows]
  259. this.focusOn = i-1
  260. }
  261. } else if(type == '2') {
  262. if(this.focusOn == this.rows.length) {
  263. return
  264. } else {
  265. let i = this.focusOn
  266. let tmp1 = rows[i]
  267. let tmp2 = rows[i+1]
  268. rows.splice(i,1,tmp2)
  269. rows.splice(i+1,1,tmp1)
  270. this.rows = [...rows]
  271. this.focusOn = i+1
  272. }
  273. }
  274. const items = utils.simpleOptionData(this.rows);
  275. this.$emit('pushValues',items);
  276. },
  277. delRow(){
  278. if(this.focusOn==-1){
  279. this.$message({
  280. message: '请先选择要删除的行',
  281. type: 'warning',
  282. showClose: true,
  283. });
  284. return;
  285. }
  286. this.$alert('确定要删除该行吗?', '提示', {
  287. confirmButtonText: '确定',
  288. type: 'warning'
  289. }).then(() => {
  290. this.rows.splice(this.focusOn,1);
  291. this.focusOn = -1;
  292. this.emitValues();
  293. }).catch(() => {});
  294. }
  295. }
  296. }
  297. </script>