要在传递 customerId
的同时也传递 customerName
,您可以对现有代码做一些调整。在 batchCustomerTags
函数中,您需要确保将 customerName
包含在调用的参数中。为了实现这一点,您可以从所选行中提取必要的信息。
以下是修改后的示例代码:
<template>
<a-table
:columns="columns"
:data-source="data"
:scroll="{
x: columns.reduce((sum, col) => sum + (col.width || 0), 0) > 1800 ? 'max-content' : undefined
}"
:pagination="{
current: currentPage,
pageSize: pageSize,
total: totalRecords,
pageSizeOptions: ['10', '20', '30', '40', '100'],
onShowSizeChange: handlePageSizeChange,
onChange: handlePageChange,
showQuickJumper: true,
showSizeChanger: true,
showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录,共 ${total} 条记录`
}"
:custom-row="customRow"
style="padding: 5px"
bordered
:row-selection="rowSelection"
:rowKey="record => record.customerId" <!-- Assuming customerId is unique -->
/>
</template>
<script setup>
import { reactive, ref } from 'vue';
import { message } from 'ant-design-vue';
const columns = [
// Define your columns here
];
const data = ref([]); // Your data source
const currentPage = ref(1);
const pageSize = ref(10);
const totalRecords = ref(0);
const rowSelection = reactive({
preserveSelectedRowKeys: true,
selectedRowKeys: [],
onChange(selectedRowKeys, selectedRows) {
newSelectedRowKeys.value = selectedRowKeys;
// Update the names of the selected customers
newSelectedCustomerNames.value = selectedRows.map(row => row.customerName);
console.log('Selected Row Keys:', selectedRowKeys);
}
});
const handlePageSizeChange = (current, size) => {
pageSize.value = size;
};
const handlePageChange = (page, size) => {
currentPage.value = page;
};
const customRow = (record) => {
return {
onClick: () => {
console.log('Clicked row:', record);
}
};
};
// Add other necessary methods and API calls
// Example for batch customer tags function
const batchCustomerTags = async ({ tagsId, customerIds, customerNames }) => {
// Implement your API call here and return a promise
console.log("Batch processing for IDs:", customerIds);
console.log("Batch processing for Names:", customerNames);
};
// To simulate handling of selected customers and displaying messages after an operation.
const newSelectedRowKeys = ref([]);
const newSelectedCustomerNames = ref([]);
function processBatchAction() {
const targetKeys = { value }; // Obtain this based on your implementation context
batchCustomerTags({
tagsId: targetKeys.value,
customerIds:newSelectedRowKeys.value,
customerNames:newSelectedCustomerNames.value
}).then(response => {
if(response.success){
message.success(response.message);
} else {
message.error(response.message);
}
});
}
</script>
修改说明:
新增变量:
- 增加了一个响应式引用
newSelectedCustomerNames
用于存储所选客户的名称。
- 增加了一个响应式引用
更新选择事件处理函数:
- 在
onChange
中,除了更新所选的selectedRowKeys
外,也通过映射选择的行来获取每个顾客的名字,并将其存储到newSelectedCustomerNames
.
- 在
更新批处理函数:
- 修改了
batchCustomerTags
函数,使其接收一个包含客户 ID 和姓名的新对象。
- 修改了
现在,当您调用 processBatchAction
函数时,它会同时传递选中的客户 ID 和姓名。这样就能满足新的需求。
内容由零声教学AI助手提供,问题来源于学员提问