index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div class="FormComponent">
  3. <div
  4. class="FormItemComponent"
  5. :style="setWidth"
  6. >
  7. <div
  8. v-for="(item,index) in dataColRol"
  9. :key="index"
  10. class="FormItemComponent-item"
  11. :style="setDiv(item)"
  12. >
  13. <component
  14. :is="item.component"
  15. :ref="'component_'+index"
  16. :index="index"
  17. :items="item.item"
  18. :readonly="readonly"
  19. @inputChange="inputChange"
  20. />
  21. </div>
  22. </div>
  23. <p v-if="buttonType">
  24. <el-button type="primary" plain icon="el-icon-search" @click="search">搜索</el-button>
  25. <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
  26. </p>
  27. </div>
  28. </template>
  29. <script>
  30. import Vue from "vue";
  31. import layoutAlgorithm from "@/api/layoutAlgorithm";
  32. export default {
  33. name: "FormItemComponent",
  34. props: {
  35. formItemLists: {
  36. type: Array,
  37. default() {
  38. return [];
  39. }
  40. },
  41. buttonType: {
  42. type: Boolean,
  43. default() {
  44. return true;
  45. }
  46. },
  47. defaultColumn: {
  48. type: Number,
  49. default: 4
  50. },
  51. readonly: {
  52. type: Boolean,
  53. default: false
  54. }
  55. },
  56. computed: {
  57. // 通过layoutAlgorithm算法得到对应的位置坐标
  58. dataColRol() {
  59. const list = layoutAlgorithm(this.defaultColumn, this.currentFormList);
  60. return Object.keys(list).reduce((temp, current) => {
  61. // 计算显示行数
  62. list[current].component = Vue.extend(list[current].component);
  63. temp.push(list[current]);
  64. return temp;
  65. }, []);
  66. },
  67. // 计算属性的 div 的坐标起始点
  68. setDiv() {
  69. return item =>
  70. ` grid-column:${item.x}/${item.col + item.x};grid-row:${
  71. item.y
  72. }/${item.y + item.row};`;
  73. },
  74. // 计算属性的 div的排列格式
  75. setWidth() {
  76. // `this` 指向 vm 实例
  77. const columns = Number(this.defaultColumn) || 4;
  78. return `grid-template-columns: repeat(${columns},${100 / columns}%`;
  79. }
  80. },
  81. watch: {
  82. formItemLists() {
  83. this.currentFormList = this.formItemLists.concat([]);
  84. }
  85. },
  86. data() {
  87. return {
  88. // defaultColumn:4, //默认一行4列
  89. formData: {}, //保存form中输入数据
  90. currentFormList: []
  91. };
  92. },
  93. created() {
  94. this.currentFormList = this.formItemLists.concat([]);
  95. console.log(this.formItemLists,999999999)
  96. },
  97. methods: {
  98. inputChange(value, items, type) {
  99. //有数据改变时
  100. // if(Object.prototype.toString.call(value) === '[object Array]' && (!value[0] || !value[1])){
  101. // delete this.formData[items.filed]
  102. // }else{
  103. // this.formData[items.filed] = value
  104. // }
  105. if (type && type === "select") {
  106. this.formData[items.slotfiled] = value;
  107. } else {
  108. this.formData[items.filed] = value;
  109. }
  110. this.$emit("formChange", this.formData);
  111. },
  112. search() {
  113. this.$emit("search", this.formData);
  114. },
  115. reset() {
  116. this.formData = {};
  117. this.currentFormList = this.currentFormList.concat([]);
  118. this.$emit("search", this.formData);
  119. }
  120. }
  121. };
  122. </script>
  123. <style lang="scss" scoped>
  124. .FormComponent {
  125. padding: 2px 10px 10px 10px;
  126. // border: 1px solid rgba(228, 228, 228, 1);
  127. }
  128. .FormComponent > p {
  129. padding-left: 108px;
  130. margin-top: 10px;
  131. display: flex;
  132. width: 300px;
  133. > button {
  134. margin-right: 10px;
  135. }
  136. }
  137. .FormItemComponent > div {
  138. /*border:1px solid #fff;*/
  139. box-sizing: border-box;
  140. }
  141. .FormItemComponent {
  142. display: grid;
  143. grid-template-columns: repeat(4, 25%);
  144. grid-auto-rows: minmax(auto);
  145. }
  146. </style>