Fieldlist.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {copyObj,rand,inArray} from "../util.js";
  2. const template=`
  3. <div class="fieldlist" :id="'fieldlist-'+id">
  4. <div class="row" v-if="label.length">
  5. <template v-for="(la,index) in label">
  6. <div class="field" :style="'width:'+parseLabelWidth[index]">{{la}}</div>
  7. </template>
  8. </div>
  9. <template v-for="(obj,row) in list">
  10. <div class="row">
  11. <template v-for="(v,k) in obj">
  12. <template v-if="inArray(keys,k)">
  13. <div class="field" :style="'width:'+parseFieldWidth[k]">
  14. <slot :name="k" :list="list" :row="row" :key="k">
  15. <el-input v-model="list[row][k]"></el-input>
  16. </slot>
  17. </div>
  18. </template>
  19. </template>
  20. <div class="field act" style="width: 15%;">
  21. <el-button type="danger" @click="removeRow(row)"><i class="fa fa-times"></i></el-button>
  22. <el-button type="primary" class="sortableButton"><i class="fa fa-arrows"></i></el-button>
  23. </div>
  24. </div>
  25. </template>
  26. <el-row :gutter="20" class="row">
  27. <el-col :span="4">
  28. <el-button type="success" @click="addRow"><i class="fa fa-plus"></i>&nbsp;追加</el-button>
  29. </el-col>
  30. </el-row>
  31. </div>
  32. `;
  33. const moveRow=function(array, oldIndex, newIndex) {
  34. if (newIndex >= array.length) {
  35. var k = newIndex - array.length;
  36. while ((k--) + 1) {
  37. array.push(undefined);
  38. }
  39. }
  40. array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
  41. return array;
  42. }
  43. export default {
  44. name: "Fieldlist",
  45. data: function () {
  46. return {
  47. id:'',
  48. list:[],
  49. addval:'',
  50. parseLabelWidth:[],
  51. parseFieldWidth:{}
  52. }
  53. },
  54. created:function (){
  55. this.id=rand(10000,99999);
  56. },
  57. watch:{
  58. list: {
  59. deep:true,
  60. handler:function (val){
  61. this.$emit('change',this.parseValue());
  62. }
  63. }
  64. },
  65. mounted:function (){
  66. let addval={};
  67. if(this.value){
  68. if(this.value instanceof Array){
  69. this.list=copyObj(this.value);
  70. }else{
  71. let list=[];
  72. for(let k in this.value){
  73. list.push({'0':k,'1':this.value[k]});
  74. }
  75. this.list=list;
  76. }
  77. }
  78. for(let k in this.keys){
  79. addval[this.keys[k]]='';
  80. }
  81. this.addval=addval;
  82. for(let k in this.keys){
  83. this.parseLabelWidth.push((80/this.keys.length)+'%');
  84. this.parseFieldWidth[this.keys[k]]=(80/this.keys.length)+'%';
  85. }
  86. this.rowDrop();
  87. },
  88. props:{
  89. keys:{
  90. type:Array,
  91. default: ['0','1']
  92. },
  93. value:{
  94. type:Object,
  95. default:null
  96. },
  97. label:{
  98. type:Array,
  99. default: ['键名','键值']
  100. },
  101. field:{
  102. type:String,
  103. default:''
  104. }
  105. },
  106. template:template,
  107. emits:['change'],
  108. methods:{
  109. inArray,
  110. rowDrop:function(){
  111. const tbody = document.querySelector('#fieldlist-'+this.id);
  112. const _this = this;
  113. Sortable.create(tbody, {
  114. draggable: ".row",
  115. animation: 100,
  116. handle:'.sortableButton',
  117. onEnd ({ newIndex, oldIndex }) {
  118. let list=copyObj(_this.list);
  119. _this.list=[];
  120. moveRow(list,oldIndex-1,newIndex-1);
  121. Vue.nextTick(()=>{
  122. _this.list=list;
  123. });
  124. }
  125. });
  126. },
  127. removeRow:function (row){
  128. this.list.splice(row,1);
  129. },
  130. addRow:function (){
  131. this.list.push(copyObj(this.addval));
  132. },
  133. parseValue:function (){
  134. if(this.keys.length==2 && this.keys[0]=='0' && this.keys[1]=='1'){
  135. let r={};
  136. for(let i=0;i<this.list.length;i++){
  137. if(!this.list[i]['0']){
  138. continue;
  139. }
  140. r[this.list[i]['0']]=this.list[i]['1'];
  141. }
  142. return this.field?{field:this.field,value:r}:r;
  143. }else{
  144. let r=copyObj(this.list);
  145. return this.field?{field:this.field,value:r}:r;
  146. }
  147. }
  148. }
  149. };