反向推导 JavaScript 代码:从后端数据推导出前端 UI 状态
反向推导 JavaScript 代码:从后端数据推导出前端 UI 状态
假设你有一个后端数据结构 extInfOptionAuths,它包含了用户选择的选项信息。现在需要根据这个数据结构,反向推导出前端 UI 应该呈现的状态,例如哪些复选框应该选中,下拉菜单应该选择什么值等。
原始数据处理逻辑:
function createAuthObject(authKey, authVals) {
return {
authScope: 1,
authKey,
authVals
};
}
if (isCheckboxOrganization) {
params.extInfOptionAuths.push(createAuthObject('RealOrganization.resourceId', selectOrganizationIds));
}
if (isCheckboxTenant) {
params.extInfOptionAuths.push(createAuthObject('RealTenant.resourceId', selectFilterTenantIds));
}
if (isCheckboxBusiness) {
params.extInfOptionAuths.push(createAuthObject('webTenants.resourceId', selectedTenantIds));
params.extInfOptionAuths.push(createAuthObject('RealBusiness.resourceId', selectedBusinessIds));
}
if (isCheckboxArea) {
params.extInfOptionAuths.push(createAuthObject('ResourcePool.type', selectAreaIdList));
}
if (isCheckboxPool) {
params.extInfOptionAuths.push(createAuthObject('ResourcePool.resourceId', filtSelectedPoolIds));
}
if (isSelectPod) {
params.extInfOptionAuths.push(createAuthObject('webPoolIds.resourceId', selectedPoolIds));
params.extInfOptionAuths.push(createAuthObject('Pod.resourceId', selectedPodIds));
}
反向推导逻辑:
根据给定的逻辑,可以编写以下代码来反向推导出返显的逻辑:
// 反向推导出选中的复选框
const isCheckboxOrganization = params.extInfOptionAuths.some(authObj => authObj.authKey === 'RealOrganization.resourceId');
const isCheckboxTenant = params.extInfOptionAuths.some(authObj => authObj.authKey === 'RealTenant.resourceId');
const isCheckboxBusiness = params.extInfOptionAuths.some(authObj => authObj.authKey === 'RealBusiness.resourceId');
const isCheckboxArea = params.extInfOptionAuths.some(authObj => authObj.authKey === 'ResourcePool.type');
const isCheckboxPool = params.extInfOptionAuths.some(authObj => authObj.authKey === 'ResourcePool.resourceId');
const isSelectPod = params.extInfOptionAuths.some(authObj => authObj.authKey === 'Pod.resourceId');
// 反向推导出选中的值
const selectOrganizationIds = params.extInfOptionAuths
.find(authObj => authObj.authKey === 'RealOrganization.resourceId')?.authVals ?? [];
const selectFilterTenantIds = params.extInfOptionAuths
.find(authObj => authObj.authKey === 'RealTenant.resourceId')?.authVals ?? [];
const selectedTenantIds = params.extInfOptionAuths
.find(authObj => authObj.authKey === 'webTenants.resourceId')?.authVals ?? [];
const selectedBusinessIds = params.extInfOptionAuths
.find(authObj => authObj.authKey === 'RealBusiness.resourceId')?.authVals ?? [];
const selectAreaIdList = params.extInfOptionAuths
.find(authObj => authObj.authKey === 'ResourcePool.type')?.authVals ?? [];
const filtSelectedPoolIds = params.extInfOptionAuths
.find(authObj => authObj.authKey === 'ResourcePool.resourceId')?.authVals ?? [];
const selectedPoolIds = params.extInfOptionAuths
.find(authObj => authObj.authKey === 'webPoolIds.resourceId')?.authVals ?? [];
const selectedPodIds = params.extInfOptionAuths
.find(authObj => authObj.authKey === 'Pod.resourceId')?.authVals ?? [];
// 根据选中的复选框和值来进行返显的逻辑
if (isCheckboxOrganization) {
// 进行返显逻辑,将 selectOrganizationIds 显示为选中状态
}
if (isCheckboxTenant) {
// 进行返显逻辑,将 selectFilterTenantIds 显示为选中状态
}
if (isCheckboxBusiness) {
// 进行返显逻辑,将 selectedTenantIds 和 selectedBusinessIds 显示为选中状态
}
if (isCheckboxArea) {
// 进行返显逻辑,将 selectAreaIdList 显示为选中状态
}
if (isCheckboxPool) {
// 进行返显逻辑,将 filtSelectedPoolIds 显示为选中状态
}
if (isSelectPod) {
// 进行返显逻辑,将 selectedPoolIds 和 selectedPodIds 显示为选中状态
}
根据实际情况,你需要将注释的部分替换为相应的返显逻辑,以实现正确的返显效果。
原文地址: https://www.cveoy.top/t/topic/pdQE 著作权归作者所有。请勿转载和采集!