最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically add TextAreas to a FormPanel on User Input with ExtJS - Stack Overflow

programmeradmin3浏览0评论

I have a FormPanel displaying a pretty basic form, essentially, it just contains a "Name" field, a "Description" field, and multiple "Rules" text areas. What I want is for the user to be able to type text into the first such Rule text area and have another empty TextField appear (when they start typing) for an additional rule.

Currently, I've got a function that should generate new TextAreas when called with a specified name (the newRulesField function), and a function to handle KeyPress events inside my text areas.

What I'm essentially looking for is how I can dynamically modify the number of TextAreas in the form.

Here's the code, in case it helps:

function handleRuleKeypress(a,b) {
  Ext.Msg.alert('kp');
}

function newRulesField(name) {
  var rulesField = new Ext.form.TextArea({ 
    fieldLabel:     'Rules',
    anchor:         '100%',
    name:            name,
    allowBlank:      false,
    grow:            false,
    enableKeyEvents: true,
    listeners: {
      keypress: handleRuleKeypress
    }
  });
  return rulesField;
}

function handleNewRuleSetClick(nodes) {

  var nameField = new Ext.form.TextField({
    fieldLabel: 'Name',
    name:       'ruleSetName',
    anchor:     '100%',
    allowBlank:  false,
    grow:        false
  });

  var descField = new Ext.form.TextField({
    fieldLabel: 'Description',
    name:       'ruleSetDescription',
    anchor:     '100%',
    allowBlank:  false,
    grow:        false
  });

  var form = new Ext.FormPanel({
    labelWidth:   75,
    defaultType: 'textfield',
    bodyStyle:   'padding:30px',
    id:          'newRuleSetPanel',
    name:        'newRuleSetPanel',
    title:       'New Rule Set',
    buttons: [{
      text:   'Save',
      id:     'saveBtn',
      hidden:  false,
      handler: function() {
        form.getForm().submit({
          url:     'server/new-rule-set',
          waitMsg: 'Saving...',
          success:  function(f,a) {
            Ext.Msg.alert('Success', 'It worked');
          },
          failure:  function(f,a) {
            Ext.Msg.alert('Warning', 'Error');
          }
        });
      }
    },{
      text:   'Cancel',
      id:     'cancelBtn',
      hidden:  false
    }]
  });

  form.add(nameField);
  form.add(descField);
  form.add(newRulesField('rules1'));

  this.add(form);
  this.doLayout();
}

I have a FormPanel displaying a pretty basic form, essentially, it just contains a "Name" field, a "Description" field, and multiple "Rules" text areas. What I want is for the user to be able to type text into the first such Rule text area and have another empty TextField appear (when they start typing) for an additional rule.

Currently, I've got a function that should generate new TextAreas when called with a specified name (the newRulesField function), and a function to handle KeyPress events inside my text areas.

What I'm essentially looking for is how I can dynamically modify the number of TextAreas in the form.

Here's the code, in case it helps:

function handleRuleKeypress(a,b) {
  Ext.Msg.alert('kp');
}

function newRulesField(name) {
  var rulesField = new Ext.form.TextArea({ 
    fieldLabel:     'Rules',
    anchor:         '100%',
    name:            name,
    allowBlank:      false,
    grow:            false,
    enableKeyEvents: true,
    listeners: {
      keypress: handleRuleKeypress
    }
  });
  return rulesField;
}

function handleNewRuleSetClick(nodes) {

  var nameField = new Ext.form.TextField({
    fieldLabel: 'Name',
    name:       'ruleSetName',
    anchor:     '100%',
    allowBlank:  false,
    grow:        false
  });

  var descField = new Ext.form.TextField({
    fieldLabel: 'Description',
    name:       'ruleSetDescription',
    anchor:     '100%',
    allowBlank:  false,
    grow:        false
  });

  var form = new Ext.FormPanel({
    labelWidth:   75,
    defaultType: 'textfield',
    bodyStyle:   'padding:30px',
    id:          'newRuleSetPanel',
    name:        'newRuleSetPanel',
    title:       'New Rule Set',
    buttons: [{
      text:   'Save',
      id:     'saveBtn',
      hidden:  false,
      handler: function() {
        form.getForm().submit({
          url:     'server/new-rule-set',
          waitMsg: 'Saving...',
          success:  function(f,a) {
            Ext.Msg.alert('Success', 'It worked');
          },
          failure:  function(f,a) {
            Ext.Msg.alert('Warning', 'Error');
          }
        });
      }
    },{
      text:   'Cancel',
      id:     'cancelBtn',
      hidden:  false
    }]
  });

  form.add(nameField);
  form.add(descField);
  form.add(newRulesField('rules1'));

  this.add(form);
  this.doLayout();
}
Share Improve this question asked Jun 29, 2009 at 22:56 ThorThor
Add a ment  | 

1 Answer 1

Reset to default 4

you are very close to doing this already - just call your newRulesField function from your handleRuleKeypress function. something like this:

function handleNewRuleSetClick(nodes) {

    var nameField = new Ext.form.TextField({
        fieldLabel: 'Name',
        name: 'ruleSetName',
        anchor: '100%',
        allowBlank: false,
        grow: false
    });

    var descField = new Ext.form.TextField({
        fieldLabel: 'Description',
        name: 'ruleSetDescription',
        anchor: '100%',
        allowBlank: false,
        grow: false
    });

    var form = new Ext.FormPanel({
        labelWidth: 75,
        defaultType: 'textfield',
        bodyStyle: 'padding:30px',
        id: 'newRuleSetPanel',
        name: 'newRuleSetPanel',
        title: 'New Rule Set',
        buttons: [{
            text: 'Save',
            id: 'saveBtn',
            hidden: false,
            handler: function() {
                form.getForm().submit({
                    url: 'server/new-rule-set',
                    waitMsg: 'Saving...',
                    success: function(f, a) {
                        Ext.Msg.alert('Success', 'It worked');
                    },
                    failure: function(f, a) {
                        Ext.Msg.alert('Warning', 'Error');
                    }
                });
            }
        }, {
            text: 'Cancel',
            id: 'cancelBtn',
            hidden: false
}]
        });

        function handleRuleKeypress(a, b) {
            form.add(newRulesField('rules' + Ext.id()));
        }

        function newRulesField(name) {
            var rulesField = new Ext.form.TextArea({
                fieldLabel: 'Rules',
                anchor: '100%',
                name: name,
                allowBlank: false,
                grow: false,
                enableKeyEvents: true,
                listeners: {
                    keypress: handleRuleKeypress
                }
            });
            return rulesField;
        }



        form.add(nameField);
        form.add(descField);
        form.add(newRulesField('rules1'));

        this.add(form);
        this.doLayout();
    }

you will want additional logic for checking if you have already added the new text area (or you will get a new one for each key press), perhaps removing the additional text area if the latest rule is emptied later on, and fixing the textarea id's a little prettier (all this is your handleRuleKeypress func).

My general thoughts on your application design is to keep the FormPanel as a class member somewhere instead of a private function member, this will make it easier to access whenever it es to modifying it after creation - stacking functions like this is not very pretty nor readable.

发布评论

评论列表(0)

  1. 暂无评论