Sencha ExtJS Grid With textareafield not accepting new lines - extjs

The RowEditor grid plugin captures the enter key as part of its processing, but I want my user to be able to edit the text in a "textarea" field. When they press "enter" to enter a newline the RowEditor thinks they want to perform an update.
A possible fix was suggested that look like the following:
listeners: {
afterrender: function() {
this.inputEl.swallowEvent([
'keypress',
'keydown'
]);
}
}
When I try and use this code I get 'inputEl' is underfined. I have also tried:
var el = Ext.get("txtTest");
el.inputEl.swallowEvent([
'keypress',
'keydown'
]);
Yet I receive the same error. I am guessing the key lies with this swallowEvent after the textarea renders but I have no idea how to implement it.
Here is my code:
var newGrid = Ext.create('Ext.grid.Panel', {
renderTo : Ext.getBody(),
title : 'Grid',
width : 1000,
height : 300,
plugins : [rowEditing],
tbar: [{
text: 'New Entry',
iconCls: 'employee-add',
height: 40,
width: 115,
handler : function() { addNewRow(newGrid); }
}],
store : store,
columns:
{
items:
[
{ id: 'txtTest', itemId: 'txtTest', text: 'Activity', dataIndex: 'Activity', editor: 'textareafield', grow: true, growMax: 300, listeners:
{
afterrender: function()
{
alert(this.inputEl);
//this.inputEl.swallowEvent(['keypress','keydown']);
}
}
}
]
}
});

Deriving your own textareafield from the default one will work:
Ext.define('EditorTextArea',{
extend:'Ext.form.field.TextArea',
xtype:'editortextarea',
listeners:{
afterrender:function(textareafield){
textareafield.inputEl.swallowEvent(['keypress','keydown']);
}
}
})
and then editor:'editortextarea'.
Alternatively you can try:
editor: Ext.create('Ext.form.field.TextArea', {
listeners: {
afterrender: function(textareafield) {
textareafield.inputEl.swallowEvent(['keypress', 'keydown']);
}
}
})

Related

How can I add an object that is added with Ext.create to my Viewport?

I'm new using Ext JS and I need to add a button in my Viewport.
I have tried adding it as an item but it doesn't work when I click on it:
items: [ {
xtype: 'datepicker',
width: 211
},
{
xtype: 'datepicker',
width: 211
},
{
xtype: 'button',
text: 'Search',
width: 211
},
],
So then, in the official documentation I have found another way to add it:
Ext.create('Ext.Button', {
text : 'Button',
renderTo : Ext.getBody(),
listeners: {
click: function() {
// this == the button, as we are in the local scope
this.setText('I was clicked!');
},
mouseover: function() {
// set a new config which says we moused over, if not already set
if (!this.mousedOver) {
this.mousedOver = true;
alert('You moused over a button!\n\nI wont do this again.');
}
}
}
});
But I want it in the west region I defined in my Viewport and I have no idea about how to achieve this since I'm completely new at Ext JS.
My code:
function init() {
Ext.application({
name: 'MyApp',
launch: function() {
Ext.create('Ext.data.Store', {
storeId:'prices',
fields:['name', 'priceNight', 'totalPrice', 'Check-in', 'Check-out'],
data:{'items':[
]},
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'items'
}
}
});
Ext.create('Ext.container.Viewport', {
layout: 'border',
items: [{
title: 'West',
region: 'west',
margins: '0 5 0 5',
flex: .3,
collapsible: true,
split: true,
titleCollapse: true,
items: [
{
xtype: 'datepicker',
width: 211
},
{
xtype: 'datepicker',
width: 211
},
{
//I want the button here.
},
],
}, {
title: 'Center',
region: 'center',
collapsible: false,
items: {
// I want to add it just there
xtype: 'grid',
store: Ext.data.StoreManager.lookup('prices'),
columns: [
],
}
}]
});
}
});
}
I would like the button here:
Any ideas?
Thank you in advance!
I have just figured out how to do it! I'm going to post here what I did so maybe it will result helpful for some people:
It's just simple as invocating it using a variable.
Button code:
var button = Ext.create('Ext.Button', {
text : 'Button',
renderTo : Ext.getBody(),
listeners: {
click: function() {
// this == the button, as we are in the local scope
this.setText('I was clicked!');
},
mouseover: function() {
// set a new config which says we moused over, if not already set
if (!this.mousedOver) {
this.mousedOver = true;
alert('You moused over a button!\n\nI wont do this again.');
}
}
}
});
In your viewport you just have to add it as an item:
...
items: [
{
items: button //button is the variable that we defined when we created the button
}
]
...
Final result:
I hope this will help someone who is having the same question as me :)

Pass data between views in ExtJS

I am reading data from store and populating main girdView. When any of the row is clicked, a windows appear and I want to fill the fieldset in that window with the same record which is clicked.
Can anyone tell me how to do it?
My popup window is like:
{
extend: 'Ext.window.Window',
height: 500,
width: 1500,
minWidth :1500,
maxWidth :1500,
layout: 'fit',
controller: 'PopUpWindowController',
initComponent: function () {
var me = this;
//console.log(me.myExtraParams);
var Store = Ext.getStore('mainStore');
Ext.applyIf(me, {
/* items: [{
xtype: 'form',
bodyPadding: 10,
region: 'center'
}]*/
});
me.callParent(arguments);
},
items: [
{
xtype: 'panel',
title : 'Projektierung',
bodyStyle : 'padding:5px',
width : 1880,
autoHeight : true,
items:
[
{
xtype : 'kopfdatenView', // Have to populate this view
}
]
}]}
and in Main view I am calling it as:
{
region: 'center',
xtype: 'gridpanel',
listeners : {
itemdblclick: function(dv, record, item, index, e) {
var extra = record.data;
win = Ext.create('App.view.PopUpWindow');
console.log(win.myExtraParams);
win.on('show', function(win) {
win.setTitle(record.get('ID'));
});
win.show();
}
},
columns: [
****
],
store: 'mainStore',
height: 100,
width: 400,
}
I want to populate fieldset in kopfdatenView. Kindly help
You directly pass your record to your window on creation :
win = Ext.create('App.view.PopUpWindow', {
myRecord: record
});
And then inside the window's initComponent function (before the callParent) :
this.title = this.myRecord.get('ID');
You can do something like this:
win = Ext.create('App.view.PopUpWindow', {
params: record
});
Then, in your PopUpWindowController you can retrieve 'params' in the render event (for example).

ExtJS Grid Delete Row - Getting Selected Row

I am getting the following error when trying to get the selected row:
Uncaught TypeError: Object #<HTMLDivElement> has no method 'getView'
How do get the current selected row when I can't get the View and hence the SelectionModel?
My View Code:
Ext.define('MyLodge.view.content.MemberGrid', {
extend: 'Ext.grid.Panel',
alias: 'widget.membergrid',
initComponent: function(){
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');
var store = Ext.create('MyLodge.store.Members');
Ext.apply(this, {
height: this.height,
plugins: [rowEditing],
store: store,
stripeRows: true,
columnLines: true,
columns: [{
id :'id',
text: 'ID',
width: 40,
sortable: true,
dataIndex: 'id'
},{
text : 'Name',
flex: 1,
sortable : true,
dataIndex: 'name',
field: {
xtype: 'textfield'
}
},{
text : 'E-Mail',
width : 150,
sortable : true,
dataIndex: 'email',
field: {
xtype: 'textfield'
}
},{
text : 'Href',
width : 200,
editable: false,
sortable : true,
dataIndex: 'href'
}],
dockedItems: [{
xtype: 'toolbar',
items: [{
text: 'Add',
iconCls: 'icon-add',
handler: function(){
// empty record
store.insert(0, new MyLodge.model.Member());
rowEditing.startEdit(0, 0);
}
}, {
text: 'Delete',
iconCls: 'icon-delete',
handler: function(){
var selection = grid.getView().getSelectionModel().getSelection()[0];
if (selection) {
store.remove(selection);
}
}
},'-',{
text: 'Save',
iconCls: 'icon-save',
handler: function(){
store.sync({
success: function(response){
store.load()
}
});
}
},{
text: 'Refresh',
handler: function(){
store.load();
}
}]
}]
});
this.callParent(arguments);
}
});
One option would be adding the scope to the closure as a variable:
initComponent: function () {
var me = this;
.
.
So in your handler you can use me to refer to the grid:
{
text: 'Delete',
iconCls: 'icon - delete ',
handler: function () {
var selection = me.getView().getSelectionModel().getSelection()[0];
if (selection) {
store.remove(selection);
}
}
}
Working example: http://jsfiddle.net/Sn4fC/
A second option can be setting a click listener instead of the handler and specifying the scope:
{
text: 'Delete',
iconCls: 'icon - delete ',
listeners: {
click: {
scope: this,
fn: function () {
var selection = this.getView().getSelectionModel().getSelection()[0];
if (selection) {
store.remove(selection);
}
}
}
}
}
Working example: http://jsfiddle.net/XyBbF/
The issue is that 'grid' is not available in the handler function scope or it is not the one what you actually expected it to be.
handler: function(){
//.... keep the debug point here in browser developer tool and verify the value of grid in console. It is definitely not an instance of the grid u expected hence it wont have getView() method.
var selection = grid.getView().getSelectionModel().getSelection()[0];
}
Try getting the reference of grid and use it in handler as shown below:
Ext.define('MyLodge.view.content.MemberGrid', {
extend: 'Ext.grid.Panel',
alias: 'widget.membergrid',
id: 'membergrid',
......
handler: function(){
var grid = Ext.getCmp('membergrid'); // where 'membergrid' is the id defined in grid config
var selection = grid.getView()......

Adding dynamic fields on click of button in extjs 4

I want to add a field dynamically when click on button in extjs 4.What I have done is added a onchange function on add button and write the adding textfield logic in that function.
Here is my code.
Ext.define('AM.view.user.Edit', {
extend: 'Ext.window.Window',
alias : 'widget.useredit',
requires: ['Ext.form.Panel'],
title : 'Edit User',
layout: 'fit',
autoShow: true,
height: 300,
width: 280,
id : 'mainPanel',
initComponent: function() {
this.items = [
{
xtype: 'form',
id: 'dyForm',
padding: '5 5 0 5',
border: false,
style: 'background-color: #fff;',
items: [
{
xtype: 'textfield',
name : 'name',
fieldLabel: 'Name'
},
{
xtype: 'textfield',
name : 'email',
fieldLabel: 'Email'
}
]
}
];
this.buttons = [
{
text: 'Save',
action: 'save'
},
{
text: 'Add',
scope: this,
handler: this.onChange
},
];
this.callParent(arguments);
},
onChange : function(){alert('test');
var form=Ext.getCmp("mainPanel");
form.items.add(Ext.create("Ext.form.field.Text", {fieldLabel:"First Name"}));
form.doLayout(true);
}
});
And I also want that on edit of this form I will load data to those new fields that i have newly created dynamically.
Change the onChange function to
onChange : function(btn){
// var form=Ext.getCmp("mainPanel");
var form = btn.up('window').down('form'); // this is a better approach
form.add(Ext.create("Ext.form.field.Text", {fieldLabel:"First Name"}));
}
should do it. You added the field directly to the collection where you should have use the supplied add method of the form component.
see JSFiddle

Extjs4, how to retrieve single textfield value?

var confirmWindow = Ext.create('Ext.window.Window', {
title: 'Selected Item List',
autoHeight: true,
width: 500,
layout: 'fit',
modal: true,
items: [{
xtype: 'panel',
bodyPadding : 5,
items: [{
xtype: 'textfield',
id : 'freightFee',
fieldLabel: 'Freight Fee ',
name : 'freight' // I need this value, when I click the button
}]
}],
bbar: ['->', {
xtype: 'buttongroup',
items: [{
text: "test",
handler: function () {
// I want to get the textfield value (freightFee)
var freightFee = Ext.getCmp('freightFee').getValue(); // error :Ext.getCmp('freightFee') is undefined
}
}]
}
});
I have a window like above, and I want to get the text inputbox value when I click the button.
I tried,
var freightFee = Ext.getCmp('freightFee').getValue();
but error message say,
Ext.getCmp('freightFee') is undefined
anybody know this?
thank you!
Don't use getCmp ever! It's very expensive and unnecessary. Check out up/down methods to locate elements parents/childrens http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Element-method-down
In your case something like that should work:
handler: function(button) {
var tf = button.up('window').down('#freightFee');
}

Resources