为三场检查制作条件逻辑的最佳方法是什么

时间:2014-03-19 15:23:48

标签: javascript loops

我有3个相互关联的列表框。每个列表框都填充到下一个列表框值。我想获取选定的值,然后通过ajax请求发布到服务器。

用户可能只选择LIST A或LIST A和LIST B或所有LISTBOX值。重要的是url可能会更改每个选定的值。因此,我想基于这些值编写条件逻辑。我知道,我可以使用嵌套的if,但我想学习更合适的方法。

+------------------------+-----+
| LISTBOX A              | --- |
+------------------------+-----+

+------------------------+-----+
| LISTBOX B              | --- |
+------------------------+-----+   

+------------------------+-----+
| LISTBOX C              | --- |
+------------------------+-----+

// Pseudo Code
IF A SELECTED 
   POST ONLY VALUE A
IF A AND B SELECTED
   POST VALUE A AND VALUE B
IF A, B AND C SELECTED
   POST VALUE A, VALUE B, VALUE C

我使用的是ExtJS框架,这里是列表框定义。

{
    xtype: 'combobox',
    fieldLabel: 'ANA MAL GRUBU',
    store: articleMain,
    id: 'art-main-group',
    queryMode: 'local',
    autoSelect: true,
    forceSelection: true,
    triggerAction: 'all',
    inputWidth: 240,
    margin: '5 0 0 0',
    listConfig: { cls: 'combo-dep' },
    valueField: 'PWHG',
    displayField: 'PWHG_BEZ',
    listeners: {
        select: function(combo) {
            articleBase.proxy.extraParams = {'maingroup': combo.getValue(), type: 'article_base'};
            articleBase.load();
        }
    }
},
{
    xtype: 'combobox',
    fieldLabel: 'MAL GRUBU',
    store: articleBase,
    id: 'art-base-group',
    queryMode: 'local',
    autoSelect: false,
    forceSelection: true,
    triggerAction: 'all',
    editable: false,
    valueField: 'PWG',
    displayField: 'PWG_BEZ',
    inputWidth: 240,
    margin: '10 0 0 0',
    listConfig: { cls: 'combo-dep' },
    listeners: {
        select: function(combo) {
            articleSub.proxy.extraParams = {'maingroup': Ext.getCmp('art-main-group').getValue(), 'basegroup': combo.getValue(), 'type': 'article_sub'}
            articleSub.load();
        }
    }
},
{
    xtype: 'combobox',
    fieldLabel: 'ALT MAL GRUBU',
    store: articleSub,
    id: 'art-sub-group',
    queryMode: 'local',
    autoSelect: false,
    forceSelection: true,
    triggerAction: 'all',
    editable: false,
    valueField: 'PWUG',
    displayField: 'PWUG_BEZ',
    inputWidth: 240,
    margin: '10 0 0 0',
    listConfig: { cls: 'combo-dep' }
},
{
    xtype: 'radiogroup',
    fieldLabel: 'SEÇİLEN MAL GRUBU',
    id: 'art-status',
    width: 300,
    margin: '10 0 0 0',
    layout: 'hbox',
    items: [
        {
            boxLabel: 'DAHİL',
            name: 'artstatus',
            inputValue: 'INCLUDE'
        },
        {
            boxLabel: 'HARİÇ',
            name: 'artstatus',
            inputValue: 'EXCLUDE',
            margin: '0 0 0 20'
        }
    ],
    listeners: {
        change: function(radio, newValue) {
            console.log(newValue.artstatus);
        }
    }
}

] },

2 个答案:

答案 0 :(得分:0)

一个班轮:

if(x = (a && ((b && ((c && 3) || 2)) || 1))) doStuff(x) ;

如果a为真,则b为1,如果a为b则为b,如果a和b为真,则为2,如果全部为真,则为3,如果a为真,则为3,如果a为b,则为假是

答案 1 :(得分:0)

感谢您的所有建议。结果,代码可读性在我做的事实如下(除了条件之外没有机会使用其他东西)。

PS:我改变了if子句的反向顺序,以便最后一个条件成为第一个。

    // LIST C equal to sub_group, LIST B equal to base_group
    handler: function() {
    var main_group, base_group, sub_group;
    if (Ext.getCmp('art-sub-group').getValue() != null) {
        main_group = Ext.getCmp('art-main-group').getValue();
        base_group = Ext.getCmp('art-base-group').getValue();
        sub_group = Ext.getCmp('art-sub-group').getValue();
    } else if (Ext.getCmp('art-main-group').getValue() != null && Ext.getCmp('art-base-group').getValue() != null) {
        base_group = Ext.getCmp('art-base-group').getValue();
        main_group = Ext.getCmp('art-main-group').getValue();
    } else {
        main_group = Ext.getCmp('art-main-group').getValue();
    }
}