el-table组件的el-table-column电脑端使用fixed属性固定,移动端不使用固定,怎么实现?

       要在电脑端使用 fixed 固定列,而在移动端不使用,可以使用 CSS 媒体查询结合 Vue 的动态绑定来实现。以下是一个示例代码:

<template>
  <el-table>
    <el-table-column
      prop="name"
      label="Name"
      :fixed="isDesktop ? '' : 'true'"
    ></el-table-column>
    <el-table-column
      prop="age"
      label="Age"
    ></el-table-column>
    <!-- 其他列 -->
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      isDesktop: false
    };
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkDeviceType);
  },
  mounted() {
    this.checkDeviceType();
    window.addEventListener('resize', this.checkDeviceType);
  },
  methods: {
    checkDeviceType() {
      this.isDesktop = window.innerWidth >= 768; // 设置阈值,这里假设大于等于 768px 的宽度为电脑端
    }
  }
};
</script>

       在上面的示例中,我们使用 isDesktop 数据属性来判断当前设备是否为电脑端。通过监听窗口的 resize 事件,动态更新 isDesktop 的值。然后,在 el-table-column 的 fixed 属性上,我们使用了动态绑定的方式来根据 isDesktop 的值设置 fixed 属性。

       这样,在电脑端时,isDesktop 为 true,fixed 属性为 'true'(可设置true, left, right),列将被固定;而在移动端时,isDesktop 为 false,fixed 属性为空,列将不被固定。

问题:电脑端正常固定显示,手机端这两列字段不显示了

方法解决:不清楚什么原因造成的,写个笨方法

<template>
  <el-table>
    <el-table-column
      prop="name"
      label="Name"
      :fixed="isDesktop ? '' : 'true'"
    ></el-table-column>

  // 重复写一遍列字段,在手机端时显示
    <el-table-column
      v-if="!isDesktop"
      prop="name"
      label="Name"
    ></el-table-column>

    <el-table-column
      prop="age"
      label="Age"
    ></el-table-column>
    <!-- 其他列 -->
  </el-table>
</template>

猜你喜欢

转载自blog.csdn.net/m0_61663332/article/details/131974299