<template>
<section class="el-table-container">
<el-table
ref="tableGroup"
:data="data"
:highlight-current-row="true"
:row-key="getRowKeys"
border
stripe
size="small"
style="width: 100%"
:header-cell-class-name="cellClass"
@selection-change="handleSelectionChange"
@select="handleSelect"
@select-all="handleSelectAll"
@row-click="handleRowClick"
>
<el-table-column
v-if="isSelection"
type="selection"
align="center"
width="55"
:selectable="selected"
/>
<template v-for="(column, index) in columns">
<el-table-column
v-if="column.slotName"
:key="index"
:prop="column.prop"
:label="column.label"
:width="column.width"
align="center"
>
<template slot-scope="scope">
<slot :name="column.slotName" :data="scope.row"></slot>
</template>
</el-table-column>
<el-table-column
v-if="!column.slotName && column.prop"
:key="index"
:label="column.label"
:prop="column.prop"
:width="column.width"
:class-name="column.className"
align="center"
/>
</template>
<el-table-column
:width="fixedWidth"
label="操作"
fixed="right"
align="center"
>
<template slot-scope="scope">
<el-button
v-if="operationGroup('edit')"
icon="el-icon-edit"
type="success"
size="mini"
@click="handleEdit(scope.$index, scope.row)"
>
编辑
</el-button>
<el-button
v-if="operationGroup('insert')"
icon="el-icon-plus"
size="mini"
@click="handleInsert(scope.$index, scope.row)"
>
增加
</el-button>
<el-button
v-if="operationGroup('delete')"
icon="el-icon-close"
type="danger"
size="mini"
@click="handleDelete(scope.$index, scope.row)"
>
删除
</el-button>
<el-button
v-if="operationGroup('check')"
icon="el-icon-info"
type="info"
size="mini"
@click="handleCheck(scope.$index, scope.row)"
>
查看
</el-button>
<span v-if="operationGroup('custom')" style="display: inline-block">
<el-button
v-for="(i, index) in customList"
v-if="
!i.condition ||
i.condition.value.indexOf(scope.row[i.condition.key]) >= 0
"
:key="index"
:icon="i.icon"
:type="i.btnType"
size="mini"
@click="handleCustom(scope.$index, scope.row, i.handler)"
>
{{ i.buttonName }}
</el-button>
</span>
</template>
</el-table-column>
</el-table>
<div class="el-pagination-total">
共 <span>{{ total }}</span
>条
</div>
<el-pagination
v-if="isPagination"
background
:current-page="currentPage"
:page-sizes="pageSizes"
:page-size="pageSize"
:layout="layout"
:total="total"
class="el-pagination-c"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
<el-row class="el-row-select">
<div class="box">
<el-button
style="margin-right: 8px"
type="primary"
size="small"
@click="handleConfirm"
>确定</el-button
>
<el-button size="small" @click="handleCancel">取消</el-button>
</div>
</el-row>
</section>
</template>
<script>
export default {
name: "ElTableSelect",
props: {
checkData: {
type: [Array, String],
default: () => [],
},
columns: {
type: Array,
required: true,
},
data: {
type: Array,
required: true,
},
isSelection: {
type: Boolean,
default: false,
},
operation: {
type: Array,
default: () => [],
},
operationCustomLength: {
type: Number,
default: 0,
},
judgeValue: {
required: true,
type: String,
},
isSelectOne: {
type: Boolean,
default: false,
},
isPagination: {
type: Boolean,
default: true,
},
total: {
type: Number,
default: 0,
},
currentPage: {
type: Number,
default: 1,
},
pageSizes: {
type: Array,
default: () => [10, 20, 50, 100],
},
pageSize: {
type: Number,
default: 10,
},
layout: {
type: String,
default: "sizes,prev, pager, next, jumper",
},
},
data() {
return {
getRowKeys(row) {
return row.index;
},
cacheCurrentPage: 1,
cacheSelectData: [],
};
},
computed: {
fixedWidth() {
return this.operationCustomLength
? this.operationCustomLength * 110 - 5
: this.operation.length * 110 - 5;
},
},
mounted() {
this.cacheCurrentPage = this.currentPage;
},
methods: {
againShow(data) {
this.cacheSelectData = data;
console.log("this.cacheSelectData", data);
this.echoSelection(this.cacheSelectData[this.cacheCurrentPage]);
},
cellClass(row) {
if (this.isSelectOne) {
if (row.columnIndex === 0) {
return "DisableSelection";
}
}
},
selected(row, index) {
return true;
},
handleRowClick(row) {
let _selectData;
let selected =
this.cacheSelectData[this.cacheCurrentPage] &&
this.cacheSelectData[this.cacheCurrentPage].some(
(item) => item.id === row.id
);
if (selected === true) {
this.cacheSelectData[this.cacheCurrentPage] = this.cacheSelectData[
this.cacheCurrentPage
].filter((item) => {
return item.id !== row.id;
});
this.$refs.tableGroup.toggleRowSelection(row, false);
} else {
this.$refs.tableGroup.toggleRowSelection(row);
if (this.isSelectOne) {
this.$refs.tableGroup.clearSelection();
this.$refs.tableGroup.toggleRowSelection(row, true);
_selectData = this.$refs.tableGroup.selection;
this.cacheSelectData[this.cacheCurrentPage] = _selectData;
this.cacheSelectData.forEach((item, index) => {
if (index !== this.cacheCurrentPage) {
this.cacheSelectData[index] = null;
}
});
} else {
_selectData = this.$refs.tableGroup.selection;
this.cacheSelectData[this.cacheCurrentPage] = _selectData;
}
}
this.handleSelectionChange(_selectData);
},
operationGroup(affix) {
if (affix === "custom") {
this.customList = this.operation.filter((n) => {
return typeof n === "object";
});
return this.customList;
} else {
return this.operation.indexOf(affix) >= 0;
}
},
handleCustom(index, row, handler) {
this.$emit(handler, index, row);
},
handleEdit(index, row) {
this.$emit("handleEdit", index, row);
},
handleInsert(index, row) {
this.$emit("handleInsert", index, row);
},
handleDelete(index, row) {
this.$confirm("确认删除?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}).then(() => {
this.$emit("handleDelete", index, row);
});
},
handleCheck(index, row) {
event.stopPropagation();
this.$emit("handleCheck", index, row);
},
handleSelectionChange(val) {
this.$emit("handleSelectionChange", val);
},
handleSelect(list, row) {
if (this.isSelectOne) {
if (list.length > 1) {
let del_row = list.shift();
this.$refs.tableGroup.toggleRowSelection(del_row, false);
this.cacheSelectData[this.cacheCurrentPage] = list;
console.log("cacheSelectData", this.cacheSelectData);
} else {
this.cacheSelectData[this.cacheCurrentPage] = list;
this.cacheSelectData.forEach((item, index) => {
if (index !== this.cacheCurrentPage) {
this.cacheSelectData[index] = null;
}
});
console.log("this.cacheSelectData", this.cacheSelectData);
}
} else {
this.cacheSelectData[this.cacheCurrentPage] = list;
}
this.$emit("handleSelect", list);
},
handleSelectAll(list) {
if (this.isSelectOne) {
if (list.length > 1) {
list.length = 1;
}
} else {
this.cacheSelectData[this.cacheCurrentPage] = list.length ? list : null;
}
this.$emit("handleSelect", list);
},
setSelection(row, bool) {
this.$refs.tableGroup.toggleRowSelection(row, bool);
},
clearSelection() {
this.$refs.tableGroup.clearSelection();
},
echoSelection(currentPageSelectList) {
if (currentPageSelectList) {
const _list = this.data.filter((row) => {
return currentPageSelectList.some((item) => {
return item[this.judgeValue] === row[this.judgeValue];
});
});
_list.forEach((row) => {
this.setSelection(row);
});
}
},
handleSizeChange(num) {
this.$emit("handleSizeChange", num);
},
handleCurrentChange(page) {
this.$emit("handleCurrentChange", page);
this.cacheCurrentPage = page;
setTimeout(() => {
this.echoSelection(this.cacheSelectData[page]);
}, 500);
},
handleConfirm() {
const _AllSelectData = []
.concat(...this.cacheSelectData)
.filter((item) => {
return item !== null && typeof item === "object";
});
this.$emit("handleConfirm", _AllSelectData, this.cacheSelectData);
this.cacheSelectData = [];
this.clearSelection();
},
handleCancel() {
this.cacheSelectData = [];
this.$emit("handleCancel");
this.clearSelection();
},
},
};
</script>
<style scoped lang="scss" ref="stylesheet/scss">
.el-table-container {
.el-pagination-c {
float: right;
margin-top: 1.5em;
}
.el-pagination-total {
float: left;
margin-top: 1.5em;
span {
margin: 0 4px;
color: #409eff;
}
}
.el-row-select {
margin-top: 6em;
text-align: right;
.box {
display: inline-block;
}
}
}
// 全选按钮的隐藏样式:
::v-deep .el-table .DisableSelection .cell .el-checkbox__inner {
display: none;
position: relative;
}
::v-deep .el-table .DisableSelection .cell:before {
content: "";
position: absolute;
}
</style>