Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data- attributes to ExtJs rendered html?

Using ExtJs 4.1.

I'm creating a panel (for example) and I would like that the generated html includes one or more "data-" attributes (for example: data-intro="some text" data-step="1")

How can this be done?

like image 318
pvieira Avatar asked Apr 11 '13 22:04

pvieira


People also ask

How add data attribute in HTML?

Adding a data attribute is easy. Any HTML element can have any number of data attributes added to its opening tag. We simply type data- followed by the name of our attribute into the element's opening tag alongside any other attributes we're already using.

How do you set data attribute using JS?

In order to create a new data attribute in JavaScript we just need to add a new property to the dataset object with a value. This will update the dataset object and our HTML which means our HTML will look like this.

What is data attribute in JavaScript?

The data-* attribute gives us the ability to embed custom data attributes on all HTML elements. The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).


1 Answers

After the component has rendered, you could apply the attributes to the top level element representing the component

Example:

var panel = Ext.create('Ext.panel.Panel',{
    title: 'Test',
    width: 500,
    height: 200,
    renderTo: Ext.getBody(),
    listeners: {
        afterrender: function(cmp) {
            cmp.getEl().set({
                "data-intro": 'some text',
                "data-step": 1
            });
        }
    }
});

panel.show();
like image 183
James Avatar answered Oct 18 '22 01:10

James