123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <template>
- <table class="com-table">
- <thead>
- <tr>
- <th @click="onSort(0)" style="width:50px">排序</th>
- <th @click="onSort(1)" style="width:50px"></th>
- <th v-for="(col, index) of data.column" :key="index" :style="{ width: col.width }" :class="{ light: col.is_light }" @click="onSort(col)">{{ col.name }}</th>
- </tr>
- </thead>
- <el-scrollbar>
- <tbody :style="{ height: height }">
- <tr v-for="(row, index) of tableData" :key="index">
- <td v-if="allowIndex == true" style="width:50px" :class="{ light: hoverRow == row || hoverCol == 0, num: true }" @mouseenter="hover(row, col)" @mouseleave="leave()">
- {{ startRow + index + 1 }}
- </td>
- <td :class="{ light: hoverRow == row || hoverCol == 1 }" style="width:50px" @mouseenter="hover(row, col)" @mouseleave="leave()">
- <input class="check" type="CheckBox" :key="index" :id="'check-' + index" :checked="row.checked ? row.checked : false" @change="onCheck(row)" />
- </td>
- <td
- v-for="(col, i) of data.column"
- :key="i"
- :style="{ width: col.width }"
- :class="{ light: hoverRow == row || hoverCol == col + 2, num: col.is_num, 'always-light': col.is_light || row.is_light }"
- @mouseenter="hover(row, col)"
- @mouseleave="leave()"
- >
- <component :is="col.type ? col.type : 'div'" v-bind="col.props" v-html="template(col, row[col.field])" @click="onClick(col, row)"> </component>
- </td>
- </tr>
- </tbody>
- </el-scrollbar>
- <el-pagination class="mg-t-8" v-if="pageable" @current-change="handleCurrentChange" :current-page="currentPage4" :page-size="pageSize" layout="total, prev, pager, next, jumper" :total="data.total"> </el-pagination>
- </table>
- </template>
- <script>
- export default {
-
- name: "ComTable",
-
- components: {},
-
- props: {
-
- data: Object,
-
- showHover: {
- type: Boolean,
- default: true,
- },
- canScroll: {
- type: Boolean,
- default: true,
- },
- pageSize: {
- type: Number,
- default: 0,
- },
- height: {
- type: String,
- default: "",
- },
- allowIndex: {
- type: Boolean,
- default: true,
- },
- },
-
- emits: {
-
- onPagging: null,
- check: null,
- },
-
- data() {
- return {
- hoverRow: -1,
- hoverCol: -1,
- sortCol: "",
- sortType: "",
- currentPage: 1,
- };
- },
- computed: {
- tableData() {
- let that = this;
- if (this.sortCol == "") {
- return this.data.data;
- } else {
- let data = this.data.data;
- data.sort((a, b) => {
- let rev = 1;
- if (that.sortType == "ASC") rev = 1;
- else if (that.sortType == "DESC") rev = -1;
- if (a[that.sortCol] > b[that.sortCol]) return rev * 1;
- if (a[that.sortCol] < b[that.sortCol]) return rev * -1;
- return 0;
- });
- return data;
- }
- },
- pageable() {
- return this.pageSize != 0;
- },
- pages() {
- if (this.pageable) return parseInt(this.data.total / this.pageSize) + 1;
- else return 0;
- },
- startRow() {
- if (this.pageable) return (this.currentPage - 1) * this.pageSize;
- else return 0;
- },
- endRow() {
- if (this.pageable) return this.currentPage * this.pageSize;
- else return this.data.data.length;
- },
- },
-
- methods: {
- onClick(col, data) {
- if (col.click) col.click(event, data);
- },
- onCheck(data) {
- data.checked = event.target.checked;
- this.$emit("check", { data: data, checked: data.checked });
- },
- onSort(col) {
- if (col.sortable == true) {
- this.sortCol = col.field;
- switch (this.sortType) {
- case "":
- this.sortType = "DESC";
- break;
- case "DESC":
- this.sortType = "ASC";
- break;
- case "ASC":
- this.sortType = "";
- break;
- }
- }
- },
- template(col, data) {
- if (!col.template) return data;
- else return col.template(data);
- },
- hover(row, col) {
- if (this.showHover) {
- this.hoverRow = row;
- this.hoverCol = col;
- }
- },
- leave() {
- this.hoverRow = -1;
- this.hoverCol = -1;
- },
- handleCurrentChange(val) {
- this.currentPage = val;
- this.$emit("onPagging", { pageIndex: this.currentPage, pageSize: this.pageSize, start: this.startRow, end: this.endRow });
- },
- },
-
- beforeCreate() {
-
- },
- created() {
-
- },
- beforeMount() {
-
- },
- mounted() {
-
- },
- beforeUpdate() {
-
- },
- updated() {
-
- },
- };
- </script>
- <style lang="less"></style>
|