The below code works perfectly but the if/else statements look so long and ugly. Is there a way I can avoid these statements?
CommissionTypeFilterVm is a list that contains 6 properties shown below whose values are either 1 or 0. I wanted to enable and disable the checkbox’s based on the values of these properties:
CommissionType
CommissionTrials
OverrideType
OverrideTrials
BonusType
AdjustmentType
Here is the code:
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);
}
}
}
It may depend on your exact
view.CommissionTypeFilterVm[0]object (what other properties does it have) but it looks like you try to do this :