SearchForm.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {
Form,
Input,
DatePicker,
Switch,
Row,
Col,
Button
} from 'ant-design-vue'
import moment from 'moment'
import BSelect from '../Select/select' // 组件得使用对应的二次封装后的组件
import styles from './search.module.less'

const FormItem = Form.Item
const RangePicker = DatePicker.RangePicker

/**
* searchList对应的格式
* [
* {
* // label: '商户名称',
* // key: 'merchantName',
* // type: 'select',
* // options: []
* // },
* {
* label: '主题',
* key: 'subject',
* type: 'input'
* }, {
* label: '状态',
* key: 'status',
* type: 'select',
* options: this.$u.getSelectOptionsByDataMap('mailResult', this.dataMap)
* }, {
* label: '完成日期',
* key: 'time',
* type: 'rangePicker'
* }
* ]
*
*
const Search = {
name: 'BSearch',
components: {
BSelect
},
props: {
searchList: {
type: Array,
required: true
}
},
data () {
return {
form: this.$form.createForm(this)
}
},
methods: {
// 转换日期格式
getStartTimeAndEndTime (values) {
const { time } = values
if (!time || time.length === 0) {
return {
...values,
beginTime: '',
endTime: ''
}
} else {
let beginTime = this.$u.formatDateByMilliSeconds(moment(time[0]))
let endTime = this.$u.formatDateByMilliSeconds(moment(time[1]))
beginTime = beginTime.split(' ')[0]
endTime = endTime.split(' ')[0]
beginTime = new Date(beginTime)
endTime = new Date(endTime)
return {
...values,
beginTime,
endTime
}
}
},
// 查询按钮
searchHandler () {
this.form.validateFields((e, values) => {
values = this.getStartTimeAndEndTime(values)
this.$emit('on-search', values)
})
},
// 重置按钮
resetHandler () {
this.form.resetFields()
this.$emit('on-search', {})
},
// 构建form-item
renderFormItem (fields) {
const { getFieldDecorator } = this.form
const { label, key, type, options, openKey = '开启', closeKey = '关闭', optionKey, optionValue, keyValue } = fields
return (
<FormItem label={label}>
{
getFieldDecorator(key, {})(
type === 'input' ? (
<Input allowClear placeholder={`请输入${label}`}></Input>
) : type === 'select' ? (
<BSelect
keyValue={keyValue}
optionKey={optionKey}
optionValue={optionValue}
loadOptions={options}
allowClear
showSearch
optionFilterProp="title"
placeholder={`请选择${label}`}
></BSelect>
) : type === 'switch' ? (
<Switch checkedChildren={openKey} unCheckedChildren={closeKey}></Switch>
) : type === 'rangePicker' ? (
<RangePicker format={['YYYY-MM-DD', 'YYYY-MM-DD']}></RangePicker>
) : type === 'datePicker' ? (
<DatePicker style="width: 100%;"></DatePicker>
) : (
<Input allowClear></Input>
)
)
}
</FormItem>
)
},
renderCols () {
return (
this.searchList.map((it, id) => {
return (
<Col md={8} sm={24} key={id}>
{this.renderFormItem(it)}
</Col>
)
})
)
}
},
render () {
// slot 分left 和right
// left:用户自定义按钮,比如新增导入导出之类的
// right: right有默认值,可以覆盖
const { right } = this.$slots
const classes = !right ? 'search-wrap-space-between' : 'search-wrap-space-right'
return (
<div class="table-page-search-wrapper">
<Form layout="inline">
<Row gutter={48}>
{this.renderCols()}
<Col span={24}>
<span class={styles[classes]}>
<span class={styles['search-left']}>
{
Object.keys(this.$slots).map(name => {
return this.$slots[name]
})
}
</span>
{
!right ? (
<span class={styles['search-right']}>
<Button type="primary" onClick={this.searchHandler}>查询</Button>
<Button onClick={this.resetHandler}>重置</Button>
</span>
) : null
}
</span>
</Col>
</Row>
</Form>
</div>
)
}
}

export default Search

search.module.less

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
.search-wrap-space-between {
display: flex;
flex-direction: row;
justify-content: space-between;
align-content: center;
}
.search-warp-space-right {
display: flex;
flex-direction: row;
justify-content: flex-end;
align-content: center;
}
.search-left {
display: flex;
button {
&:not(:first-child) {
margin-left: 10px;
}
}
}
.search-right {
display: flex;
button {
&:not(:first-child) {
margin-left: 10px;
}
}
}