EXTJS: Window Form Masking With Statusbar

May 24th, 2010 by aditia rahman / No Comments  

     

In this tutorial i want to show you how to masking a form when user enter a submit button, statusbar that used in this tutorial is from the Extjs ux example, so I borrow this cause it is useable plugin, like always on the server side process I use simple PHP scripting. I created this post because when I used a form in Extjs and the user make a request it seem not really responsive, the system should interact the user, inform what is going on in the system and most user is not advance user. Let’s get started:

1. Setting the StatusBar

Copy the StatusBar css file and icon from the default examples to your desired folder, first the css file you can find it in “extjs/examples/ux/statusbar/css/statusbar.css”, I copy it to another folder called “css” and next the statusbar icon is in “/extjs/examples/ux/statusbar/images” and I copy all the icon files to another folder called “img” most the file structure you can see it on the image on the right, this is how it look in Netbeans project. You can see the “form_masking.html” and “server_response.php” file, this is the main file that we have to create, and it will be mentioned on the next step, but before that we have to edit the StatusBar.css file that we have copied so the icon can be loaded, by changing the “background-image” properties. So find this all code like this

background-image: url(../images/loading.gif);

Change the background-image to to this code, and do this to all background-image but you only need to change the url path not the image file

background-image: url(../img/loading.gif);

2. Create the main file

Like usual include EXTJS file in your html header, but this time you need to include the StatusBar.css that you have modified and the StatusBar.js (I don’t modified this file, this file directly linked from the Extjs ux examples, you can found it bundled on the examples)

<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css"/>
<link rel="stylesheet" type="text/css" href="css/StatusBar.css"/>
<script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="extjs/ext-all.js"></script>
<script type="text/javascript" src="extjs/examples/ux/statusbar/StatusBar.js"> </script>

3. Create the Form, Function Handler and the Window

I Create a file named “form_masking.html”, the form is similar to the form from the previous post, but in this code I added the key EventObject on the form, so when user press enter on the form, it will submit the form like the traditional html form

<script type="text/javascript">
Ext.onReady(function(){
    var formMasking = new Ext.FormPanel({
        frame: false, border: false, buttonAlign: 'center',
        url: 'server_response.php', method: 'POST', id: 'frmMasking',
        bodyStyle: 'padding:10px 10px 15px 15px;background:#dfe8f6;',
        labelWidth: 150, width: 300,
        items: [{
                xtype: 'textfield',
                fieldLabel: 'Username',
                name: 'username',
                id: 'formUsername',
                allowBlank: false
            }, {
                xtype: 'textfield',
                fieldLabel: 'Password',
                name: 'password',
                id: 'formPassword',
                allowBlank: false,
                inputType: 'password'
            }
        ],
        keys: [
            { key: [Ext.EventObject.ENTER], fn: fnLogin }
        ],
        buttons: [
            { text: 'Login', handler: fnLogin },
            { text: 'Reset', handler: function() {
                    formMasking.getForm().reset();
                }
            }
        ]
    });

    function fnLogin() {
        // this function must before
        Ext.getCmp('frmMasking').on({
            beforeaction: function() {
                Ext.getCmp('winMask').body.mask();
                Ext.getCmp('sbWin').showBusy();
            }
        });

        formMasking.getForm().submit({
            success: function() {
                Ext.getCmp('winMask').body.unmask();
                Ext.getCmp('sbWin').setStatus({
                    text: 'Login success !',
                    iconCls: 'x-status-valid'
                });
            },
            failure: function(form, action) {
                Ext.getCmp('winMask').body.unmask();
                if (action.failureType == 'server') {
                    obj = Ext.util.JSON.decode(
                        action.response.responseText);
                    Ext.getCmp('sbWin').setStatus({
                        text: obj.errors.reason,
                        iconCls: 'x-status-error'
                    });
                } else {
                    if (typeof(action.response) == 'undefined') {
                        Ext.getCmp('sbWin').setStatus({
                            text: 'Field cannot be empty !',
                            iconCls: 'x-status-error'
                        });
                    } else {
                        Ext.Msg.alert('Warning!',
                            'Server is unreachable : '
                                + action.response.responseText);
                    }
                }
            }
        });
    }

    var winMasking = new Ext.Window({
        title: 'EXTJS &mdash; Window Masking', layout: 'fit',
        width: 350, height: 150, resizable: false, closable: false,
        items: [formMasking], id:'winMask',
        bbar: new Ext.ux.StatusBar({
            text: 'Ready',
            id: 'sbWin',
            iconCls: 'x-status-saved'
        })
    });

    winMasking.show();
});
</script>

4. The Function Handler

The function handler name is “fnLogin” as you can see on the code above (on the step 3), when the form submitted the event on beforeaction is called an it masking the window, why not mask the form? because when I try mask the form it will only masked the fieldLabel and the textField but not the button, so just mask the window and it will mask everything inside it. The beforeaction event have to be defined before the form submit function, or it wil not work.

5. Create The Server Response

I Create a file named “server_response.php”, the php code is the same as the previous post, it just checking username from the hardcoded array.

<?php

$username_list = array('admin', 'administrator', 'superadmin', 'john', 'michael');
if (in_array($_POST['username'], $username_list)) {
    echo "{success:true}";
}
else {
    echo "{success:false, errors: { reason: 'User not found !' }}";
}

?>

The expected screen should be like the image below, the form when in the default condition, loading, give the error message, and when the success is achieved.

If you want another form load masking example there a sample by Saki here (I think it is more advanced), or if you like to add more advance StatusBar validation you can learn the official samples here. Finally like always I provide the code the be downloaded but I’m not included the Extjs file, you can download it from the official website, here.

DOWNLOAD SOURCE

        submit to reddit Delicious

No Comment Leave a Comment Subscribe RSS

Leave a Comment