1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| <template> <Table class="border-table" :columns="columns" :data="resData" border :span-method="handleSpan" > <!-- 操作 --> <template slot="xm" slot-scope="{ row, index }"> <a href="">{{ row.name }}</a> </template> <!-- 操作 --> <template slot="action" slot-scope="{ row, index }"> <div class="tableBtnBox enniu"> <div class="tableBtn"> <Button class="cz_btn" type="success" size="small">查看详情</Button> </div> </div> </template> </Table> </template> <script> export default { data() { return { columns: [ { title: "Name", // key: "name", slot: "xm", }, { title: "Age", key: "age", }, { title: "Address", key: "address", }, { title: "Desc", key: "desc", }, { title: "操作", align: "center", slot: "action", }, ], resData: [ { "id": 1, "name": "John Doe", "age": 32, "address": "202 Main Street, New York, NY", "desc": "Data engineer" }, { "id": 2, "name": "Lisa Wang", "age": 28, "address": "99 Long Road, Shanghai, CN", "desc": "Product manager" }, { "id": 3, "name": "Jane Smith", "age": 35, "address": "99 Long Road, Shanghai, CN", "desc": "Software architect" }, { "id": 4, "name": "Jane Smith", "age": 29, "address": "51 South Avenue, Miami, FL", "desc": "UI designer" }, { "id": 5, "name": "Tom Brown", "age": 29, "address": "1675 Park Avenue, Paris, FR", "desc": "Backend developer" }, { "id": 6, "name": "Alice Chen", "age": 30, "address": "20 Surry Street, London, UK", "desc": "DevOps engineer" } ], nameMerges: [], addrMerges: [], opMerges: [], }; }, methods: { /** * 合并行或列的算法 * @param row 当前行 * @param column 当前列 * @param rowIndex 当前行索引 * @param columnIndex 当前列索引 * @returns {{colspan: (number), rowspan: *}} */ handleSpan({ row, column, rowIndex, columnIndex }) { // 姓名合并 if (columnIndex === 0) { // 二维数组存储的数据 取出 const _row = this.nameMerges[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col, }; }
// 地址合并 if (columnIndex === 2) { // 二维数组存储的数据 取出 const _row = this.addrMerges[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col, }; }
if (columnIndex === 4) { // 操作列序号 // 二维数组存储的数据 取出 const _row = this.opMerges[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col, }; } }, formatData() { this.nameMerges = this.getMerges(this.resData, "name"); this.addrMerges = this.getMerges(this.resData, "address"); this.opMerges = [...this.nameMerges]; // 复制name的合并信息
console.log(this.opMerges); }, /** * * @param data 数据列表 * @param key 将要合并的对比字段 * @returns {*[]} */ getMerges(data, key) { // 页面展示的数据,不一定是全部的数据,所以每次都清空之前存储的 保证遍历的数据是最新的数据。以免造成数据渲染混乱 let spanArr = []; let pos = 0; // 遍历数据 data.forEach((item, index) => { // 判断是否是第一项 if (index === 0) { spanArr.push(1); pos = 0; } else { // 不是第一项时,就根据标识去存储 if (data[index][key] === data[index - 1][key]) { // 查找到符合条件的数据时每次要把之前存储的数据+1 spanArr[pos] += 1; spanArr.push(0); } else { // 没有符合的数据时,要记住当前的index spanArr.push(1); pos = index; } } }); return spanArr; }, }, mounted() { this.formatData(); }, }; </script> <style scoped>
</style>
|