index.vue 11.4 KB
<!-- 搜索 -->
<template>
  <div class="d2-search">
    <el-form v-model="searchForm"
             class="d2-search-block"
             size="small"
             :inline="true"
             :label-width="labelWidth"
             :style="{ '--labelWidth': labelWidth }">
      <template v-for="(item, index) in formData">
        <el-col v-if="!item.isHide"
                :key="index"
                class="item-height"
                :span="8">
          <el-form-item :class="{ 'no-label': !item.label }"
                        :label="item.label ? `${item.label}:` : ''"
                        :style="item.itemStyle || ''">
            <el-input v-if="item.type === 'input'"
                      v-model.trim="searchForm[item.name]"
                      :clearable="item.isClear || false"
                      :disabled="item.isDisabled"
                      :maxlength="item.maxlength || 30"
                      :placeholder="item.placeholder || `请输入${item.label}`"
                      :style="{ width: item.width || '100%' }"
                      :type="item.inputType || 'text'"
                      @input="(e) => maxLengthInput(e, item)"
                      @keydown.native="(e) => handleInputDown(e, item)" />

            <!-- 选择-输入框 -->
            <el-input v-else-if="item.type === 'selectInput'"
                      v-model.trim="searchSelectObj[item.selectName].value"
                      class="input-with-select"
                      :clearable="item.isClear || false"
                      :maxlength="item.maxlength || ''"
                      :placeholder="
                        `请输入${
                          item.selectOption.find(
                            (k) => k.name === searchSelectObj[item.selectName].attr,
                          ).label}`
                      ">
              <template #prepend>
                <el-select v-model="searchSelectObj[item.selectName].attr"
                           placeholder="请选择"
                           :popper-append-to-body="false"

                           :style="{ width: item.selectWidth || '130px' }"
                           @change="item.handleSelectChange">
                  <el-option v-for="(subItem, subIndex) in item.selectOption"
                             :key="subIndex"
                             :label="subItem.label"
                             :value="subItem.name" />
                </el-select>
              </template>
            </el-input>
            <!-- 选择-时间选择框 -->
            <template v-else-if="item.type === 'selectDatePicker'">
              <el-select v-model="searchSelectObj[item.selectName].attr"
                         placeholder="请选择"
                         :style="{ width: item.selectWidth || '130px' }"
                         @change="item.handleSelectChange">
                <el-option v-for="(subItem, subIndex) in item.selectOption"
                           :key="subIndex"
                           :label="subItem.label"
                           :value="subItem.name" />
              </el-select>
              <el-date-picker v-model="searchSelectObj[item.selectName].value"
                              end-placeholder="结束日期"
                              range-separator="至"
                              start-placeholder="开始日期"
                              :clearable="item.isClear || false"
                              :picker-options="
                                item.pickerOptions
                                  ? pickerOption[item.pickerOptions]
                                  : undefined
                              "
                              :style="{
                                width:
                                  `calc(100% - ${
                                    typeof item.selectWidth === 'number'
                                      ? `${item.selectWidth}px`
                                      : item.selectWidth || '130px'
                                  })`,
                              }"
                              :type="
                                item.diffType
                                  ? searchSelectObj[item.selectName].attr
                                  : item.dateType || 'daterange'
                              " />
            </template>
            <!-- 双选择框 -->

            <template v-else-if="item.type === 'selectDouble'">
              <el-select v-model="searchSelectObj[item.selectName].attr"
                         placeholder="请选择"
                         style="width: 130px"
                         @change="item.handleSelectChange">
                <el-option v-for="(subItem, subIndex) in item.selectOption"
                           :key="subIndex"
                           :label="subItem.label"
                           :value="subItem.name" />
              </el-select>
              <el-select v-model="searchSelectObj[item.selectName].value"
                         style="width: calc(100% - 130px)"
                         :placeholder="
                           `请选择${
                             item.selectOption.find(
                               (k) => k.name === searchSelectObj[item.selectName].attr,
                             ).label}`
                         ">
                <el-option label="全部" value="" />
                <el-option v-for="subItem in item.selectOption.find(
                             (k) => k.name === searchSelectObj[item.selectName].attr,
                           ).optionList"
                           :key="subItem[item.optionId]"
                           :label="subItem[item.optionLabel]"
                           :value="subItem[item.optionId]" />
              </el-select>
            </template>
            <!-- 选择框 -->
            <el-select v-else-if="item.type === 'select'"
                       v-model="searchForm[item.name]"
                       :clearable="item.isClear || false"
                       :collapse-tags="item.collapseTags"
                       :disabled="item.isDisabled"
                       :filterable="!item.noFilter"
                       :multiple="item.multiple"
                       :multiple-limit="item.multipleLimit || 0"
                       :no-data-text="item.noDataText || '无数据'"
                       :placeholder="item.placeholder || `请选择${item.label}`"
                       :popper-append-to-body="selectPopperAppendToBody"
                       :style="{ width: item.width || '100%' }"
                       @change="selectAddAttr($event, item)">
              <el-option v-for="(subItem, subIndex) in item.optionList"
                         :key="subIndex"
                         :label="subItem[item.optionLabel]"
                         :value="subItem[item.optionId]" />
            </el-select>
            <el-radio-group v-else-if="item.type === 'radio'"
                            v-model="searchForm[item.name]"
                            :style="{ width: item.width || '100%' }"
                            @change="item.changeAttr && item.changeAttr($event, item)">
              <el-radio v-for="(subItem, subIndex) in item.optionList"
                        :key="subIndex"
                        :label="subItem[item.optionId]">
                {{ subItem[item.optionLabel] }}
              </el-radio>
            </el-radio-group>
            <el-date-picker v-else-if="item.type === 'datePicker'"
                            v-model="searchForm[item.name]"
                            end-placeholder="结束日期"
                            range-separator="至"
                            start-placeholder="开始日期"
                            :clearable="item.isClear || false"
                            :default-time="
                              item.defaultTime !== undefined ? item.defaultTime : undefined
                            "
                            :picker-options="
                              item.pickerOptions
                                ? pickerOption[item.pickerOptions]
                                : undefined
                            "
                            :placeholder="item.placeholder"
                            :style="{ width: item.width || '100%' }"
                            :type="item.dateType || 'daterange'" />
            <slot v-if="item.addHtml"
                  :name="item.addHtml"
                  :scope="item" />
          </el-form-item>
        </el-col>
      </template>
      <slot />
    </el-form>
  </div>
</template>

<script>
import { isFunction } from "lodash-es";
import {
	rangePickerOptions,
	rangePickerOptions_30day2,
	rangePickerOptions_lastOneYear,
	rangePickerOptions_oneYearNoFuture,
	rangePickertwoMonthOptions,
	rangePickertwoMonthOptionsWithCurrentLimit,
	stockQueryDatePickerOptions,
	todayEndPickerOptions,
	yesterdayPicker,
} from "@/common/format";

export default {
	name: "D2Search",

	props: {
		labelWidth: {
			type: String,
			default: "85px",
		},
		formData: {
			type: Array,
			default: () => [],
		},
		searchForm: {
			type: Object,
			default: () => {},
		},
		hasTitle: {
			type: Boolean,
			default: true,
		},
		// 搜索+输入框/搜索组合组件,搜索栏绑值
		searchSelectObj: {
			type: Object,
			default: () => {},
		},
		// colWidth: {
		//   type: Number,
		//   default: 12
		// }
		selectPopperAppendToBody: {
			type: Boolean,
			default: true,
		},
	},
	data() {
		return {
			pickerOption: {
				rangePickerOptions,
				yesterdayPicker,
				rangePickerOptions_30day2,
				todayEndPickerOptions,
				rangePickertwoMonthOptions,
				rangePickertwoMonthOptionsWithCurrentLimit,
				rangePickerOptions_lastOneYear,
				rangePickerOptions_oneYearNoFuture,
				stockQueryDatePickerOptions,
			},
		};
	},

	mounted() {},

	methods: {
		// 选择框-添加其他属性
		selectAddAttr(value, targetItem) {
			console.log("selectAddAttr value", value, "targetItem", targetItem);
			targetItem.changeAttr && targetItem.changeAttr(value, targetItem);
			// 根据所选值 获取数据
			if (targetItem.name === "sceneType") {
				this.$emit("handleChangeSelect", value);
			}
			// 选择分类与所属系列联动
			if (targetItem.name === "goodsType") {
				this.$emit("handleChangeSelect", value);
			}
		},
		handleInputDown(e, item) {
			if (e.keyCode === 13 && item.hasOwnProperty("keyFn")) {
				item.keyFn();
			}
		},
		maxLengthInput(value, item) {
			if (item.inputType && value.length > item.maxlength) {
				this.searchForm[item.name] = value.slice(0, item.maxlength);
			}
			if (item.input && isFunction(item.input)) {
				item.input({ value, schema: item, formModel: this.searchForm });
			}
		},
	},
};
</script>

<style>
.d2-search {
	min-width: 922px;
}

.d2-search .d2-search-block {
	padding: 16px 40px 0;
}

.d2-search .el-form-item {
	width: 94%;
}

.d2-search .no-label.el-form-item--small .el-form-item__content,
.el-form-item--small .el-form-item__label {
	width: 100%;
}

.d2-search .el-form-item--small .el-form-item__content {
	width: calc(100% - var(--labelWidth));
}

.d2-search .el-col {
	margin-bottom: 10px;
}

.d2-search .el-select {
	width: 100%;
}

.d2-search .item-height {
	height: 37px;
}

.d2-search input::-webkit-outer-spin-button,
.d2-search input::-webkit-inner-spin-button {
	-webkit-appearance: none !important;
}

.d2-search input[type="number"] {
	-moz-appearance: textfield !important;
}

.d2-search .input-with-select .el-input-group__prepend {
	background-color: #fff;
}

.d2-search .el-input__inner {
	line-height: 1px !important;
}
</style>