index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <div :class="{ search__col: layout === 'right' }">
  3. <el-row :gutter="20">
  4. <el-col :span="layout === 'right' ? 22 : 24">
  5. <el-form
  6. ref="ruleForm"
  7. inline
  8. :model="formInline"
  9. :rules="rules"
  10. :label-width="labelWidth"
  11. :label-position="labelPosition"
  12. :hide-required-asterisk="hideRequiredAsterisk"
  13. @submit.prevent
  14. >
  15. <el-form-item
  16. v-for="(item, $index) in data"
  17. :key="$index"
  18. :label="item.label"
  19. :prop="item.field"
  20. :rules="item.rules"
  21. >
  22. <template v-if="item.itemType === 'switch'">
  23. <el-switch
  24. v-model="formInline[item.field]"
  25. v-bind="{...getItemBindValue(item)}"
  26. @change="((val) => {changeVal(val, item)})"
  27. />
  28. </template>
  29. <template v-if="item.itemType === 'input'">
  30. <el-input
  31. v-model="formInline[item.field]"
  32. v-bind="{...getItemBindValue(item)}"
  33. @change="((val) => {changeVal(val, item)})"
  34. />
  35. </template>
  36. <template v-if="item.itemType === 'select'">
  37. <el-select
  38. v-model="formInline[item.field]"
  39. v-bind="{...getItemBindValue(item)}"
  40. @change="((val) => {changeVal(val, item)})"
  41. >
  42. <el-option
  43. v-for="v in item.options"
  44. :key="item.optionValue ? v[item.optionValue] : v.value"
  45. :value="item.optionValue ? v[item.optionValue] : v.value"
  46. :label="item.optionLabel ? v[item.optionLabel] : v.title"
  47. />
  48. </el-select>
  49. </template>
  50. <template v-if="item.itemType === 'radio'">
  51. <el-radio-group
  52. v-model="formInline[item.field]"
  53. @change="((val) => {changeVal(val, item)})"
  54. >
  55. <template v-if="item.radioType === 'radio'">
  56. <el-radio
  57. v-for="v in item.options"
  58. :key="item.optionValue ? v[item.optionValue] : v.value"
  59. v-bind="{...getItemBindValue(item)}"
  60. :label="item.optionValue ? v[item.optionValue] : v.value"
  61. >
  62. {{ item.optionLabel ? v[item.optionLabel] : v.label }}
  63. </el-radio>
  64. </template>
  65. <template v-else-if="item.radioType === 'button'">
  66. <el-radio-button
  67. v-for="v in item.options"
  68. :key="item.optionValue ? v[item.optionValue] : v.value"
  69. v-bind="{...getItemBindValue(item)}"
  70. :label="item.optionValue ? v[item.optionValue] : v.value"
  71. >
  72. {{ item.optionLabel ? v[item.optionLabel] : v.label }}
  73. </el-radio-button>
  74. </template>
  75. </el-radio-group>
  76. </template>
  77. <!-- element近期会新增treeSelect组件,所以不打算在自己维护一套。等待ing -->
  78. <!-- <template v-if="item.itemType === 'treeSelect'">
  79. <el-tree-select
  80. v-model:value="formInline[item.field]"
  81. :size="item.size"
  82. :dropdown-style="item.dropdownStyle"
  83. :tree-data="item.options"
  84. :placeholder="item.placeholder"
  85. :tree-checkable="item.treeCheckable"
  86. :max-tag-count="item.maxTagCount"
  87. :tree-default-expand-all="item.treeDefaultExpandAll"
  88. :allow-clear="item.allowClear"
  89. style="min-width: 201px;"
  90. @change="((val) => {changeVal(val, item)})"
  91. >
  92. <template #title="{ title }">
  93. <span>{{ title }}</span>
  94. </template>
  95. </el-tree-select>
  96. </template> -->
  97. <template v-if="item.itemType === 'timePicker'">
  98. <el-time-picker
  99. v-model="formInline[item.field]"
  100. v-bind="{...getItemBindValue(item)}"
  101. @change="((val) => {changeVal(val, item)})"
  102. />
  103. </template>
  104. <template v-if="item.itemType === 'timeSelect'">
  105. <el-time-select
  106. v-model="formInline[item.field]"
  107. v-bind="{...getItemBindValue(item)}"
  108. @change="((val) => {changeVal(val, item)})"
  109. />
  110. </template>
  111. <template v-if="item.itemType === 'datePicker' || item.itemType === 'dateTimePicker'">
  112. <el-date-picker
  113. v-model="formInline[item.field]"
  114. v-bind="{...getItemBindValue(item)}"
  115. @change="((val) => {changeVal(val, item)})"
  116. />
  117. </template>
  118. </el-form-item>
  119. <el-form-item v-if="data.length > 0 && layout === 'classic'">
  120. <el-button
  121. type="primary"
  122. icon="el-icon-search"
  123. @click="submitForm"
  124. >
  125. 查询
  126. </el-button>
  127. <el-button
  128. v-if="showReset"
  129. icon="el-icon-refresh-right"
  130. @click="resetForm"
  131. >
  132. 重置
  133. </el-button>
  134. </el-form-item>
  135. </el-form>
  136. </el-col>
  137. <el-col :span="layout === 'right' ? 2 : 24">
  138. <div
  139. v-if="data.length > 0 && (layout === 'bottom' || layout === 'right')"
  140. class="search__bottom"
  141. :class="{ 'search__bottom--col': layout === 'right' }"
  142. >
  143. <div class="search__bottom--button">
  144. <el-button
  145. type="primary"
  146. icon="el-icon-search"
  147. @click="submitForm"
  148. >
  149. 查询
  150. </el-button>
  151. </div>
  152. <div class="search__bottom--button">
  153. <el-button
  154. v-if="showReset"
  155. :style="{
  156. 'margin-left': layout !== 'right' ? '15px' : '0',
  157. 'margin-top': layout === 'right' ? '27px' : '0'
  158. }"
  159. icon="el-icon-refresh-right"
  160. @click="resetForm"
  161. >
  162. 重置
  163. </el-button>
  164. </div>
  165. </div>
  166. </el-col>
  167. </el-row>
  168. </div>
  169. </template>
  170. <script lang="ts">
  171. import { defineComponent, PropType, watch, ref, unref } from 'vue'
  172. import { deepClone } from '@/utils'
  173. export default defineComponent({
  174. name: 'Search',
  175. props: {
  176. // 表单域标签的宽度,例如 '50px'。作为 Form 直接子元素的 form-item 会继承该值。支持 auto。
  177. labelWidth: {
  178. type: String as PropType<string>,
  179. default: ''
  180. },
  181. labelPosition: {
  182. type: String as PropType<'right' | 'left' | 'top'>,
  183. default: 'right'
  184. },
  185. // 隐藏所有表单项的必选标记
  186. hideRequiredAsterisk: {
  187. type: Boolean as PropType<boolean>,
  188. default: true
  189. },
  190. // 表单数据对象
  191. data: {
  192. type: Object as PropType<{ [key: string]: any }>,
  193. default: () => {}
  194. },
  195. // 表单验证规则
  196. rules: {
  197. type: Object as PropType<{ [key: number]: any }>,
  198. default: () => []
  199. },
  200. // 是否显示重置按钮
  201. showReset: {
  202. type: Boolean as PropType<boolean>,
  203. default: true
  204. },
  205. // 是否显示导出按钮
  206. showExport: {
  207. type: Boolean as PropType<boolean>,
  208. default: false
  209. },
  210. // 风格
  211. layout: {
  212. type: String as PropType<'classic' | 'bottom' | 'right'>,
  213. default: 'classic'
  214. }
  215. },
  216. emits: ['search-submit', 'reset-submit', 'change'],
  217. setup(props, { emit }) {
  218. const ruleForm = ref<HTMLElement | null>(null)
  219. const formInline = ref<{ [key: string]: any }>({})
  220. watch(
  221. () => props.data,
  222. (data) => {
  223. initForm(data)
  224. },
  225. {
  226. deep: true,
  227. immediate: true
  228. }
  229. )
  230. function getItemBindValue(item: any) {
  231. const delArr: string[] = ['label', 'itemType', 'value', 'field']
  232. const obj = deepClone(item)
  233. for (const key in obj) {
  234. if (delArr.indexOf(key) !== -1) {
  235. delete obj[key]
  236. }
  237. }
  238. return obj
  239. }
  240. function initForm(data: any): void {
  241. for (const v of data) {
  242. formInline.value[v.field] = formInline.value[v.field] || v.value
  243. }
  244. }
  245. async function submitForm(): Promise<void> {
  246. const form = unref(ruleForm) as any
  247. if (!form) return
  248. try {
  249. form.validate((valid: boolean) => {
  250. if (valid) {
  251. emit('search-submit', unref(formInline))
  252. } else {
  253. console.log('error submit!!')
  254. return false
  255. }
  256. })
  257. } catch (err) {
  258. console.log(err)
  259. }
  260. }
  261. async function resetForm(): Promise<void> {
  262. const form = unref(ruleForm) as any
  263. if (!form) return
  264. await form.resetFields()
  265. emit('reset-submit', unref(formInline))
  266. }
  267. function changeVal(val: any, item: any): void {
  268. if (item.onChange) {
  269. emit('change', {
  270. field: item.field,
  271. value: unref(formInline.value[item.field])
  272. })
  273. }
  274. }
  275. return {
  276. getItemBindValue,
  277. ruleForm,
  278. formInline,
  279. submitForm,
  280. resetForm,
  281. changeVal
  282. }
  283. }
  284. })
  285. </script>
  286. <style lang="less" scoped>
  287. .ant-form-inline {
  288. .ant-form-item {
  289. min-height: 60px;
  290. }
  291. .ant-form-item-with-help {
  292. margin-bottom: 0;
  293. }
  294. }
  295. .search__bottom {
  296. text-align: center;
  297. padding-bottom: 20px;
  298. .search__bottom--button {
  299. display: inline-block;
  300. }
  301. }
  302. .search__bottom--col {
  303. padding-bottom: 0;
  304. margin-top: 5px;
  305. position: relative;
  306. .search__bottom--button {
  307. display: inline-block;
  308. }
  309. }
  310. .search__bottom--col::before {
  311. content: "";
  312. width: 1px;
  313. height: 100%;
  314. border-left: 1px solid #d9d9d9;
  315. position: absolute;
  316. top: 0;
  317. left: 0;
  318. }
  319. </style>