app开发定制解决Element-ui的el-table出现的表格错位问题

app开发定制在使用中的el-table时,app开发定制我们经常会用到fiexd属性,而使用了fixed属性之后,app开发定制就会容易在各种场景出现表格错位的问题。

查阅element-ui官网,发现官网提供了doLayout方法来解决这个问题

总结容易出现错位问题的几种场景及解决办法

1、数据更新后出现的错位问题

1.1 直接在数据赋值后执行doLayout方法

  1. this.data = data;
  2. // 在数据更新后执行
  3. this.$nextTick(() => {
  4. // myTable是表格的ref属性值
  5. if (this.$refs.myTable && this.$refs.myTable.doLayout) {
  6. this.$refs.myTable.doLayout();
  7. }
  8. })

1.2在生命周期updated里执行doLayout方法

  1. updated() {
  2. // myTable是表格的ref属性值
  3. if (this.$refs.myTable && this.$refs.myTable.doLayout) {
  4. this.$refs.myTable.doLayout();
  5. }
  6. }

2、浏览器窗口大小变化时出现的错位问题

  1. // 绑定window的onresize事件(注意,onresize事件只能有一个)
  2. window.onresize = () => {
  3. // myTable是表格的ref属性值
  4. if (this.$refs.myTable && this.$refs.myTable.doLayout) {
  5. this.$refs.myTable.doLayout();
  6. }
  7. }

3、当有多个Tab标签时,切换标签出现的错位问题

这时可以有多种解决方式

3.1 在组件守卫里执行doLayout方法

  1. beforeRouteEnter(to, from, next) {
  2. // myTable是表格的ref属性值
  3. if (this.$refs.myTable && this.$refs.myTable.doLayout) {
  4. this.$refs.myTable.doLayout();
  5. }
  6. //不能忘记这个哦
  7. next();
  8. }

3.2 如果使用了keep-alive,可以在activated里执行doLayout方法

  1. activated() {
  2. // myTable是表格的ref属性值
  3. if (this.$refs.myTable && this.$refs.myTable.doLayout) {
  4. this.$refs.myTable.doLayout();
  5. }
  6. }

3.3 也可以通过监听路由,在watch里执行doLayout方法

  1. watch: {
  2. $route() {
  3. this.$nextTick(() => {
  4. // myTable是表格的ref属性值
  5. if (this.$refs.myTable && this.$refs.myTable.doLayout) {
  6. this.$refs.myTable.doLayout();
  7. }
  8. })
  9. }
  10. }

如果当项目已经开发进入尾声,此时需要修改大量的文件,而我们可能更希望一次性解决这个问题,这个时候可以在App.vue里找到解决的思路

  1. <template>
  2. <div id="app">
  3. <router-view ref="appView"></router-view>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. },
  10. watch: {
  11. $route() {
  12. //切换标签时
  13. this.handleRefreshTable();
  14. }
  15. },
  16. created() {
  17. let _this = this;
  18. //窗口改变时
  19. window.onresize = function () {
  20. _this.handleRefreshTable();
  21. };
  22. },
  23. updated() {
  24. //数据改变时
  25. this.handleRefreshTable();
  26. },
  27. methods: {
  28. handleRefreshTable() {
  29. this.$nextTick(() => {
  30. // 获取页面中已注册过ref的所有的子组件。
  31. let refList = this.$refs.appView.$refs;
  32. if (refList) {
  33. for (let i of Object.keys(refList)) {
  34. // 根据doLayout方法判断子组件是不是el-table
  35. if (refList[i] && refList[i].doLayout) {
  36. // 执行doLayout方法
  37. refList[i].doLayout();
  38. }
  39. }
  40. }
  41. })
  42. }
  43. }
  44. };
  45. </script>

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发