One to many data binding - extjs

I have a case where I want to get the value from one input (main input field in fiddle) and bind it to other input fields.
https://fiddle.sencha.com/#view/editor&fiddle/2001
Due to fairly complex architecture, these input fields are in different components. I was wondering what would be the best way to bind value from the main input field to all other fields, is there a similar example I could take a look at?
Thx.

As you said, the input fields are from different components, you may not want to create viewModel for all the components.
I have created a listener and got the references of all the corresponding fields you want to set value and setting the value in change event of main input field.
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.create('Ext.panel.Panel', {
title: 'Main Input field',
width: 200,
items:[{
xtype: 'textfield',
listeners:{
change:function(cmp, newValue, oldValue, eOpts){
if(newValue){
var inputFields = Ext.ComponentQuery.query('textfield[type=inputField]');
for(var k in inputFields){
inputFields[k].setValue(newValue);
}
}
}
}
}],
renderTo: Ext.getBody()
});
Ext.create('Ext.panel.Panel', {
title: 'Input field 1',
width: 200,
items:[{
xtype: 'textfield',
type:'inputField'
}],
renderTo: Ext.getBody()
});
Ext.create('Ext.panel.Panel', {
title: 'Input field 2',
width: 200,
items:[{
xtype: 'textfield',
type:'inputField'
}],
renderTo: Ext.getBody()
});
Ext.create('Ext.panel.Panel', {
title: 'Input field 3',
width: 200,
items:[{
xtype: 'textfield',
type:'inputField'
}],
renderTo: Ext.getBody()
});
}
});
I have tested in the shared fiddle, its working fine.

Related

How to dynamically set fieldLabel value in ExtJS?

I want to achieve a situation where my fieldLabel values come from store. Store has the values I need, but I can't figure out how to set these values to fieldLabel.
Let's say I have some block:
items: [{
name: 'someName',
fieldLabel: 'someFieldLabel'
}]
someName in name field comes from store and works as expected.
someFieldLabel value in fieldLabel field is present in store but it shows literally 'someFieldLabel', not the value from store.
Any suggestions how to make this work dynamically with values from store?
Below is the code from a fiddle on how to change the fieldLabel dynamically.
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.create('Ext.panel.Panel', {
title: 'Dynamic Set Field Label',
layout: 'vbox',
renderTo: Ext.getBody(),
items: [{
xtype: 'textfield',
itemId: 'lblItem',
fieldLabel: 'Testing',
margin: '0 0 20 0'
}, {
xtype: 'button',
text: 'Set Field Label',
handler: function (btn) {
// alert('button presssed');
Ext.ComponentQuery.query('#lblItem')[0].setFieldLabel('New Label');
}
}]
})
}
});
You can see it working here.
Note: Using Ext.ComponentQuery it will return an Array of objects meeting the query requirements. You need to specify index to obtain a single object.
store.load({
callback: function (records) {
Ext.ComponentQuery.query('#lblItem')
}
})
items: [{
itemId: 'someField'
name: 'someName',
fieldLabel: 'someFieldLabel'
}];
on store load function
var somefield = Ext.ComponentQuery.query('field[itemId=someField]');
somefield[0].setFieldLabel('dynamic_field_label');

How to use the onkeyup event to a TextField?

I want to use onkeyup event of text field, so we can count that character when enter the text.
enableKeyEvents: true
For key events to work, you need to explicitly specify it.
Working POC code:
Ext.onReady(function () {
Ext.create({
xtype: 'panel',
renderTo: Ext.getBody(),
items: [{
xtype: 'form',
height: 100,
items: [{
xtype: 'textfield',
fieldLabel: 'field label',
value: '1',
enableKeyEvents: true,
listeners: {
keyup: function() {
console.log("key Up called");
},
change: function() {
console.log('changed');
}
}
}]
}]
})
});
Working Fiddle: https://fiddle.sencha.com/#view/editor&fiddle/2cbd
You are almost there. In the doc field key events, it is written that key events get fired only if enableKeyEvents is set to true.
{
xtype: 'textfield',
fieldLabel: 'Address',
width: '100%',
enableKeyEvents : true,
listeners: {
keyup : function(obj) {
alert('test fire ' + obj.getValue());
}
}
},

How to optimize adding components to a view?

First of all, I have about 1000-1500 records and I need to show them in a grid in custom style. But, first row has to contain checkbox and a form(combo,textfield), which is below that checkbox.
Similar to:
row1: checkbox
combo, textfield, textfield
row2: checkbox
combo, textfield, textfield
...
So, I could not use grid for this approach(Because I have to use 'colum render function' to each row and it causes re-render whole grid again even if change one row).
That's why I tried to add each component to a panel dynamically. But the main problem is that whenever I add the components to the panel, it takes too long . For example, it takes 4.5 s for only 100 records. I can't even imagine how long would it take for 1500 records.
Please check this fiddle. You will understand the exact problem. please do not forget the check console.time:
https://fiddle.sencha.com/#fiddle/13g0
And sample codes:
console.time('FakeGrid');
Ext.suspendLayouts();
var fakeGrid = Ext.create('Ext.panel.Panel', {
title: 'Fake grid Panel',
renderTo: Ext.getBody(),
});
var records = [];
for(var i=0; i < 10; i++) {
records.push({
id: 'Check'+ i,
reg: 'Reg' + i
})
}
Ext.each(records, function(rec) {
var regionPanel = {
xtype: 'fieldset',
collapsible: true,
border: false,
title: rec.reg,
layout: 'vbox',
items: []
};
Ext.each(records, function(rec) {
var hBoxPanel = {
xtype: 'panel',
layout: 'hbox',
items: [{
xtype: 'checkbox',
boxLabel: rec.id
}]
};
var fakeSubPanel = Ext.create('Ext.panel.Panel', {
layout: 'hbox',
items: [{
xtype: 'combobox',
fieldLabel: 'Combo'
}, {
xtype: 'textfield',
fieldLabel: 'field1'
}, {
xtype: 'textfield',
fieldLabel: 'field2'
}]
});
var sitePanel = {
xtype: 'panel',
layout: 'vbox',
//margin: '5 0 0 31',
items: [hBoxPanel, fakeSubPanel]
};
regionPanel.items.push(sitePanel);
}); //Ext.each
fakeGrid.add(regionPanel);
}); //Ext.each
Ext.resumeLayouts(true);
console.timeEnd('FakeGrid');
If you have any suggestion, please enlight me with your ideas. Thank you so much!

How to compare two text field values using ext js

I have small query,i have two text fields.here my query is if i enter a value in second text field,If the value is greater than first text field it's show pop up message box,
how can i implement this requirement.Any one can u please help to me.
Here is an example:
working fiddle here : https://fiddle.sencha.com/#fiddle/osl
Ext.create('Ext.form.Panel', {
title: 'Check Fields',
width: 300,
bodyPadding: 10,
renderTo: Ext.getBody(),
items: [{
xtype: 'textfield',
name: 'name',
fieldLabel: 'Number1',
xtype: 'numberfield',
id: 'num1'
}, {
xtype: 'textfield',
name: 'text2',
fieldLabel: 'Number2',
xtype: 'numberfield',
listeners:{
change: function( me, newValue, oldValue, eOpts ){
if (Ext.getCmp("num1").getValue() > newValue){
Ext.Msg.alert('Status', 'Field 1 greater than Field2');
}
}
}
}],
});
There are many ways to implement that. One possible way would be:
Ext.widget({
xtype: 'container',
defaults: {
xtype: 'textfield'
},
items: [
{
fieldLabel: 'Field 1',
name: 'f1'
},
{
fieldLabel: 'Field 2',
listeners: {
'change': function() {
if (this.getValue() > this.ownerCt.down('textfield[name="f1"]').getValue()) {
Ext.Msg.alert('Attention', 'The value of the second field is greater than the value of the first one');
}
}
}
}
],
renderTo: Ext.getBody()
});
(I'm using jquery for example)
first text field id=A
second text field id=B
var a=$('#A');
var b=$('#B');
$("#B").on(function() {
if(a>b){
alert("B number is greater than A number");
}
});

extJS events/listeners for newb to js/extjs

I'm trying to learn a little bit about manipulating UI elements in forms. I have 2 graphical elements. I have a comboBox that has a list of arbitrary things and I have a very basic form panel.
what I'm trying to do is pass the contents of a clicked item in the combobox to the input box on the form.
Click on 'cat" in combobox then the form will say animal: cat ... I've been trying to add listeners and using the .on approaches to do this but I can't seem to get it. Any advice or hints would be much appreciated.
Ext.onReady(function () {
// The data store containing the list of cool stuffz
var animals = Ext.create('Ext.data.Store', {
fields: ['id', 'name'],
data: [{
"id": 'cat',
"name": "mycat"
}, {
'id' : 'dog',
"name": "mydog"
}, {
'id' : 'sbBarGirls',
"name": "heartbreaking-man-eating-deathclaw"
}
//...
]
});
// Create the combo box, attached to the states data store
Ext.create('Ext.form.ComboBox', {
id: 'combo',
width: 600,
fieldLabel: 'Choose animal',
emptyText: 'dont select the last one',
store: animals,
queryMode: 'local',
displayField: 'name',
valueField: 'id',
renderTo: Ext.getBody()
});
});
//non related code here..
Ext.onReady(function(){
Ext.create('Ext.form.Panel', {
title: 'Animal sanctuary, one animal per location ',
width: 300,
bodyPadding: 10,
style: 'background-color: #Fdd;',
renderTo: Ext.getBody(),
items: [{
xtype: 'textfield',
fieldLabel: 'animal:',
name: 'myanimal'
}]
});
});
what i was trying to do was use the dom event mousedown on one of the comboselections trigger a listener but i couldn't seem to get it to work, my apologies if the event/listener tags are incorrect.
So you need describe id for the text field, e.g.:
{
id: 'wild_but_very_good_animal',
xtype: 'textfield',
fieldLabel: 'animal:',
name: 'myanimal'
}
And add listener for the combo:
Ext.create('Ext.form.ComboBox', {
id: 'combo',
width: 600,
fieldLabel: 'Choose animal',
emptyText: 'dont select the last one',
store: animals,
queryMode: 'local',
displayField: 'name',
valueField: 'id',
renderTo: Ext.getBody(),
listeners: {
'change': function(field, selectedValue) {
Ext.getCmp('wild_but_very_good_animal').setValue(selectedValue);
}
}
});

Resources