基于element ui封装select 下拉表格

父组件代码示例

<el-col :span="24">
              <el-form-item label="依据协议:" prop="agreementFrom">
                <ComboGrid
                  height="200px"
                  :value="this.dataForm.agreementFrom"
                  placeholder="请选择依据协议"
                  :disabled="isDetail"
                  :url="this.url"
                  @rowClick="rowClick"
                  width="100%"
                  :isPage="true">
                  <el-table-column type="index"/>
                  <el-table-column prop="agreementCode" label="协议代码" width="100%" align="center"/>
                  <el-table-column prop="agreementName" label="协议名称" width="100%" align="center"/>
                  <el-table-column prop="notes" label="备注" width="100%" align="center"/>

                </ComboGrid>
              </el-form-item>
            </el-col>

子组件代码示例

<template>
  <div>
    <el-input style="width:100%;"
              placeholder="请选择"
              @click.native="togglePanel($event)"
              v-popover:popSel
              ref="sel"
              v-model="searchValue"
              @change="getGridList()"
              :suffix-icon="popShow?'el-icon-arrow-up':'el-icon-arrow-down'"
              :size="size"
              :placeholder="placeholder"
              :clearable="clearable"
              :disabled="disabled"
              :filterable="filterable"
    ></el-input>
    <el-popover
      ref="popSel"
      v-model="popShow"
      :width="width"
      placement="bottom-start"
      trigger="click"
      @hide="tableHide"
      @show="tableShow">
      <div>
        <el-table
          border
          ref="moviesTable"
          fit
          :data="datalist"
          highlight-current-row
          size="small"
          width="100%"
          :height="height"
          @row-click="rowClick">
          <!--  插槽  -->
          <slot></slot>

        </el-table>
        <div v-if="isPage">
          <pagination
            v-show="isPage"
            :limit.sync="page.pageSize"
            :page.sync="page.currentPage"
            :total="page.total"
            layout="total, prev, pager, next"
            @pagination="queryGridList">

          </pagination>
        </div>
      </div>
    </el-popover>
  </div>


</template>

<script>

import request from '@/utils/request'

export default {
  components: {},
  name: 'ComboGrid',
  props: {
    value: String,

    //查询参数
    queryObject: {
      type: Object,
      default() {
        return {}
      }
    },
    //是否分页
    isPage: {
      type: Boolean,
      default: true
    },
    url: {
      type: String
    },
    // 弹出选择框的高度,非输入框的
    height: {
      type: String,
      default: '40px'
    },
    // 弹出选择框的宽度
    width: {
      type: String,
      default: '100px'
    },
    placeholder: { //占位提示
      type: String,
      default: ''
    },
    disabled: {    // 是否只读
      type: Boolean,
      default: false,
      required: false
    },
    size: {
      type: String,
      default: 'medium'
    },
    filterable: { // 是否可搜索
      type: Boolean,
      default: true
    },
    remote: {   //远程搜索,与el-select一致
      type: Boolean,
      default: true
    },
    clearable: { // 是否可清空
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      // 表格数据源
      datalist: [],
      // 是否显示下拉
      popShow: false,
      // 显示值
      searchValue: '',
      // 分页对象
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      // 选中行对象
      row: {}
    }
  },
  created() {
    this.getGridList();
  },
  mounted() {
    // 初始化组件赋值
    if (this.isPage) {
      if (this.queryObject.pageSize) this.page.pageSize = this.queryObject.pageSize
      if (this.queryObject.currentPage) this.page.currentPage = this.queryObject.currentPage
      if (this.queryObject.total) this.page.total = this.queryObject.total
    }
  },
  watch: {
    searchValue: function (newVal) {
      if (!this.popShow) {
        return false
      }
      setTimeout(() => { // 延迟300ms查询
        this.getGridList();
        this.popShow = true;
      }, 300);
    },
    // 传入id
    value: function (newVal) {
      if (newVal) {
        this.getGridList(newVal);
        this.$parent.$emit("el.form.blur");
        this.$parent.$emit("el.formItem.blur")
      }
    }
  },
  methods: {
    queryGridList() {
      this.getGridList(false);
    },

    /**
     * 查询方法
     * @param isInit
     */
    getGridList(id) {
      if (this.url) {
        let queryParam = {}
        // 开启分页赋值
        if (this.isPage) {
          queryParam.pageSize = this.page.pageSize
          queryParam.currentPage = this.page.currentPage
          if (id) {
            queryParam.agreementCode = id
          }else {
            // 模糊查询协议代码以及 协议名称
            queryParam.agreementCode = this.searchValue
            queryParam.agreementName = this.searchValue
          }
        }
        // 没有开启分页采用 父组件传入值
        if (!this.isPage) {
          queryParam = this.queryObject
        }
        // 信息查询
        request({
          url: this.url,
          methods: 'get',
          data: queryParam
        }).then(request => {
          if (id) {
            this.searchValue = request.data.records[0].agreementName
          }else {
            this.datalist = request.data.records
            // 分页开启时设置信息
            if (this.isPage) {
              this.page.total = request.data.total
            }
          }
        })
      }
    },

    togglePanel(event) {
      event || (event = window.event);
      event.stopPropagation ? event.stopPropagation() : (event.cancelBubble = true)
    },
    tableShow() {
      if (this.disabled) {
        this.popShow = false
        return
      }
      if (this.searchValue === '') {
        this.getGridList();
      }
    },
    tableHide() {
      if (this.datalist.length === 0) {
        this.searchValue = "";
        this.popShow = false;
      }
    },
    rowClick(row) {
      this.popShow = false;
      this.$emit('rowClick', row);
      this.row = JSON.parse(JSON.stringify(row))
      this.searchValue = row['agreementName'];
      this.$refs.sel.blur();

    },
  }
}
</script>

<style lang="scss">
.pop-div-my {
  padding: 0;
  line-height: normal;
  box-shadow: 0 0 11px 0 rgba(174, 187, 211, 0.24);
  border: solid 1px #9eff00;
}

</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于element-ui下拉表格多选框的问题,可以通过以下步骤来解决。首先,在表格中的el-table-column中添加属性type="selection"来实现多选框的显示。其次,为了解决默认值不可删除的问题,可以在el-select中添加属性reserve-selection="true",这样在数据更新之后会保留之前选中的数据。最后,通过在toggleSelection方法中调用this.$refs.multipleTable.toggleRowSelection(row)来实现勾选和取消勾选的功能。如果需要清除所有勾选项,可以调用this.$refs.multipleTable.clearSelection()方法。这样就可以解决element-ui下拉表格多选框的问题了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Element-UI表格多选框与tags联动](https://blog.csdn.net/weixin_42823060/article/details/120413321)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [解决element-ui里的下拉多选框 el-select 时,默认值不可删除问题](https://download.csdn.net/download/weixin_38596093/12924091)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国家级著名CV工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值