» Fig Leaf Software Home

We've Got You Covered.

Wednesday, April 2, 2014

Developing an Ext JS 4 Checkbox Group with “Other (please specify)” option

I’m currently working on a dynamic forms/workflow solution for one of our clients. One of the requirements is to allow the user to not only choose from a set of values that are represented as a series of checkboxes, but also be able to specify their own custom value if necessary.
Image
In order to meet this requirement I subclassed the Ext.form.CheckboxGroup and it’s related layout – Ext.layout.container.CheckboxGroup
Defining a custom layout
Making room for the additional checkbox was a relatively straightforward affair. We just added an additional row to the layout class’ render template as illustrated on line 15 of the following source listing :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Ext.define('Ext.ux.layout.container.CheckboxOther', {
    extend: 'Ext.layout.container.CheckboxGroup',
    alias: ['layout.checkboxgroupother'],
 
    renderTpl: [
        '<table id="{ownerId}-innerCt" class="' + Ext.plainTableCls + '" cellpadding="0"',
            'role="presentation" style="{tableStyle}">',
            '<tbody role="presentation"><tr role="presentation">',
            '<tpl for="columns">',
                '<td class="{parent.colCls}" valign="top" style="{style}" role="presentation">',
                    '{% this.renderColumn(out,parent,xindex-1) %}',
                '</td>',
            '</tpl>',
        '</tr>',
        '<tr><td colspan="{[values.columnCount]}" class="{colCls}" valign="top" style="{style}" role="presentation"></td></tr>',
        '</tbody>',
        '</table>'
    ]
 
});
Extending the Ext.form.CheckboxGroup class
The final step was to extend the Ext.form.CheckboxGroup class in order to inject the “Other” textfield. This involved:
  • Rendering the textfield into the layout template (line 34)
  • Using the Ext.util.TextMetrics class to determine the pixel width of the text field prompt and then setting its labelWidth property accordingly (lines 30-31)
  • Overriding the getValue(), setValue(), getSubmitData(), and getModelData() fields to handle the textfield input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
Ext.define('Ext.ux.form.CheckboxGroupOther', {
    extend: 'Ext.form.CheckboxGroup',
    alias: 'widget.checkboxgroupother',
    requires: ['Ext.ux.layout.container.CheckboxGroupOther'],
    layout: 'checkboxgroupother',
     
    config: {
        otherBoxLabel: 'Other :',
        otherName: ''
    },
 
    // private
 
    textfield: null,
 
    initComponent: function() {
 
        // prevent checkboxes from submitting
        if (this.items) {
            for (var i=0; i<this.items.length; i++) {
                this.items[i].submitValue = false;
            }
        }
        this.callParent(arguments);
    },
 
    afterRender: function() {
 
        this.callParent(arguments);
        var tm = Ext.create('Ext.util.TextMetrics');
        var labelWidth = tm.getWidth(this.getOtherBoxLabel());
        var colspanEl = Ext.get(this.getEl().select('td [colspan=' + this.columns + ']').elements[0]);
 
        this.textfield = Ext.widget('textfield', {
            renderTo: colspanEl,
            labelWidth: labelWidth,
            margin: '10 0 0 0',
            width: '100%',
            fieldLabel: this.getOtherBoxLabel(),
            name: this.getOtherName(),
            listeners: {
                change: {
                    fn: this.otherBoxChange,
                    scope: this
                }
            }
        });
 
    },
 
    isDirty: function() {
 
        if (this.textfield.isDirty()) {
            return true;
        } else {
            return this.callParent(arguments);
        }
    },
 
    setReadOnly: function(readOnly) {
 
        this.textfield.setReadOnly(readOnly);
        this.callParent(arguments);
 
    },
 
    reset: function() {
        this.textfield.reset();
        this.callParent(arguments);
    },
 
    otherBoxChange: function(cmp) {
        this.validate();
    },
 
    resetOriginalValue: function() {
        this.textfield.resetOriginalValue();
        this.callParent(arguments);
    },
 
    getErrors: function() {
 
        var errors = [];
        if (!this.allowBlank && Ext.isEmpty(this.getChecked()) && Ext.isEmpty(this.textfield.getValue())) {
            errors.push(this.blankText);
        }
        return errors;
    },
 
 
    // setValue syntax:
    //
    // setValue({rb: ['1','2', '3', 'Other: Foo']})
    //
    //
 
    setValue: function(value) {
 
        var cb = null;
 
        if (!Ext.isArray(value)) {
            value = [value];
        }
 
        for (var i = 0; i < this.items.items.length; i++) {
 
            cb = this.items.items[i];
 
            if (Ext.Array.contains(value, cb.inputValue)) {
                cb.setValue(true);
            } else {
                cb.setValue(false);
            }
 
        }
 
        for (var i = 0; i < value.length; i++) {
            if (Ext.isString(value[i]) && value[i].indexOf('Other: ') == 0) {
                this.textfield.setValue(value[i].substr(7));
            }
        }
 
    },
 
 
    getValue: function() {
 
        var values = [];
 
        for (var i = 0; i < this.items.items.length; i++) {
            var cb = this.items.items[i];
 
            if (cb.isCheckbox && cb.getValue()) {
                values.push(cb.inputValue)
            }
        }
 
        if (this.textfield) {
            var tfValue = this.textfield.getValue();
            if (!Ext.isEmpty(tfValue)) {
                values.push('Other: ' + tfValue);
            }
        }
 
        if (values.length == 1) {
            values = values[0];
        }
 
        return values;
    },
 
 
    getSubmitData: function() {
 
        var val = new Object();
        val[this.name] = this.getValue();
 
        return val;
 
    },
 
    getModelData: function() {
 
        var val = new Object();
        val[this.name] = this.getValue();
 
        return val;
    }
});
What are you waiting for?
[Written by: Steve Drucker]
[Cross-promoted: Druck-I.T.

No comments:

Post a Comment

About Us

Fig Leaf Software is an award-winning team of imaginative designers, innovative developers, experienced instructors, and insightful strategists.

For over 20 years, we’ve helped a diverse range of clients...

Read More

Contact Us

202-797-7711

Fig Leaf Software

1400 16th Street NW
Suite 450
Washington, DC 20036

info@figleaf.com