Dustin Graham
30 Sep 2009, 10:37 AM
Basically, I am wondering how to prepend a message to a displayfield.
I see/found an example on append.
displayField.append('<p>Something</p>');
It would be nice to have displayField.prepend('<p>Sweet</p>');
But, I tried various combinations of displayField.el.dom...
displayField.el.dom.insertBefore('<p>Test</p>', displayField.el.dom.firstChild);
Fail
Or...
displayField.el.dom.insertBefore(document.createElement('p'), displayField.el.dom.firstChild);
Failt... no wait... Success! But wait, it's just a <p> tag... no text.
Eventually I fugred something out that works...
newP = document.createElement('p');
newI = document.createElement('i');
newT = document.createTextNode('test');
newI.appendChild(newT);
newP.appendChild(newI);
displayField.el.dom.insertBefore(newP, displayField.el.dom.firstChild);
But, it was nasty, so I thought maybe the DOM Helper could help
So I tried
newP = {tag: 'p', children: [{ tag: 'i', html: 'test' }] }
Ext.DomHelper.insertBefore(newP, displayField.el.dom.firstChild);
Still fail...
Finally after a lot of testing, crashing, testing, crashing... reading the docs, googling a little on DOM insert/append/create... reading the docs some more.
Once I figured it out, it seems SOO easy and I feel pretty dumb. But, because I spent over 30 minutes trying to get this to work, I figure it would be nice to post this in case someone googles exactly what I googled... "How to prepend string to displayfield?" they might just find this post and save themselves 30 minutes.
Here's the final solution that works pretty nicely...
Ext.DomHelper.insertHtml('afterBegin', displayField.el.dom, '<p><i>Yay!</i></p>');
*Sigh* and this is the simple stuff. At least I got the complicated stuff done already. :D
Cheers.
-Dustin
I see/found an example on append.
displayField.append('<p>Something</p>');
It would be nice to have displayField.prepend('<p>Sweet</p>');
But, I tried various combinations of displayField.el.dom...
displayField.el.dom.insertBefore('<p>Test</p>', displayField.el.dom.firstChild);
Fail
Or...
displayField.el.dom.insertBefore(document.createElement('p'), displayField.el.dom.firstChild);
Failt... no wait... Success! But wait, it's just a <p> tag... no text.
Eventually I fugred something out that works...
newP = document.createElement('p');
newI = document.createElement('i');
newT = document.createTextNode('test');
newI.appendChild(newT);
newP.appendChild(newI);
displayField.el.dom.insertBefore(newP, displayField.el.dom.firstChild);
But, it was nasty, so I thought maybe the DOM Helper could help
So I tried
newP = {tag: 'p', children: [{ tag: 'i', html: 'test' }] }
Ext.DomHelper.insertBefore(newP, displayField.el.dom.firstChild);
Still fail...
Finally after a lot of testing, crashing, testing, crashing... reading the docs, googling a little on DOM insert/append/create... reading the docs some more.
Once I figured it out, it seems SOO easy and I feel pretty dumb. But, because I spent over 30 minutes trying to get this to work, I figure it would be nice to post this in case someone googles exactly what I googled... "How to prepend string to displayfield?" they might just find this post and save themselves 30 minutes.
Here's the final solution that works pretty nicely...
Ext.DomHelper.insertHtml('afterBegin', displayField.el.dom, '<p><i>Yay!</i></p>');
*Sigh* and this is the simple stuff. At least I got the complicated stuff done already. :D
Cheers.
-Dustin