PDA

View Full Version : Extjs4 - TextArea



Simi Thomas
7 Nov 2012, 2:20 AM
Extjs4 TextArea
How to set the cursor position

droessner
7 Nov 2012, 6:16 AM
Use the selectText method and specify the from and to value as the position where you want to put the cursor.

tvanzoelen
7 Nov 2012, 6:22 AM
Found that somewhere one the forum and it work pretty well. Just extend the text field with the new functions


// helper functions for caret positioning in HTML text fields
// http://www.sencha.com/forum/showthread.php?95486#post609639
Ext.override(Ext.form.field.Text, {
setCaretPosition: function(pos, dir) {
var el = this.inputEl.dom;
if (typeof (el.selectionStart) === "number") {
el.focus();

if (el.selectionStart == 0 && el.selectionEnd != el.selectionStart && el.selectionEnd == el.value.length) {
if (dir == "left") {
el.setSelectionRange(el.selectionEnd, el.selectionEnd);
}

if (dir == "right") {
el.setSelectionRange(pos, pos);
}
}
else {
el.setSelectionRange(pos, pos);
}
} else if (el.createTextRange) {
var range = el.createTextRange();
range.move("character", pos);
range.select();
} else {
throw 'setCaretPosition() not supported';
}
},

getCaretPosition: function() {
var el = this.inputEl.dom;
if (typeof (el.selectionStart) === "number") {
return el.selectionStart;
} else if (document.selection && el.createTextRange) {
var range = document.selection.createRange();
range.collapse(true);
range.moveStart("character", -el.value.length);
return range.text.length;
} else {
throw 'getCaretPosition() not supported';
}
}
});

Simi Thomas
7 Nov 2012, 8:53 PM
How we can insert text toa specified cursor position

tvanzoelen
8 Nov 2012, 12:01 AM
Well thats a new question but, you now have a method to get the carretPosition and set the caretPosition. Just get the carret position split the string on that index and concat your text in the middle.