vue 函数组件解决动态渲染问题
1. 场景: 有一个检索池,检索条件可以自定义一个,检索条件对应的数据动态加载,检索条件对应的项不确定,有 sleect,input, dateRabge....
2. 需求遇到的问题
1. 方案1: 使用 v-html 解决,1. 不能解决数据动态加载问题,2. 不能解决项目是 innput select 动态的问题
2. 方案2:使用render方法,函数组件动态渲染
3. 解决步骤:
1. 定义条件枚举:
searchTemplates: [ { key: 'order_id', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName !== '' && ) } }, { key: 'original_order_no', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} this.highSearchFormal.order_id} />this.removeCondition(row, 'order_id') }> { row.relationShipName !== '' && ) } }, { key: 'execution_no', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} this.highSearchFormal.original_order_no} />this.removeCondition(row, 'original_order_no') }> { row.relationShipName !== '' && ) } }, { key: 'dispatch_id', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} this.highSearchFormal.execution_no} />this.removeCondition(row, 'execution_no') }> { row.relationShipName !== '' && ) } }, { key: 'consignee_name', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} this.highSearchFormal.dispatch_id} />this.removeCondition(row, 'dispatch_id') }> { row.relationShipName !== '' && ) } }, { key: 'state', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} {/* */} this.highSearchFormal.consignee_name} />this.removeCondition(row, 'consignee_name') }> { row.relationShipName !== '' && ) } }, { key: 'priority', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'state') }> { row.relationShipName !== '' && ) } }, { key: 'stocking_state', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'priority') }> { row.relationShipName !== '' && ) } }, { key: 'operation_state', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'stocking_state') }> { row.relationShipName !== '' && ) } }, { key: 'order_source', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'operation_state') }> { row.relationShipName !== '' && ) } }, { key: 'ros_state', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'order_source') }> { row.relationShipName !== '' && ) } }, { key: 'dispatch_type', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'ros_state') }> { row.relationShipName !== '' && ) } }, { key: 'customer_category', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'dispatch_type') }> { row.relationShipName !== '' && ) } }, { key: 'order_flag', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'customer_category') }> { row.relationShipName !== '' && ) } }, { key: 'company_name', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'order_flag') }> { row.relationShipName !== '' && ) } }, { key: 'shifts_name', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} this.highSearchFormal.company_name} />this.removeCondition(row, 'company_name') }> { row.relationShipName !== '' && ) } }, { key: 'delivery_date', type: 'range', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}this.removeCondition(row, 'shifts_name') }> { row.relationShipName !== '' && ) } }, { key: 'delivery_date', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} <DatePicker clearable editable={false} format="yyyy-MM-dd" type="daterange" placement="bottom-end" placeholder="选择配送日期" on-on-clear={() => this.clearDateTime('delivery_date')} on-on-change={this.changeComposeDeliveryDate} v-model={this.delivery_date_arr} />this.removeCondition(row, 'delivery_date') }> { row.relationShipName !== '' && ) } }, { key: 'handover_date', type: 'range', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} <DatePicker clearable editable={false} format="yyyy-MM-dd" type="date" placement="bottom-end" placeholder="选择配送日期" on-on-change={(val) => this.formatDate('delivery_date', val)} v-model={this.highSearchFormal.delivery_date} />this.removeCondition(row, 'delivery_date') }> { row.relationShipName !== '' && ) } }, { key: 'handover_date', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} <DatePicker clearable editable={false} format="yyyy-MM-dd" type="daterange" placement="bottom-end" placeholder="选择交货日期" on-on-clear={() => this.clearDateTime('handover_date')} on-on-change={this.changeComposeHandoveDate} v-model={this.handover_date_arr} />this.removeCondition(row, 'handover_date') }> { row.relationShipName !== '' && ) } }, { key: 'earliest_arrival_time', type: 'range', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} <DatePicker clearable editable={false} format="yyyy-MM-dd" type="date" placement="bottom-end" placeholder="选择交货日期" on-on-change={(val) => this.formatDate('handover_date', val)} v-model={this.highSearchFormal.handover_date} />this.removeCondition(row, 'handover_date') }> { row.relationShipName !== '' && ) } }, { key: 'earliest_arrival_time', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}false} format="yyyy-MM-dd HH:mm" placement="bottom-end" type="datetimerange" placeholder="选择要求最早到达时间" on-on-clear={() => this.clearDateTime('earliest_arrival_time')} on-on-change={this.changeArliestTime} v-model={ this.earliest_arrival_time_arr } /> this.removeCondition(row, 'earliest_arrival_time') }> { row.relationShipName !== '' && ) } }, { key: 'latest_arrival_time', type: 'range', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}false} format="yyyy-MM-dd HH:mm" placement="bottom-end" type="datetime" placeholder="选择要求最早到达时间" on-on-change={() => this.formatTime('earliest_arrival_time')} v-model={ this.highSearchFormal.earliest_arrival_time } /> this.removeCondition(row, 'earliest_arrival_time') }> { row.relationShipName !== '' && ) } }, { key: 'latest_arrival_time', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}false} format="yyyy-MM-dd HH:mm" placement="bottom-end" type="datetimerange" placeholder="选择要求最晚到达时间" on-on-clear={() => this.clearDateTime('latest_arrival_time')} on-on-change={this.changeLatestTime} v-model={ this.latest_arrival_time_arr } /> this.removeCondition(row, 'latest_arrival_time') }> { row.relationShipName !== '' && ) } }, { key: 'creator_name', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}false} format="yyyy-MM-dd HH:mm" placement="bottom-end" type="datetime" placeholder="选择要求最晚到达时间" on-on-change={() => this.formatTime('latest_arrival_time')} v-model={ this.highSearchFormal.latest_arrival_time } /> this.removeCondition(row, 'latest_arrival_time') }> { row.relationShipName !== '' && ) } }, { key: 'modifier_name', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} this.highSearchFormal.creator_name} />this.removeCondition(row, 'creator_name') }> { row.relationShipName !== '' && ) } }, { key: 'create_time', type: 'range', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }} this.highSearchFormal.modifier_name} />this.removeCondition(row, 'modifier_name') }> { row.relationShipName !== '' && ) } }, { key: 'create_time', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}false} format="yyyy-MM-dd HH:mm:ss" placement="bottom-end" type="datetimerange" placeholder="选择创建时间" on-on-clear={() => this.clearDateTime('create_time')} on-on-change={this.changeCreatTime} v-model={ this.create_time_arr } /> this.removeCondition(row, 'create_time') }> { row.relationShipName !== '' && ) } }, { key: 'update_time', type: 'range', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}false} format="yyyy-MM-dd HH:mm:ss" placement="bottom-end" type="datetime" placeholder="选择创建时间" on-on-change={() => this.formatHMSTime('create_time')} v-model={ this.highSearchFormal.create_time } /> this.removeCondition(row, 'create_time') }> { row.relationShipName !== '' && ) } }, { key: 'update_time', type: 'normal', relationShipName: '', render: (h, row) => { return ({ row.relationShipName }}false} format="yyyy-MM-dd HH:mm:ss" placement="bottom-end" type="datetimerange" placeholder="选择更新时间" on-on-clear={() => this.clearDateTime('update_time')} on-on-change={this.changeUpdateTime} v-model={ this.update_time_arr } /> this.removeCondition(row, 'update_time') }> { row.relationShipName !== '' && ) } } ]{ row.relationShipName }}false} format="yyyy-MM-dd HH:mm:ss" placement="bottom-end" type="datetime" placeholder="选择更新时间" on-on-change={() => this.formatHMSTime('update_time')} v-model={ this.highSearchFormal.update_time } /> this.removeCondition(row, 'update_time') }>
2. 定义渲染函数组件
export default { name: 'searchItems', functional: true, props: { render: Function, index: Number, type: String, relationShipName: String }, render: (h, ctx) => { const params = { index: ctx.props.index, relationShipName: ctx.props.relationShipName, type: ctx.props.type } return ctx.props.render(h, params) } }
3. 渲染