iview 自定义table 表格

export const tableTh = (dataLength, monthnum, el) => {
//th配置字典
  returnData = [
    {
      keyId: "13",
      tableColumns: [
 
        {
          title: "工业增加值增速",
          align: "center",
          children: [
            {
              title: "地  区",
              key: "region",
              align: "left"
            },
            {
              title: "累计(%)",
              fatherTitle: "工业增加值增速",
              unit: "%",
              key: "added_lj",
              field: "added_lj",
              align: "right",
              render: (h, params) => {
                return h(
                  "div",
                  {
                    on: {
                      click: () => {
                        if (params.row["added_lj"]) {
                          el.getColumnMethod(params, changeYearMonth, el);
                        }
                      }
                    }
                  },
                // 所要展示内容
                  params.row["added_lj"]
                    ? parseFloat(params.row["added_lj"]).toFixed(1)
                    : "-"
                );
              }
            },
          ]
        },
      ]
    },
  ];
  return returnData;
};

使用页面引用方式

methods: {
  getColumnMethod(params, changeYearMonth, el) {}
}
//调用
import {
  tableTh,
  addedkey12Formdata,
  addedkey14Formdata,
} from "@/utils/monthdatatableth";

this.tableColumns = tableTh(reslength, self.monthnum, self)
          .filter(item => item.keyId === self.keyId)
          .map(item => item.tableColumns)[0];
//vue  
<DateList @yearmodel="yearmodel" @monthmodel="monthmodel"></DateList>
<TableDataview
            v-show="docUrltype === 'excel'"
            :table-columns="tableColumns"
            :table-data="tableData"
            :table-type="keyId"
          ></TableDataview>

组件使用

<!--   选择器 --->
<template>
  <div class="datelist">
    <div>
      <label>
        <Select
          v-model="yearmodel"
          class="selectstyle"
          @on-change="initMonth()"
        >
          <Option
            v-for="(item, key) in yearList"
            :value="item.label"
            :key="key"
            >{{ item.label }}</Option
          >
        </Select>
      </label>
    </div>
    <div
      class="monthblock"
      :class="monthmodel === item.idx ? 'activemonth' : ''"
      v-for="(item, key) in monthList"
      :key="key"
      @click="changeMonth(item.idx)"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  name: "DateList",
  data() {
    return {
      yearList: [],
      yearmodel: "",
      monthList: [],
      monthmodel: ""
    };
  },
  mounted() {
    this.initDate();
  },
  methods: {
    initDate() {
      this.yearList = [];
      let nowyear = new Date().getFullYear();
      for (let i = 0; i <= 4; i++) {
        if (nowyear - i > 2015) {
          this.yearList.push({
            label: nowyear - i
          });
        }
      }
      this.yearmodel = nowyear;
      this.initMonth("init");
    },
    initMonth(operation) {
      operation = operation || "";
      let monthli;
      this.monthList = [];
      let nowmonth = new Date().getMonth() + 1;
      let nowyear = new Date().getFullYear();
      this.$emit("yearmodel", this.yearmodel, "init");
      if (this.yearmodel === nowyear) {
        monthli = nowmonth;
        this.changeMonth(nowmonth, operation);
      } else {
        monthli = 12;
        this.changeMonth(12, operation);
      }
      for (let i = 1; i <= monthli; i++) {
        if (i !== 1) {
          this.monthList.push({
            idx: i,
            label: i + "月"
          });
        }
      }
    },
    changeMonth(idx, operation) {
      operation = operation || "";
      this.monthmodel = idx;
      this.$emit("monthmodel", this.monthmodel, operation);
    }
  }
};
</script>

<!--    table 组件    --->
<template>
  <div class="content">
    <NoneData v-if="!tableData.length"></NoneData>
    <div v-else style="height: 550px;overflow: auto">
      <vue-scroll style="width: calc(100% - 20px)">
        <Table
          :columns="tableColumns"
          :data="tableData"
          :row-class-name="rowClassName"
          class="contentbox"
          :span-method="handleSpan"
          height="550"
        ></Table>
      </vue-scroll>
    </div>
  </div>
</template>

<script>
import NoneData from "@/components/none-data/none-data";
export default {
  name: "TableDataview",
  components: {
    NoneData
  },
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    tableColumns: {
      type: Array,
      default: () => []
    },
    tableType: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      currency: ""
    };
  },
  watch: {
    tableData() {
      let _this = this;
      if (_this.tableType === "14") {
        setTimeout(() => {
          if (_this.$("table").length) {
            _this.$("table")[0].tHead.children[0].children[0].rowSpan = 2;
            _this.$("table")[0].tHead.children[1].children[0].remove();
          }
        }, 50);
      }
    }
  },
  methods: {
    rowClassName(row) {
    //根据列名选择哪一行可以点击  
      if (this.tableType === "13") {
        if (row.region === "全国") {
          return "demo-table-info-row";
        } else if (row.region === "陕西") {
          return "demo-table-one-row";
        } else if (row.salesRegion === "陕西") {
          return "demo-table-two-row";
        }
      } else {
        if (row.region === "合计") {
          return "demo-table-info-row";
        } else if (row.region === "全国合计") {
          return "demo-table-info-row";
        } else if (row.region === "全国") {
          return "demo-table-info-row";
        } else if (row.region === "陕西") {
          return "demo-table-info-row";
        } else if (row.name === "分类" && row.INDUSTRY === "总计") {
          return "demo-table-info-row";
        }
        return "";
      }
    },
    handleSpan({ rowIndex, columnIndex }) {
      //根据table表头的数目判断是否合并单元格
      this.currency = this.tableColumns;
      if (this.tableType === "14") {
        if (rowIndex === 0 && columnIndex === 0) {
          return {
            rowspan: 1,
            colspan: 1
          };
        } else if (rowIndex === 1 && columnIndex === 0) {
          return {
            rowspan: 4,
            colspan: 1
          };
        } else if (rowIndex > 1 && rowIndex <= 4 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 5 && columnIndex === 0) {
          return {
            rowspan: 8,
            colspan: 1
          };
        } else if (rowIndex > 5 && rowIndex <= 12 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 13 && columnIndex === 0) {
          return {
            rowspan: 9,
            colspan: 1
          };
        } else if (rowIndex > 13 && rowIndex <= 21 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 22 && columnIndex === 0) {
          return {
            rowspan: 14,
            colspan: 1
          };
        } else if (rowIndex > 22 && rowIndex <= 35 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 37 && columnIndex === 0) {
          return {
            rowspan: 4,
            colspan: 1
          };
        } else if (rowIndex > 37 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      } else if (this.tableType === "15") {
        if (rowIndex === 0 && columnIndex === 0) {
          return {
            rowspan: 10,
            colspan: 1
          };
        } else if (rowIndex > 0 && rowIndex <= 9 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 11 && columnIndex === 0) {
          return {
            rowspan: 8,
            colspan: 1
          };
        } else if (rowIndex > 11 && rowIndex <= 18 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 19 && columnIndex === 0) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else if (rowIndex > 19 && rowIndex <= 20 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 21 && columnIndex === 0) {
          return {
            rowspan: 4,
            colspan: 1
          };
        } else if (rowIndex > 21 && rowIndex <= 24 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 25 && columnIndex === 0) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else if (rowIndex > 25 && rowIndex <= 26 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 27 && columnIndex === 0) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else if (rowIndex > 27 && rowIndex <= 28 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 29 && columnIndex === 0) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else if (rowIndex > 29 && rowIndex <= 30 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 31 && columnIndex === 0) {
          return {
            rowspan: 3,
            colspan: 1
          };
        } else if (rowIndex > 31 && rowIndex <= 33 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 34 && columnIndex === 0) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else if (rowIndex > 34 && rowIndex <= 35 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else if (rowIndex === 36 && columnIndex === 0) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else if (rowIndex === 37 && columnIndex === 0) {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    }
  }
};
</script>

<style scoped lang="less">
.content {
  text-align: left;
  padding: 20px;
  font-size: 14px;
}
/deep/ .demo-table-info-row td {
  background-color: #477fff;
  color: #fff;
  cursor: pointer;
}
/deep/ .contentbox tr td:first-child {
  background: #1b2a4c !important;
  cursor: default;
}

/deep/ .demo-table-one-row td:nth-child(2) {
  background-color: #477fff;
  color: #fff;
  cursor: pointer;
}
/deep/ .demo-table-one-row td:nth-child(3) {
  background-color: #477fff;
  color: #fff;
  cursor: pointer;
}
/deep/ .demo-table-one-row td:nth-child(4) {
  background-color: #477fff;
  color: #fff;
  cursor: pointer;
}
/deep/ .demo-table-two-row td:nth-child(5) {
  background-color: #477fff;
  color: #fff;
  cursor: pointer;
}
/deep/ .demo-table-two-row td:nth-child(6) {
  background-color: #477fff;
  color: #fff;
  cursor: pointer;
}
/deep/ .demo-table-two-row td:nth-child(7) {
  background-color: #477fff;
  color: #fff;
  cursor: pointer;
}
</style>


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355