<template>
|
<a-card :bordered="false">
|
<!-- 新增按钮 -->
|
<div class="table-operator" style="margin-bottom: 10px;">
|
<a-button
|
@click="handleAdd"
|
type="primary"
|
icon="plus"
|
:disabled="!selectedCuttingId || selectedCuttingId === '' || selectedCuttingId === '-1'"
|
>
|
新增
|
</a-button>
|
</div>
|
|
<!-- table区域-begin -->
|
<div>
|
<a-table
|
ref="table"
|
size="middle"
|
:scroll="{x:true}"
|
bordered
|
rowKey="id"
|
:columns="columns"
|
:dataSource="dataSource"
|
:pagination="ipagination"
|
:loading="loading"
|
class="j-table-force-nowrap"
|
@change="handleTableChange">
|
|
<template slot="htmlSlot" slot-scope="text">
|
<div v-html="text"></div>
|
</template>
|
<template slot="imgSlot" slot-scope="text,record">
|
<span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
|
<img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
|
</template>
|
<template slot="fileSlot" slot-scope="text">
|
<span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
|
<a-button
|
v-else
|
:ghost="true"
|
type="primary"
|
icon="download"
|
size="small"
|
@click="downloadFile(text)">
|
下载
|
</a-button>
|
</template>
|
|
<!-- 可编辑单元格 -->
|
<template
|
v-for="col in ['workpieceMaterial', 'ratedLife']"
|
:slot="col"
|
slot-scope="text, record"
|
>
|
<div :key="col">
|
<a-input
|
v-if="record.editable"
|
style="margin: -5px 0"
|
:value="text"
|
@change="e => handleChange(e.target.value, record.id, col)"
|
/>
|
<template v-else>
|
{{ text }}
|
</template>
|
</div>
|
</template>
|
|
<!-- 操作列 -->
|
<span slot="action" slot-scope="text, record">
|
<div class="editable-row-operations">
|
<span v-if="record.editable">
|
<a-popconfirm title="确定保存?" @confirm="() => save(record.id)">
|
<a>保存</a>
|
</a-popconfirm>
|
<a style="margin-left: 8px" @click="() => cancel(record.id)">取消</a>
|
</span>
|
<span v-else>
|
<a :disabled="editingKey !== ''" @click="() => edit(record.id)">编辑</a>
|
<a-divider type="vertical" />
|
<a-dropdown>
|
<a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
|
<a-menu slot="overlay">
|
<a-menu-item>
|
<a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
|
<a>删除</a>
|
</a-popconfirm>
|
</a-menu-item>
|
</a-menu>
|
</a-dropdown>
|
</span>
|
</div>
|
</span>
|
|
</a-table>
|
</div>
|
|
<rated-life-modal ref="modalForm" @ok="modalFormOk"></rated-life-modal>
|
</a-card>
|
</template>
|
|
<script>
|
import '@/assets/less/TableExpand.less'
|
import { mixinDevice } from '@/utils/mixin'
|
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
|
import { putAction, getAction, postAction } from '@/api/manage'
|
import RatedLifeModal from './modules/RatedLifeModal'
|
|
export default {
|
name: 'RatedLifeList',
|
mixins:[JeecgListMixin, mixinDevice],
|
components: {
|
RatedLifeModal
|
},
|
props: {
|
selectedCuttingId: {
|
type: String,
|
default: ''
|
}
|
},
|
data () {
|
return {
|
description: '额定寿命管理页面',
|
editingKey: '',
|
// 表头
|
columns: [
|
{
|
title: '#',
|
dataIndex: '',
|
key:'rowIndex',
|
width:60,
|
align:"center",
|
customRender:function (t,r,index) {
|
return parseInt(index)+1;
|
}
|
},
|
{
|
title:'刀具编码',
|
align:"center",
|
dataIndex: 'cuttingId_dictText',
|
sorter: true
|
},
|
{
|
title:'工件材质',
|
align:"center",
|
dataIndex: 'workpieceMaterial',
|
scopedSlots: { customRender: 'workpieceMaterial' }
|
},
|
{
|
title:'额定寿命',
|
align:"center",
|
dataIndex: 'ratedLife',
|
scopedSlots: { customRender: 'ratedLife' }
|
},
|
{
|
title: '操作',
|
dataIndex: 'action',
|
align:"center",
|
scopedSlots: { customRender: 'action' },
|
width: 200,
|
}
|
],
|
url: {
|
list: "/cms/ratedLife/list",
|
delete: "/cms/ratedLife/delete",
|
deleteBatch: "/cms/ratedLife/deleteBatch",
|
exportXlsUrl: "/cms/ratedLife/exportXls",
|
importExcelUrl: "cms/ratedLife/importExcel",
|
edit: "/cms/ratedLife/edit",
|
add: "/cms/ratedLife/add"
|
},
|
dictOptions:{},
|
superFieldList:[],
|
}
|
},
|
created() {
|
this.getSuperFieldList();
|
},
|
computed: {
|
importExcelUrl: function(){
|
return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
|
},
|
},
|
methods: {
|
initDictConfig(){
|
},
|
getRatedLife(cuttingId) {
|
this.queryParam.cuttingId = cuttingId;
|
// this.ipagination.current = 1;
|
this.loadData(1);
|
},
|
getSuperFieldList(){
|
let fieldList=[];
|
fieldList.push({type:'string',value:'cuttingId',text:'刀具ID'})
|
fieldList.push({type:'string',value:'workpieceMaterial',text:'工件材质'})
|
fieldList.push({type:'number',value:'ratedLife',text:'额定寿命'})
|
this.superFieldList = fieldList
|
},
|
|
// 新增行
|
handleAdd() {
|
// 如果已经在编辑状态,自动取消当前编辑项
|
if (this.editingKey !== '') {
|
// 查找正在编辑的记录并取消编辑
|
const editingRecord = this.dataSource.find(item => item.id === this.editingKey);
|
if (editingRecord) {
|
this.cancel(this.editingKey);
|
}
|
}
|
|
// 创建新的空数据项
|
const newRow = {
|
id: 'new_' + new Date().getTime(), // 临时ID
|
cuttingId: this.queryParam.cuttingId,
|
workpieceMaterial: '',
|
ratedLife: '',
|
editable: true,
|
isNew: true // 标记为新添加的行
|
};
|
|
// 将新行插入到数据源开头
|
this.dataSource = [newRow, ...this.dataSource];
|
this.editingKey = newRow.id;
|
},
|
|
// 编辑行数据
|
edit(key) {
|
// 先保存当前数据的副本
|
const newData = [...this.dataSource];
|
const target = newData.find(item => key === item.id);
|
this.editingKey = key;
|
|
if (target) {
|
target.editable = true;
|
this.dataSource = newData;
|
}
|
},
|
|
// 保存编辑的数据
|
save(key) {
|
const newData = [...this.dataSource];
|
const target = newData.find(item => key === item.id);
|
|
if (target) {
|
delete target.editable;
|
this.dataSource = newData;
|
|
// 判断是新增还是编辑
|
if (target.isNew) {
|
// 新增操作
|
const params = { ...target };
|
delete params.id;
|
delete params.isNew;
|
// 删除创建时间字段,让后端自动生成
|
delete params.createTime;
|
delete params.updateTime;
|
|
postAction(this.url.add, params).then(res => {
|
if (res.success) {
|
this.$message.success('新增成功');
|
this.editingKey = '';
|
this.loadData(); // 重新加载数据
|
} else {
|
this.$message.error(res.message);
|
}
|
}).catch(err => {
|
this.$message.error('新增失败');
|
console.log(err);
|
});
|
} else {
|
// 编辑操作 - 只发送需要更新的字段
|
const params = {
|
id: target.id,
|
cuttingId: target.cuttingId,
|
workpieceMaterial: target.workpieceMaterial,
|
ratedLife: target.ratedLife,
|
};
|
|
putAction(this.url.edit, params).then(res => {
|
if (res.success) {
|
this.$message.success('保存成功');
|
this.editingKey = '';
|
this.loadData(); // 重新加载数据
|
} else {
|
this.$message.error(res.message);
|
}
|
}).catch(err => {
|
this.$message.error('保存失败');
|
console.log(err);
|
});
|
}
|
}
|
},
|
|
// 取消编辑
|
cancel(key) {
|
const newData = [...this.dataSource];
|
const targetIndex = newData.findIndex(item => key === item.id);
|
this.editingKey = '';
|
|
if (targetIndex >= 0) {
|
// 如果是新增的行,直接删除
|
if (newData[targetIndex].isNew) {
|
newData.splice(targetIndex, 1);
|
this.dataSource = newData;
|
} else {
|
// 如果是编辑的行,从后端重新获取数据以恢复原始值
|
const target = newData[targetIndex];
|
getAction(this.url.list, { id: key }).then(res => {
|
if(res.success && res.result.records.length > 0) {
|
const originalData = res.result.records[0];
|
Object.keys(target).forEach(key => {
|
if(key !== 'editable') {
|
target[key] = originalData[key];
|
}
|
});
|
delete target.editable;
|
this.dataSource = newData;
|
}
|
});
|
}
|
}
|
},
|
|
// 处理单元格值变化
|
handleChange(value, key, column) {
|
const newData = [...this.dataSource];
|
const target = newData.find(item => key === item.id);
|
|
if (target) {
|
target[column] = value;
|
this.dataSource = newData;
|
}
|
}
|
}
|
}
|
</script>
|
|
<style scoped>
|
@import '~@assets/less/common.less';
|
|
.editable-row-operations a {
|
margin-right: 8px;
|
}
|
</style>
|