前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >el-select选中变色及百分比怎么做

el-select选中变色及百分比怎么做

作者头像
不爱吃糖的程序媛
发布2024-01-18 21:21:43
2380
发布2024-01-18 21:21:43
举报
文章被收录于专栏:夏天的前端笔记
效果图:

选中后超过80%的显示红色,没有则显示黄色,没有百分比的不变颜色。

解决思路:

1.A思路:用的是获取dom节点,每切换一次就获取dom节点,然后给它变色;但是这样子的缺点就是有时候切换就会不显示颜色,这个原因是它捕获不到最新的dom节点。 2.B思路:用的vue里面的:class,并且就是设置一个变量,如果这个变量为true就是红色,否则就是false黄色,但是这样子不能正确显示百分比出来。 3.C思路:就是在外面蒙一层div,设置宽度和颜色为变量,定义一个函数,在改变下拉框的时候调用函数并且传入参数。

源代码:
代码语言:javascript
复制
 <div class="stock-location">
          <el-form-item :label="$t('lang.intoStockLocation')" prop="shelvesSn">
            <div class="changeColor" style="position: relative;">
            //使用内联样式,在它的外面给它加个div盒子,设置两个变量:宽度和颜色
              <div
                :style="{
                  position: 'absolute',
                  backgroundColor: backgroundColorShelves,
                  width: backgroundWidthShelves + 'px',
                  height: '40px',
                  borderRadius: '5px'
                }"
              ></div>
              <el-select
                v-model="ruleForm.shelvesSn"
                class="shelfLocation"
                :placeholder="$t('lang.pleaseSelect')"
                @change="handleShelvesChange"
                popper-class="changeColor-select"
              >
                <el-option
                  :label="
                    item.shelvesUsage > 0
                      ? $t('lang.shelfUsage', {
                          shelf: item.shelvesSn,
                          usage: item.shelvesUsage
                        })
                      : item.shelvesSn
                  "
                  :value="item.shelvesSn"
                  v-for="(item, index) in shelvesList"
                  :key="index"
                ></el-option>
              </el-select>
            </div>
            <div class="unit">{{ $t("lang.shielf") }}</div>
          </el-form-item>
       
          <el-form-item
            prop="shelvesLevel"
            :label="$t('lang.nth')"
            label-width="22px"
          >
            <div class="changeColor" style="position: relative;">
              <div
                :style="{
                  position: 'absolute',
                  backgroundColor: backgroundColor,
                  width: backgroundWidth + 'px',
                  height: '40px',
                  borderRadius: '5px'
                }"
              ></div>
              <Popover placement="top" :imageUrl="imgUrl" v-if="imgUrl">
                <el-select
                  v-model="ruleForm.shelvesLevel"
                  class="shelfLevel"
                  :placeholder="$t('lang.pleaseSelect')"
                  @change="handleLevelChange"
                  popper-class="changeColor-select"
                >
                  <el-option
                    :label="
                      item.usage > 0
                        ? $t('lang.levelUsage', {
                            level: item.level,
                            usage: item.usage
                          })
                        : item.level
                    "
                    :value="item.level"
                    v-for="(item, index) in levelList"
                    :key="index"
                  ></el-option>
                </el-select>
              </Popover>
              <el-select
                v-else
                v-model="ruleForm.shelvesLevel"
                class="shelfLevel"
                :placeholder="$t('lang.pleaseSelect')"
                @change="handleLevelChange"
              >
                <el-option
                  :label="
                    item.usage > 0
                      ? $t('lang.levelUsage', {
                          level: item.level,
                          usage: item.usage
                        })
                      : item.level
                  "
                  :value="item.level"
                  v-for="(item, index) in levelList"
                  :key="index"
                ></el-option>
              </el-select>
            </div>
            <div class="unit">{{ $t("lang.level") }}</div>
          </el-form-item>
        </div>
代码语言:javascript
复制
<script>
export default {
data(){
  return {
    //定义变量给它一个默认值
      backgroundColorShelves: "white",//货架编号
      backgroundWidthShelves: 0,
      backgroundColor: "white",//货架层级
      backgroundWidth: 0,
    
  }
},

//定义变颜色以及宽度变化的方法
//改变货架编号
 updateStyleShelves(percentage1, percentage2) {
    //为什么设置202是因为输入框的宽度是202
      this.backgroundWidthShelves = 202 * percentage1;
      this.backgroundColorShelves = percentage1 > 0.8 ? "#ffe2e2" : "#fff0e0";
      this.backgroundWidth = 202 * percentage2;
      this.backgroundColor = percentage2 > 0.8 ? "#ffe2e2" : "#fff0e0";
    },
    
//改变货架层级
    updateStyle(percentage) {
      this.backgroundWidth = 202 * percentage;
      this.backgroundColor = percentage > 0.8 ? "#ffe2e2" : "#fff0e0";
    },
    
 //改变货架编号的方法       
  handleShelvesChange(item) {
      //定义一个选中的货架
      const selectedShelf = this.shelvesList.filter(
        ele => ele.shelvesSn === item //item为货架编号
      );
      if (selectedShelf && selectedShelf.length > 0) {
        //监控层
        this.levelList = selectedShelf[0].floors || [];
        this.shelvesUsePercentage = Number(selectedShelf[0].shelvesUsage) / 100;
        this.ruleForm.shelvesLevel = this.levelList.length > 0 ? 1 : "";
        this.defaultlevelUsePercentage = Number(this.levelList[0].usage) / 100; //选择货架编号的默认第一个的货架的百分比
        // console.log("123",this.shelvesUsePercentage, this.defaultlevelUsePercentage)
        //调用变色的方法,将货架编号百分比以及货架层数百分比传进去
        this.updateStyleShelves(
          this.shelvesUsePercentage,
          this.defaultlevelUsePercentage
        );
         }
      this.checkSelectedShelves();
    },
    
 //改变货架层级的方法   
  handleLevelChange(val) {
      this.defineUsagePercentAndImageUrl(val);
      this.updateStyle(this.levelUsePercentage);
  }
 </script>

第一种思路重现: (从代码上来看逻辑没有问题,但是获取不到最新的dom节点,这就导致了bug)

代码语言:javascript
复制
handleShelvesChange(item) {
      //定义一个选中的货架
      const selectedShelf = this.shelvesList.filter(
        ele => ele.shelvesSn === item
      );
      if (selectedShelf && selectedShelf.length > 0) {
       //监控层
        this.levelList = selectedShelf[0].floors || [];
        this.shelvesUsePercentage = Number(selectedShelf[0].shelvesUsage) / 100;
        this.ruleForm.shelvesLevel = this.levelList.length > 0 ? 1 : "";
        if (this.ruleForm.shelvesLevel) {
          this.defineUsagePercentAndImageUrl(this.ruleForm.shelvesLevel);
          // 插入背景色(货架层)
          if (this.isShowUsage) {
          //关键代码,他传的是选择器和百分比
            this.calculateSelectBackground(
              ".shelfLevel>.el-input",
              this.levelUsePercentage
            );
            // 插入背景色(货架)
            this.calculateSelectBackground(
              ".shelfLocation>.el-input",
              this.shelvesUsePercentage
            );
          }
        }
      }
      this.checkSelectedShelves();
    },
    changeCheckbox() {
      if (this.checked == false) {
        this.ruleForm.operateType = 0;
      } else {
        this.ruleForm.operateType = 2;
      }
    },
    // 用js向下拉选择框插入背景色百分比进度条
    calculateSelectBackground(selector, percentage) {
      console.log(selector, percentage);
      let dom = document.querySelector(selector);
      if (dom) {
        let lastDom = document.querySelector(`${selector}>div`);
        if (lastDom) {
          dom.removeChild(lastDom);
        }
        console.log(selector, percentage, 2);
        let domHeight = dom.offsetHeight - 2;
        let domWidth = dom.offsetWidth * percentage;
        let newDom = document.createElement("div");
        console.log(domHeight, domWidth, percentage, 3);
        //这样的写法
        newDom.style = `
          width: ${domWidth}px;
          height: ${domHeight}px;
          background: ${percentage > 0.8 ? "#ffe2e2" : "#fff0e0"};
          position: absolute;
          border-radius:4px;
          opacity:0.5;
          bottom:0px;
        `;
        dom.insertBefore(newDom, dom.children[0]);
      }
    },
    destroyDom(selector) {
      let dom = document.querySelector(selector);
      let lastDom = document.querySelector(`${selector}>div`);
      if (lastDom) {
        dom.removeChild(lastDom);
      }
    },
    handleLevelChange(val) {
      this.ruleForm.shelvesLevel = val;
      this.defineUsagePercentAndImageUrl(val);
      this.checkSelectedShelves();
      // console.log(this.isShowUsage);
      if (this.isShowUsage) {
        this.calculateSelectBackground(
          ".shelfLevel>.el-input",
          this.levelUsePercentage
        );
      } else {
        this.calculateSelectBackground(".shelfLevel>.el-input", 0);
      }
    },
    defineUsagePercentAndImageUrl(val) {
      const selectedItem = this.levelList.filter(item => item.level === val)[0];
      this.levelUsePercentage = Number(selectedItem.usage) / 100;
      this.imgUrl = selectedItem.url;
      this.isShowUsage = selectedItem.usage > 0; //图片对否显示:false不显示,true显示
    },
   checkSelectedShelves() {
      if (this.selectedShelvesList.length > 0) {
        const selectedShelf = this.selectedShelvesList.filter(
          item =>
            item.shelvesSn === this.ruleForm.shelvesSn &&
            item.shelvesLevel === this.ruleForm.shelvesLevel
        );
        if (selectedShelf.length > 0) {
          this.selectedShelvesList.forEach((elem, idx) => {
            if (
              selectedShelf[0].shelvesLevel === elem.shelvesLevel &&
              selectedShelf[0].shelvesSn === elem.shelvesSn
            ) {
              this.mouseIndex = idx;
              this.isSelectShow = true;
            }
          });
        } else {
          this.mouseIndex = -1;
          this.isSelectShow = false;
        }
      }
    },
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 效果图:
  • 解决思路:
  • 源代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档