Results 1 to 9 of 9

Thread: Newbie - How to get/set text for label

  1. #1
    Sencha User
    Join Date
    Nov 2011
    Posts
    5

    Default Answered: Newbie - How to get/set text for label

    Hi,

    I am a new to ext4js. I have problem of setting/getting text for item xtype:'label'. I would like to do somethig like grid-form example but instead using label. Some sample code.
    Code:
            var locationList = win.child('gridpanel').getSelectionModel();
    win.child('label').setText('Hello'); // this work but need to update on specific label
    
            locationList.on('selectionchange', function(sm, selectedRecord) {
                if (selectedRecord.length) {
                   *** need to update label text here ***
                   // alert("XX");
                   // Ext.Element.getById('locationID').setText(selectedRecord[0].data.id);
                    //alert(selectedRecord[0].data.id);
                  //  alert(selectedRecord[1].data.description);
                }
            });
    Please help...
    Last edited by skirtle; 3 Jan 2012 at 10:30 PM. Reason: CODE not QUOTE

  2. It would be something like this:

    Code:
    form.child('label[id=label2]').setText('Hello')
    That isn't much better though as it still relies on a static id with all the potential problems that can cause.

    One trick you see used sometimes is to add an arbitrary property to a component's config purely for CQ purposes. Like this:

    Code:
    {
        xtype: 'label',
        someMadeUpPropertyName: 'nameLabel',
        ...
    }
    Then you can do this:

    Code:
    form.child('label[someMadeUpPropertyName=nameLabel]').setText('Hello')

  3. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,791
    Answers
    585

    Default

    Setting the text is just a call to setText:

    http://docs.sencha.com/ext-js/4-0/#!...method-setText

    There is no getter but you can get the current text via either the text or the html property of the label, depending on whether or not the text was HTML encoded last time it was set.

    Take a look at the source code, there isn't much to it:

    http://docs.sencha.com/ext-js/4-0/source/Label2.html

    I wonder whether what you're actually asking is how to get a reference to the label so that you can call setText? That's a much more difficult question to answer as there's not enough information in your question. It's just like in any other OO environment, the question 'how do I get a reference to my object?' is a pretty open-ended question. Component queries will probably be part of it.

  4. #3
    Sencha User
    Join Date
    Nov 2011
    Posts
    5

    Default

    Thanks a lot for the reply. But, how to do setText for specific label item as code sample below :

    {
    xtype: 'label',
    id : 'label1'
    height: 20,
    width: 20,
    text: ':',
    x: 100,
    y: 180
    },
    {
    xtype: 'label',
    height: 20,
    width: 150,
    id : 'label2'
    text: 'None',
    x: 120,
    y: 180
    }
    How to setText for label with id = 'label2'? Thanks a lot.

  5. #4
    Sencha User
    Join Date
    Nov 2011
    Posts
    5

    Default

    Hi. I got it. I use

    Code:
    Ext.getCmp('label2').setText('Hello');
    I works. Thanks a lot.

  6. #5
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,791
    Answers
    585

    Default

    Please use [CODE] tags for posting code, not [QUOTE] tags.

    The use of static ids with Ext.getCmp is highly discouraged. Obtaining a reference to the relevant label is dependent upon its wider role in the app and the context from which you need to obtain it. From what little information we have it's impossible to give a good recommendation but as I've already mentioned it seems likely that component queries will form a part of it.

    This isn't so much an ExtJS question as one about the design of your application and the dependencies between your objects. The use of Ext.getCmp is effectively resorting to a global variable.

  7. #6
    Sencha User
    Join Date
    Nov 2011
    Posts
    5

    Default

    Thanks Skirtle. At least for now, it solves my problem. But as I go along with Ext4JS developlement I definitely find an efficient way of doing ext way. I have tried using something like :

    Code:
    form.child('label').Id('label2').setText('Hello')
    with no luck.

    Thanks anyway.

  8. #7
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,791
    Answers
    585

    Default

    It would be something like this:

    Code:
    form.child('label[id=label2]').setText('Hello')
    That isn't much better though as it still relies on a static id with all the potential problems that can cause.

    One trick you see used sometimes is to add an arbitrary property to a component's config purely for CQ purposes. Like this:

    Code:
    {
        xtype: 'label',
        someMadeUpPropertyName: 'nameLabel',
        ...
    }
    Then you can do this:

    Code:
    form.child('label[someMadeUpPropertyName=nameLabel]').setText('Hello')

  9. #8
    Sencha User
    Join Date
    Nov 2011
    Posts
    5

    Default

    It works as per your suggestion which is much better than using global and static id. Thanks lot for your help.

  10. #9
    Sencha Premium Member
    Join Date
    Aug 2011
    Location
    Bangalore, INDIA
    Posts
    51

    Default

    I am having the same problem. I want to change the Header of a column.
    I tried something like:

    Code:
    grid.getCmp('value').setText("Yahooooo!!");
    where value is the id of a column.

    but it throws an error, object doesn't support this property or method.

    Any suggestions ??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •