下面的代码运行良好,但 if/else 语句看起来又长又丑。有没有办法可以避免这些陈述?
CommissionTypeFilterVm
是一个包含 6 个属性的列表,如下所示,其值为1
或0
。我想根据这些属性的值启用和禁用复选框:
CommissionType
CommissionTrials
OverrideType
OverrideTrials
BonusType
AdjustmentType
这是代码:
if (view != null)
{
if (view.CommissionTypeFilterVm !=null && view.CommissionTypeFilterVm.length>0)
{
if (view.CommissionTypeFilterVm[0].CommissionType != 1) {
this.$commissionType.prop("checked", false).prop("disabled", true);
} else {
this.$commissionType.prop("disabled", false).prop("checked", true);
}
if (view.CommissionTypeFilterVm[0].CommissionTrials != 1) {
this.$commissionTrails.prop("checked", false).prop("disabled", true);
} else {
this.$commissionTrails.prop("disabled", false).prop("checked", true);
}
if (view.CommissionTypeFilterVm[0].OverrideType != 1) {
this.$overrideType.prop("checked", false).prop("disabled", true);
} else {
this.$overrideType.prop("disabled", false).prop("checked", true);
}
if (view.CommissionTypeFilterVm[0].OverrideTrials != 1) {
this.$overrideTrails.prop("checked", false).prop("disabled", true);
} else {
this.$overrideTrails.prop("disabled", false).prop("checked", true);
}
if (view.CommissionTypeFilterVm[0].BonusType != 1) {
this.$bonusType.prop("checked", false).prop("disabled", true);
} else {
this.$bonusType.prop("disabled", false).prop("checked", true);
}
if (view.CommissionTypeFilterVm[0].AdjustmentType != 1) {
this.$adjustmentType.prop("checked", false).prop("disabled", true);
} else {
this.$adjustmentType.prop("disabled", false).prop("checked", true);
}
}
}