vue3 antdv Select 实现输入关键词,通过服务器去查询数据,并显示到表格中的实现思路。

实现思路:

1)输入关键词,通过Select的查询事件(onSearch)来到服务器查询数据。

2)根据查询到的数据显示到表格中,然后通过表格的(cellClickEvent)事件来选择相关的用户。

3)如果没有查询到,那么点击底部的增加(onAddInfo),弹出框来增加。

4)弹框中增加到服务器后,通过服务器返回的id,及输入的用户名,电话,直接赋值到界面。

1、先上一个效果图:

2、实现

Ant Design Vue --- An enterprise-class UI components based on Ant Design and Vue.js

根据官方文档,我们要实现这个slot,上代码:

复制代码
<Select
                  placeholder="请输入姓名"
                  v-model:value="formStateUser.realname"
                  style="width: 100%"
                  show-search
                  :open="mnuOpen"
                  :options="items.map((item) => ({ value: item }))"
                  @search="onSearch"
                  allow-clear
                  ref="mnuSelect"
                  :dropdownMatchSelectWidth="false"
                >
                  <template #dropdownRender="{ menuNode: menu }">
                    <vxe-grid
                      ref="tableRef"
                      v-bind="gridOptions"
                      style="width: 100%; margin-top: -3px"
                      @cell-click="cellClickEvent"
                      :row-config="{ isHover: true }"
                    />
                    <Divider style="margin: 4px 0" />
                    <div
                      style="padding: 4px 8px; cursor: pointer"
                      @mousedown="(e) => e.preventDefault()"
                      @click="onAddInfo"
                    >
                      <plus-outlined />
                      <span class="modal-btn">增加</span>

                    </div>
                  </template>
                  <template #notFoundContent>
                    <span>暂无任用户信息。</span>
                  </template>
                </Select>

这里就是表格显示的位置 ,用的是vxe-grid来实现的:

上面就是Select数据选择的实现,提供一个简单思路,可以根据具体的环境来实现。

下面帖几个代码,来检测是否是点了表格+分页+增加按钮的,点击外边的部分,自动关闭对话框:

复制代码
onMounted(() => {
    // 监听 document 上的点击事件
    document.addEventListener('click', (event) => {
      if (!isVxeTableClick(event)) {
        mnuOpen.value = false;//close
      }
    });
  });

const isVxeTableClick = (event) => {
    let target = event.target;
    while (target && target !== document) {
      if (
        target.className &&
        typeof target.className === 'string' &&
        (target.className.includes('vxe-table') ||
          target.className.includes('vxe-pager') ||
          target.className.includes('modal-btn'))
      ) {
        // 点击事件来自 vxe-table
        return true;
      }
      target = target.parentNode;
    }
    return false;
  };

主要是上面的代码,来检测是否要关闭dropdownRender。

相关推荐
harrain3 天前
vue2开发环境搭建指南
vue
by__csdn4 天前
Electron+Vite:实现electron + vue3 + ts + pinia + vite高效跨平台开发指南
前端·javascript·vue.js·typescript·electron·node.js·vue
人工智能训练4 天前
前端框架选型破局指南:Vue、React、Next.js 从差异到落地全解析
运维·javascript·人工智能·前端框架·vue·react·next.js
李纲明5 天前
WordPress外贸成品网站的免费获取渠道
vue·php
加洛斯6 天前
前端小知识002:ref 与 reactive 详解
前端·vue
计算机毕设vx_bysj68696 天前
计算机毕业设计必看必学~Springboot教学进度管理系统,原创定制程序、单片机、java、PHP、Python、小程序、文案全套、毕设成品等!
java·spring boot·vue·课程设计·管理系统
小贺要学前端7 天前
【无标题】
前端·javascript·vue·技术趋势
IT教程资源C7 天前
(N_141)基于springboot,vue网上拍卖平台
mysql·vue·前后端分离·拍卖系统·springboot拍卖
IT教程资源C8 天前
(N_144)基于微信小程序在线订餐系统
mysql·vue·uniapp·前后端分离·订餐小程序·springboot订餐
合作小小程序员小小店8 天前
web网页开发,在线短视频管理系统,基于Idea,html,css,jQuery,java,springboot,mysql。
java·前端·spring boot·mysql·vue·intellij-idea