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 ( <form-item label="运输任务单号"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Input placeholder="运输任务单号" clearable v-model={this.highSearchFormal.order_id} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'order_id') }></Icon> </form-item> ) } }, { key: 'original_order_no', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="运输订单号"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Input placeholder="运输订单号" clearable v-model={this.highSearchFormal.original_order_no} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'original_order_no') }></Icon> </form-item> ) } }, { key: 'execution_no', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="运输执行单号"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Input placeholder="运输执行单号" clearable v-model={this.highSearchFormal.execution_no} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'execution_no') }></Icon> </form-item> ) } }, { key: 'dispatch_id', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="调度单号"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Input placeholder="调度单号" clearable v-model={this.highSearchFormal.dispatch_id} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'dispatch_id') }></Icon> </form-item> ) } }, { key: 'consignee_name', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="收货方"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } {/* <Select clearable filterable placement="bottom-end" v-model={this.highSearchFormal.consignee_name} on-on-open-change={this.openComsignee} remoteSearch={this.remoteComposeConsignee} scrollBottom={this.scrollBottomComposeConsignee}> {this.highConsigneeDatas.map((o, i) => <Option key={i} value={o.consigneeId} label={o.consigneeName} />)} </Select> */} <Input placeholder="收货方名称" clearable v-model={this.highSearchFormal.consignee_name} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'consignee_name') }></Icon> </form-item> ) } }, { key: 'state', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="订单状态"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable multiple on-on-change={this.changeComposeState} v-model={this.state_arr}> {this.searchDatas.stateList.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'state') }></Icon> </form-item> ) } }, { key: 'priority', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="优先级"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable v-model={this.highSearchFormal.priority}> {this.searchDatas.priorityList.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'priority') }></Icon> </form-item> ) } }, { key: 'stocking_state', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="备货状态"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable v-model={this.highSearchFormal.stocking_state}> {this.searchDatas.stockingStateList.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'stocking_state') }></Icon> </form-item> ) } }, { key: 'operation_state', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="作业状态"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable multiple on-on-change={this.changeComposeOperationState} v-model={this.operation_state_arr}> {this.operationStateList.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'operation_state') }></Icon> </form-item> ) } }, { key: 'order_source', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="订单来源"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable v-model={this.highSearchFormal.order_source}> {this.orderSourceList.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'order_source') }></Icon> </form-item> ) } }, { key: 'ros_state', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="智能调度状态"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable v-model={this.highSearchFormal.ros_state}> {this.rosStateEnum.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'ros_state') }></Icon> </form-item> ) } }, { key: 'dispatch_type', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="调度方式"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable v-model={this.highSearchFormal.dispatch_type}> {this.dispatchTypeEnum.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'dispatch_type') }></Icon> </form-item> ) } }, { key: 'customer_category', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="客户类别"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable v-model={this.highSearchFormal.customer_category}> {this.customerCategoryDict.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'customer_category') }></Icon> </form-item> ) } }, { key: 'order_flag', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="订单标识"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable v-model={this.highSearchFormal.order_flag}> {this.orderFlagList.map((o, i) => <Option key={i} value={o.dictKey} label={o.dictValue} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'order_flag') }></Icon> </form-item> ) } }, { key: 'company_name', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="所属公司"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Input placeholder="所属公司名称" clearable v-model={this.highSearchFormal.company_name} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'company_name') }></Icon> </form-item> ) } }, { key: 'shifts_name', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="班次"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Select clearable filterable v-model={this.highSearchFormal.shifts_name} remoteSearch={this.remoteComposeShifts} scrollBottom={this.scrollBottomComposeShifts}> {this.highShiftsList.map((o, i) => <Option key={i} value={o.shiftsName} label={o.shiftsName} />)} </Select> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'shifts_name') }></Icon> </form-item> ) } }, { key: 'delivery_date', type: 'range', relationShipName: '', render: (h, row) => { return ( <form-item label="配送日期"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <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} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'delivery_date') }></Icon> </form-item> ) } }, { key: 'delivery_date', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="配送日期"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <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} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'delivery_date') }></Icon> </form-item> ) } }, { key: 'handover_date', type: 'range', relationShipName: '', render: (h, row) => { return ( <form-item label="交货日期"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <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} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'handover_date') }></Icon> </form-item> ) } }, { key: 'handover_date', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="交货日期"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <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} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'handover_date') }></Icon> </form-item> ) } }, { key: 'earliest_arrival_time', type: 'range', relationShipName: '', render: (h, row) => { return ( <form-item label="要求最早到达时间"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <DatePicker clearable editable={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 } /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'earliest_arrival_time') }></Icon> </form-item> ) } }, { key: 'earliest_arrival_time', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="要求最早到达时间"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <DatePicker clearable editable={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 } /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'earliest_arrival_time') }></Icon> </form-item> ) } }, { key: 'latest_arrival_time', type: 'range', relationShipName: '', render: (h, row) => { return ( <form-item label="要求最晚到达时间"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <DatePicker clearable editable={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 } /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'latest_arrival_time') }></Icon> </form-item> ) } }, { key: 'latest_arrival_time', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="要求最晚到达时间"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <DatePicker clearable editable={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 } /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'latest_arrival_time') }></Icon> </form-item> ) } }, { key: 'creator_name', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="创建人"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Input placeholder="创建人名称" clearable v-model={this.highSearchFormal.creator_name} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'creator_name') }></Icon> </form-item> ) } }, { key: 'modifier_name', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="更新人"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <Input placeholder="更新人名称" clearable v-model={this.highSearchFormal.modifier_name} /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'modifier_name') }></Icon> </form-item> ) } }, { key: 'create_time', type: 'range', relationShipName: '', render: (h, row) => { return ( <form-item label="创建时间"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <DatePicker clearable editable={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 } /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'create_time') }></Icon> </form-item> ) } }, { key: 'create_time', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="创建时间"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <DatePicker clearable editable={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 } /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'create_time') }></Icon> </form-item> ) } }, { key: 'update_time', type: 'range', relationShipName: '', render: (h, row) => { return ( <form-item label="更新时间"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <DatePicker clearable editable={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 } /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'update_time') }></Icon> </form-item> ) } }, { key: 'update_time', type: 'normal', relationShipName: '', render: (h, row) => { return ( <form-item label="更新时间"> { row.relationShipName !== '' && <div class="sign">{ row.relationShipName }</div> } <DatePicker clearable editable={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 } /> <Icon type="icon-close" color="blue" class="closeBtn" onClick={ () => this.removeCondition(row, 'update_time') }></Icon> </form-item> ) } } ]
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. 渲染
<div v-for="(item, index) in renderSearchTemplate" :key="index">
<searchItems :render="item.render" :index="index" :relationShipName="item.relationShipName">
</searchItems>
</div>