vue中有多个下拉框,第二个下拉框要根据第一个得下拉框数据进行选择

思路:首先获取第一个下拉框得数据,给第一个下拉框注册一个change事件,如果选中了第一个下拉框,就会通过第一个下拉框得值查询第二个下拉框所要显示得值;

代码如下:

      <el-form-item label="一级分类名称" prop="oneId" >
        <el-select v-model="queryParams.oneId"  placeholder="请选择一级分类名称" @change="checkCategoryPromotion(queryParams.oneId)">
          <el-option
                  v-for="item in oneCategoryOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
          ></el-option>
        </el-select>

      </el-form-item>

      <el-form-item label="二级分类名称" prop="twoId" >
        <el-select v-model="queryParams.twoId"  placeholder="先选择一级,后选二级分类" >
          <el-option
                  v-for="item in twoCategoryOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
methods: {
   
    getOneCategorys() {
      getOneCategorys().then(response => {
        this.oneCategoryOptions = response.data;


      });
    },
    checkCategoryPromotion(oneId){
      getTwoCategorys(oneId).then(response => {
        this.queryParams.twoId=null;
        this.twoCategoryOptions = response.data;
        // alert("oneId"+JSON.stringify(this.twoCategoryOptions));
      });

}
import { getOneCategorys } from "@/api/backstage/oneCategory";
import { getTwoCategorys } from "@/api/backstage/twoCategory";
// 查询一级分类 js文件
export function getOneCategorys() {
  return request({
    url: '/backstage/oneCategory/all',
    method: 'get'
  })
}
// 查询二级分类 js文件
export function getTwoCategorys(oneId) {
  return request({
    url: '/backstage/twoCategory/all/'+oneId,
    method: 'get'
  })
}

controller中的代码段

    /**
     * 查询二级分类列表
     */
    @PreAuthorize("@ss.hasPermi('backstage:twoCategory:all')")
    @GetMapping("/all/{oneId}")
    public AjaxResult all(@PathVariable Long oneId)
    {
     
        List<TTwoCategory> list = tTwoCategoryService.selectTTwoCategoryNameList(oneId);

        return AjaxResult.success(list);
    }
    /**
     * 查询一级分类列表
     */
    @PreAuthorize("@ss.hasPermi('backstage:oneCategory:all')")
    @GetMapping("/all")
    public AjaxResult all()
    {
        List<TOneCategory> list = tOneCategoryService.selectTOneCategoryNameList();

        return AjaxResult.success(list);
    }

实现查询数据。

做个笔记。。。

  • 13
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
对于Vue Ant Design中的两个下拉框联动,你可以通过监听第一个下拉框的选中事件来实现第二个下拉框的展开。具体步骤如下: 1. 在Vue组件中,先定义两个下拉框数据和选中: ```vue <template> <div> <a-select v-model="firstSelectValue" @change="handleFirstSelectChange"> <a-select-option value="option1">Option 1</a-select-option> <a-select-option value="option2">Option 2</a-select-option> </a-select> <a-select v-model="secondSelectValue" v-if="isSecondSelectVisible"> <!-- 第二个下拉框的选项 --> </a-select> </div> </template> <script> export default { data() { return { firstSelectValue: "", secondSelectValue: "", isSecondSelectVisible: false }; }, methods: { handleFirstSelectChange(value) { // 根据第一个下拉框的选中决定是否展开第二个下拉框 if (value === "option1") { this.isSecondSelectVisible = true; } else { this.isSecondSelectVisible = false; } // 清空第二个下拉框的选中 this.secondSelectValue = ""; } } }; </script> ``` 在上述代码中,第一个下拉框通过`v-model`绑定了`firstSelectValue`,当其选中发生变化时,会触发`handleFirstSelectChange`方法。在该方法中,根据第一个下拉框的选中,设置`isSecondSelectVisible`变量来决定第二个下拉框是否展开。同时,清空第二个下拉框的选中,以避免出现不一致的情况。 2. 根据你的需求,自行定义第二个下拉框的选项,并通过`v-if`指令来控制其展示与隐藏。 这样,当你选择第一个下拉框的选项时,第二个下拉框会根据选中的不同来展示或隐藏。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值