index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <m-card :shadow="shadow">
  3. <el-form
  4. :inline="true"
  5. :model="query"
  6. ref="form"
  7. label-width="80px"
  8. size="small"
  9. :show-message="false"
  10. inline-message
  11. @submit.native.prevent
  12. >
  13. <template v-for="(item, index) in items">
  14. <el-form-item
  15. v-if="!item.hidden"
  16. :key="index"
  17. v-bind="item"
  18. >
  19. <!-- 输入框 -->
  20. <el-input
  21. v-if="item.type === 'input'"
  22. v-model="query[item.prop]"
  23. @keydown.enter.native="onSubmit"
  24. :clearable="item.option?.clearable ?? true"
  25. v-bind="item.option"
  26. v-on="item.handles"
  27. ></el-input>
  28. <!-- 下拉框 -->
  29. <el-select
  30. v-if="item.type === 'select'"
  31. v-model="query[item.prop]"
  32. :clearable="item.option?.clearable ?? true"
  33. v-bind="item.option"
  34. v-on="item.handles"
  35. >
  36. <el-option
  37. v-for="option in item.option.items"
  38. :key="option.value"
  39. :label="option[item.option.labelText ?? option.label]"
  40. :value="option[item.option.labelValue ?? option.value]"
  41. ></el-option>
  42. </el-select>
  43. <el-autocomplete
  44. v-if="item.type === 'autocomplete'"
  45. v-model="query[item.prop]"
  46. v-bind="item.option"
  47. v-on="item.handles"
  48. >
  49. <template v-for="slot in item.slots" :slot="scope">
  50. <slot :name="`${item.prop}.${slot}`" :scope="scope"></slot>
  51. </template>
  52. </el-autocomplete>
  53. <!-- 时间选择器 -->
  54. <el-date-picker
  55. v-if="item.type === 'datePicker'"
  56. v-model="query[item.prop]"
  57. v-bind="item.option"
  58. v-on="item.handles"
  59. ></el-date-picker>
  60. <!-- 其他类型可以根据需要扩展 -->
  61. </el-form-item>
  62. </template>
  63. <el-form-item class="flex">
  64. <template v-if="items.length">
  65. <m-button class="ml-3" icon="el-icon-search" @click="onSubmit">查询</m-button>
  66. <m-button class="ml-3" icon="el-icon-refresh" @click="onReset">重置</m-button>
  67. </template>
  68. <slot name="button"></slot>
  69. </el-form-item>
  70. </el-form>
  71. </m-card>
  72. </template>
  73. <script>
  74. export default {
  75. name: 'm-search',
  76. props: {
  77. shadow: String,
  78. items: {
  79. type: Array,
  80. default: () => []
  81. },
  82. value: {
  83. type: Object,
  84. default: () => ({})
  85. }
  86. },
  87. data () {
  88. return {
  89. searchQuery: null,
  90. query: { ...this.value }
  91. }
  92. },
  93. watch: {
  94. value: {
  95. handler (newVal) {
  96. this.query = newVal
  97. },
  98. deep: true
  99. },
  100. query: {
  101. handler (newVal) {
  102. this.$emit('input', newVal)
  103. },
  104. deep: true
  105. }
  106. },
  107. methods: {
  108. onSubmit () {
  109. this.$emit('search', this.query)
  110. },
  111. onReset () {
  112. this.$refs.form.resetFields()
  113. this.$emit('search', this.query)
  114. }
  115. }
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. ::v-deep .el-form-item {
  120. margin: 0 !important;
  121. }
  122. .flex ::v-deep .el-form-item__content{
  123. display: flex;
  124. }
  125. </style>