3

I am using a extjs field container for my application where I have details, funds, dependants sections in the form. In the funds and dependants section I have a button 'add another' which is intended to add a new line item every time when button is clicked.

This is the form i have created http://jsbin.com/evevod/4/edit

I am new to extjs4. Can anyone please help me how to create a item dynamically onclick?

2 Answers 2

10

Below is example handler.

handler: function() {
    var container = this.up('fieldset');
    var config = Ext.apply({}, container.initialConfig.items[0]);
    config.fieldLabel = container.items.length + 1;
    container.add(config);
}

Basically it finds parent component which holds rows (which is fieldset), access initialConfig property, finds first line config in it (items[0]), makes shallow copy of config (Ext.apply) and adds it to container (container.add).

Working sample: http://jsbin.com/evevod/6/edit#preview

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, exactly of what i am looking for.
Hi, i have implemented the same in the application but struggling with one issue. can we by default assign ID's to the fields of dependant & funds and on hitting 'add another' button (dependant & fund)item a new id should be dynamically generated for all the dependant & fund fields. e.g. we have title1, fname1, lname1 for the initial set of fields and we want to have title2, fname2, lname2, title3, fname3, lname3 etc etc as the IDs of the additional fields
You can change names directly in handler or you can create some function which will create config for new row. Eg here I've created getDepConfig function.
1

We can change the name of fields like this:

var container = this.up('fieldset'); 
var config = Ext.apply({}, container.initialConfig.items[0]);
config.fieldLabel = container.items.length+1;
config.items[0].name = 'fname' + config.fieldLabel;
config.items[1].name = 'lname' + config.fieldLabel;
config.items[2].name = 'title' + config.fieldLabel;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.