学无先后,达者为师

网站首页 编程语言 正文

解决多个el-checkbox内容折行对不齐问题(可以限制每行展示的个数)

作者:migexiaoliang 更新时间: 2022-08-15 编程语言

项目场景:

有时候使用element的el-checkbox-group多个el-checkbox内容折行后不对齐,如下图所示:

在这里插入图片描述


解决方案:

可以用样式来调整(可以限制每行展示的个数)

实现代码:

<el-form-item label="地域设置:" prop="regionIdList" class="region-list-style">
 <el-checkbox-group v-model="userForm.regionIdList">
    <el-checkbox
      :label="item.name"
      v-for="item in azonesListData[1]"
      :key="item.id"
      :disabled="item.status === 1"
      class="region-list-item"
      :title="item.name">
      <span>
        {{ item.name.length > 10 ? item.name.substring(0, 10) + '...' : item.name }}
      </span>
    </el-checkbox>
  </el-checkbox-group>
</el-form-item>

css代码:

<style scoped>
/* 用来对齐 */
.region-list-style /deep/ .el-checkbox + .el-checkbox {
  margin-left: 0px;
}
.region-list-style /deep/ .el-checkbox {
  margin-right: 25px;
}
/* 用来调整每一项的宽度 */
.region-list-item {
  width: 150px;
  white-space: nowrap;
  /* text-overflow: ellipsis; */
  overflow: hidden;
}
</style>

代码解释:

  1. 最多展示10个字符,多了被截断
{{ item.name.length > 10 ? item.name.substring(0, 10) + '...' : item.name }}
  1. 鼠标悬停后展示全部
:title="item.name"

最终效果图:

在这里插入图片描述

原文链接:https://blog.csdn.net/migexiaoliang/article/details/125509376

栏目分类
最近更新