Add record to the store and display on the grid - extjs

I have AddRecord method, that should add the record to the store and display on the grid that record.
I've put the form inside the window, and that is fine.
I was looking at the documentation but I got lost a bit.
There is a fiddle created.
Here is the fiddle: https://fiddle.sencha.com/#view/editor&fiddle/2tuf
AddRecord: function (grid, rowId, record) {
Ext.create('Ext.window.Window', {
title: "Add Person",
height: 200,
width: 400,
closeAction: 'hide',
closable: true,
items: [{
xtype:'form',
defaultType: 'textfield',
layout: 'anchor',
items: [{
fieldLabel: 'First Name',
name: 'First Name',
type: 'String',
allowBlank: false
}, {
fieldLabel: 'Last Name',
name: 'Last Name',
type: 'String',
allowBlank: false
}, ],
buttons: [{
text: 'Add',
formBind: true,
disabled: true,
handler: function () {
var record = Ext.getStore().getAt(rowId);
var store = grid.getStore('store.Personal')
var form = this.up('form').grid.getStore();
if (form.isValid()) {
form.add({
success: function (record) {
var store = grid.getStore('store.Personal')
store.add(record);
},
})
}
},
}, {
text: 'Close', handler: function () {
this.up('window').close();
}
}],
}]
}).show();
}
});

You were almost there, You made a little confusion about grid.getStore(), as it just
Returns the store associated with this Panel.
and how you are getting your form values, I think you were trying to do something like this:
buttons: [{
text: 'Add',
formBind: true,
disabled: true,
handler: function(button) {
let formValues = this.up('form').getForm().getValues(); //get the form Values
let form = this.up('form').getForm(); //get the form itself
var store = grid.getStore(); //get Store of your grid
if (form.isValid()) {
//add the formvalues to store
store.add(formValues);
}
},
}]
I also made a Fiddle

Related

ExtJS: Issue with scope in class

I'm keep facing with a issue to choice exact component with scope. As you'll notice below I've created 2 different functions inside gridpanel. One of those creates a Ext.MessageBox for confirm. And other function creates a Ext.window.Window depends on button click of MessageBox.
The thing here is; It should destroy related component with cancel and no buttons. Both buttons always point to gridpanel because of var me = this state and destroys the gridpanel itself.
How can I point destroy method directly to related component?
Ext.define('MyApp.FooGrid', {
extend: 'Ext.grid.Panel',
reference: 'fooGrid',
getGridMenu: function () {
// Here is the 'Update' function; with right-click user being able to see `contextmenu`
var me = this;
var ret = [
{
text: 'Update',
listeners: {
click: me.onUpdate,
scope: me
}
}
];
return me.callParent().concat(ret);
},
onUpdate: function () {
var me = this,
gridRec = this.getSelectionModel().getSelection(); // Here being able to retrieve row data.
Ext.MessageBox.confirm(translations.confirm, translations.confirmChange, me.change, me);
return gridRec;
},
change: function (button) {
var me = this;
var selectedRec = me.onUpdate();
var selectedRecEmail = selectedRec[0].data.email; //Retrieves selected record's email with right-click action
if (button === "yes") {
return new Ext.window.Window({
alias: 'updateWin',
autoShow: true,
title: translations.update,
modal: true,
width: 350,
height: 200,
items: [
{
xtype: 'container',
height: 10
},
{
xtype: 'textfield',
width: 300,
readOnly: true,
value: selectedRecEmail //Display selected record email
},
{
xtype: 'textfield',
width: 300,
fieldLabel: translations.newPassword
}
],
dockedItems: [
{
xtype: 'toolbar',
dock: 'bottom',
items: [
{
xtype: 'tbfill'
},
{
xtype: 'button',
text: translations.cancel,
listeners: {
click: function () {
me.destroy(); // Here is the bug: When user clicks on this button; should destroy current window but it destroys 'gridpanel' itself
}
}
},
{
xtype: 'button',
text: translations.save,
listeners: {
click: function () {
console.log("I'll save you!");
}
}
}
]
}
]
});
} else {
console.log('this is no!');
me.destroy(); // Another bug raises through here: If user will click on No then 'messagebox' should destroy. This one is destroys the gridpanel as well.
}
}
});
How can I point destroy method directly to related component?
Firstly on confirmation box button's(No) click, you don't need to destroy it will automatically hide the box whenever you click into No.
And for update window instead of using me.destroy() you need to use directly button.up('window').destroy() so it will only destroy your update window not the grid.
And also you don't need to again call me.onUpdate() inside of change function otherwise it will again show the confirmation box. You can directly get selected record on the change function like this me.getSelection().
In this Fiddle, I have created a demo using your code and I have put my efforts to get result.
CODE SNIPPET
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.create('Ext.data.Store', {
storeId: 'demostore',
fields: ['name', 'email', 'phone'],
data: [{
name: 'Lisa',
email: 'lisa#simpsons.com',
phone: '555-111-1224'
}, {
name: 'Bart',
email: 'bart#simpsons.com',
phone: '555-222-1234'
}, {
name: 'Homer',
email: 'homer#simpsons.com',
phone: '555-222-1244'
}, {
name: 'Marge',
email: 'marge#simpsons.com',
phone: '555-222-1254'
}]
});
Ext.create('Ext.grid.Panel', {
title: 'Demo GRID',
store: 'demostore',
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
height: 200,
listeners: {
itemcontextmenu: function (grid, record, item, index, e, eOpts) {
e.stopEvent();
grid.up('grid').getGridMenu().showAt(e.getXY());
}
},
renderTo: Ext.getBody(),
getGridMenu: function () {
var me = this;
if (!me.contextMenu) {
me.contextMenu = Ext.create('Ext.menu.Menu', {
width: 200,
items: [{
text: 'Update',
handler: me.onUpdate,
scope: me
}]
});
}
return me.contextMenu;
},
onUpdate: function () {
var me = this;
Ext.MessageBox.confirm('Confirmation ', 'Are your sure ?', me.change, me);
},
change: function (button) {
var me = this,
selectedRecEmail = me.getSelection()[0].data.email; //Retrieves selected record's email with right-click action
if (button === "yes") {
return new Ext.window.Window({
autoShow: true,
title: 'Update',
modal: true,
width: 350,
height: 200,
items: [{
xtype: 'tbspacer',
height: 10
}, {
xtype: 'textfield',
width: 300,
readOnly: true,
value: selectedRecEmail //Display selected record email
}, {
xtype: 'textfield',
inputType:'password',
width: 300,
fieldLabel: 'New Password'
}],
dockedItems: [{
xtype: 'toolbar',
dock: 'bottom',
items: [{
xtype: 'tbfill'
}, {
xtype: 'button',
text: 'cancel',
listeners: {
click: function (btn) {
btn.up('window').destroy(); // Here is the bug: When user clicks on this button; should destroy current window but it destroys 'gridpanel' itself
}
}
}, {
xtype: 'button',
text: 'save',
listeners: {
click: function () {
console.log("I'll save you!");
}
}
}]
}]
});
}
}
});
}
});

extjs6 modern card layut

I am new in Extjs. I have a container with card layout with 3 sub views including a grid, a form for creating and a form for updating using route.
items: [
{xtype: 'feature-grid',id:'feature-grid},
{xtype: 'create-form'},
{xtype: 'update-form'}
],
it works well at the first time but when I change the route and switch to this route again this error appears:
Uncaught Error: DOM element with id feature-grid in Element cache is not the same as element in the DOM. Make sure to clean up Element instances using destroy()
and when I remove the id the save button in my create form doesnt add record to the grid without any error!
my save button is like this:
var form = button.up('formpanel');
var values = form.getValues();
var user = Ext.create('App.model.User',values);
var cntr = this.getView('UserContainer')
var mainpanel = button.up('user-container');
var grid = mainpanel.down('grid');
grid.store.add(user);
form.reset();
this.redirectTo('users')
any idea?
As you are using routes so in this case first you need to check you view is already created or not. If view is already created then you can use that view otherwise you can create view.
In this FIDDLE, I have create a demo using cardlayout, grid and form. I hope this will help/guide you to achieve your requirement.
CODE SNIPPET
Ext.application({
name: 'Fiddle',
launch: function () {
//Define cotroller
Ext.define('MainController', {
extend: 'Ext.app.ViewController',
alias: 'controller.maincontroller',
routes: {
'application/:node': 'onViewChange'
},
//this event will fire whenver routing will be changed
onViewChange: function (xtype) {
var view = this.getView(),
newNode = view.down(xtype);
//if view is not crated then 1st created
if (!newNode) {
newNode = Ext.create({
xtype: xtype
});
}
// is new view is form then first reset the form
if (newNode.isXType('form')) {
newNode.reset();
}
// if new view type is update-form then load the record
if (xtype == 'update-form') {
newNode.setRecord(this.record)
this.record = null;
}
//If view is created then directly set active that view
view.setActiveItem(newNode);
},
//this event will fire when main view render
onMainViewAfterRedner: function () {
this.redirectTo('application/feature-grid');
},
//this event will fire when grid items clicked
onGridItemClick: function (grid, index, target, record, e, eOpts) {
this.record = record;
this.redirectTo('application/update-form');
},
//this event will fire when cancel button clicked
onCancelButtonClick: function () {
this.redirectTo('application/feature-grid');
},
//this event will fire when add new button clicked
onAddNew: function () {
this.redirectTo('application/create-form');
},
//this event will fire when save button clicked
onSaveButtonClick: function (button) {
var me = this,
form = button.up('formpanel'),
store = me.getView().down('grid').getStore(),
values = form.getValues();
if (form.xtype == 'update-form') {
store.findRecord('id', values.id).set(values);
} else {
if (values.name && values.email && values.phone) {
delete values.id;
store.add(values);
} else {
Ext.Msg.alert('Info','Please enter form details');
return false;
}
}
this.onCancelButtonClick();
}
});
Ext.define('AppForm', {
extend: 'Ext.form.Panel',
layout: 'vbox',
bodyPadding: 10,
defaults: {
xtype: 'textfield',
//flex: 1,
width: '100%',
margin: '10 5',
labelAlign: 'top',
allowBlank: false
},
items: [{
name: 'id',
hidden: true
}, {
name: 'name',
label: 'Name'
}, {
name: 'email',
label: 'Email'
}, {
name: 'phone',
label: 'Phone Number'
}, {
xtype: 'toolbar',
defaults: {
xtype: 'button',
ui: 'action',
margin: 5,
flex: 1
},
items: [{
text: 'Save',
formBind: true,
handler: 'onSaveButtonClick'
}, {
text: 'Cancel',
handler: 'onCancelButtonClick'
}]
}]
});
//this create-form.
Ext.define('CreateForm', {
extend: 'AppForm',
alias: 'widget.create-form',
title: 'Create form'
});
//this update-form.
Ext.define('UpdateForm', {
extend: 'AppForm',
alias: 'widget.update-form',
title: 'Update form'
});
//this feature-grid.
Ext.define('fGrid', {
extend: 'Ext.panel.Panel',
alias: 'widget.feature-grid',
title: 'User List grid',
layout: 'vbox',
items: [{
xtype: 'grid',
flex: 1,
store: {
fields: ['name', 'email', 'phone'],
data: [{
name: 'Lisa',
email: 'lisa#simpsons.com',
phone: '555-111-1224'
}, {
name: 'Bart',
email: 'bart#simpsons.com',
phone: '555-222-1234'
}, {
name: 'Homer',
email: 'homer#simpsons.com',
phone: '555-222-1244'
}, {
name: 'Marge',
email: 'marge#simpsons.com',
phone: '555-222-1254'
}]
},
columns: [{
text: 'Name',
dataIndex: 'name',
flex: 1
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone',
flex: 1
}],
listeners: {
itemtap: 'onGridItemClick'
}
}],
tools: [{
type: 'plus',
iconCls: 'x-fa fa-plus',
handler: 'onAddNew'
}],
flex: 1
});
//Define the main view form extending panel
Ext.define('MainView', {
extend: 'Ext.panel.Panel',
controller: 'maincontroller',
alias: 'widget.mainview',
layout: 'card',
listeners: {
painted: 'onMainViewAfterRedner'
}
});
//this will create main view that is card layout
Ext.create({
xtype: 'mainview',
renderTo: Ext.getBody(),
fullscreen: true
});
}
});

How to create a Form for each row in a grid panel:extjs

How do i create a different form for each row in the Grid...
i have a grid like ..
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: 'Phone', dataIndex: 'phone' }
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
Your question doesn't explain your problem very well. Please update your topic and question. As I understood from your question, yes, you can. There are couple of ways. One of them is putting a form into a grid cell using grid column renderer. Another way is using editor in grid column. The second way is easy, but it's not proper way. If you want the second way also, I can add it. So, I'll add an efficient one. Please check the code below and fiddle:
https://fiddle.sencha.com/#fiddle/11i5
Ext.define('UploadForm', {
extend: 'Ext.form.Panel',
width: 200,
frame: true,
items: [{
xtype: 'filefield',
name: 'photo',
msgTarget: 'side',
allowBlank: false,
buttonText: 'Select'
}],
buttons: [{
text: 'Upload',
handler: function() {
var form = this.up('form').getForm();
if(form.isValid()){
form.submit({
url: 'photo-upload.php',
waitMsg: 'Uploading your photo...',
success: function(fp, o) {
Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.');
}
});
}
}
}],
initComponent: function () {
if (this.delayedRenderTo) {
this.delayRender();
}
this.callParent();
},
delayRender: function () {
Ext.TaskManager.start({
scope: this,
interval: 200,
run: function () {
var container = Ext.fly(this.delayedRenderTo);
if (container) {
this.render(container);
return false;
} else {
return true;
}
}
});
}
});
var store = Ext.create('Ext.data.Store', {
fields: ['Name', 'Phone', 'Email', 'filePath'],
data: [{
Name: 'Rick',
Phone: '23122123',
Email: 'something#mail.com',
filePath: '/home'
}, {
Name: 'Jane',
Phone: '32132183',
Email: 'some#thing.com',
filePath: '/home'
}]
});
var renderTree = function(value, meta, record) {
var me = this;
var container_id = Ext.id(),
container = '<div id="' + container_id + '"></div>';
Ext.create('UploadForm', {
delayedRenderTo: container_id
});
return container;
}
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: store,
columns: [
{ text: 'Name', dataIndex: 'Name' },
{ text: 'Email', dataIndex: 'Email' },
{ text: 'Phone', dataIndex: 'Phone' },
{ text: 'Upload',
dataIndex: 'filePath',
width: 300,
renderer: renderTree
}
],
renderTo: Ext.getBody()
});
P.s. Its based from Render dynamic components in ExtJS 4 GridPanel Column with Ext.create

Record is undefined Extjs4 to get the data out of the form

I have window with button enter and few fields i need to get the data out of the form and there is method on button :
enter: function (button) {
var win = button.up('window'),
form = win.down('form'),
record = form.getRecord(),
values = form.getValues();
record.set(values);
win.close();
this.getUsersStore().sync();
Here record is undefined. What i do wrong?
////////////////////////////////////////////////////////////////////
Here the form:
Ext.define('ExtMVC.view.portlet.Login', {
extend: 'Ext.window.Window',
alias: 'widget.login',
layout: 'fit',
title: 'LogIn',
width: 300,
height: 150,
autoShow: true,
store: 'LoginModels',
initComponent: function () {
this.items = [
{
xtype: 'form',
items: [
{
xtype: 'textfield',
name: 'Name',
fieldLabel: 'Name',
style: { 'margin': '10px' },
},
{
xtype: 'textfield',
name: 'Password',
fieldLabel: 'Password',
style: { 'margin': '10px' },
}
]
}
];
this.buttons = [
{
text: 'Enter',
action: 'enter',
//handler: this.enter
},
{
text: 'Cancel',
scope: this,
handler: this.close
},
{
text: 'Sing in',
scope: this,
handler: this.close
}
];
this.callParent(arguments);
}
});
try to replace with this code
values=form.getForm().getValues();
Please go through the ext doc as it clearly says:
getRecord( ) : Ext.data.Model :
Returns the currently loaded Ext.data.Model instance if one was loaded via loadRecord.
And in case of your example I dont see any code that loads your form panel using loadRecord().
enter: function (button) {
var win = button.up('window'),
form = win.down('form'),
//record = form.getRecord(), /*not required here*/
record = this.getUsersStore().findRecord('id', 1) /*if you know id or some thing which field is know*/
values = form.getValues();
record.set(values);
win.close();
this.getUsersStore().sync();
You have to load form using form.loadRecord() before fetching it through form.getRecord(), otherwise form.getRecord() returns undefined.

ExtJs getValues() from Form

i have a question.
probably it will be a easy solution.
how can i get the Values of the textfields, when i click the Save Button???
Ext.define('MyApp.view.main.MyForm', {
extend: 'Ext.Window',
layout: 'column',
.
.
.
defaults: {
layout: 'form',
xtype: 'container',
defaultType: 'textfield',
labelWidth: 150,
width: 300
},
items: [{
items: [
{ fieldLabel: 'FirstName', allowBlank: false },
{ fieldLabel: 'LastName', allowBlank: false },
]
}, {
items: [
{ fieldLabel: 'Street' },
{ fieldLabel: 'Town' },
]
}],
buttons: [
{ text: 'Save', handler: function(){ alert('Saved!'); } },
]
});
You must use form field container, for example - Ext.form.Panel.
Then you can use getForm() and then getValues(), also check your fields - isValid() for checking allowBlank.
var formPanel = Ext.create('Ext.form.Panel', {
name: 'myfieldform',
defaults: {
layout: 'form',
xtype: 'container',
defaultType: 'textfield',
labelWidth: 150,
width: 300
},
items: [{
items: [
{
fieldLabel: 'FirstName',
allowBlank: false
},
{
fieldLabel: 'LastName',
allowBlank: false
},
]
}, {
items: [
{ fieldLabel: 'Street' },
{ fieldLabel: 'Town' },
]
}]
});
Ext.define('MyApp.view.main.MyForm', {
...
items: [
formPanel
],
buttons: [
{
text: 'Save',
handler: function(btn) {
var form = btn.up().up().down('[name="myfieldform"]').getForm(),
values;
if (!form || !form.isValid())
{
alert('Check your form please!');
return;
}
values = form.getValues();
for(var name in values) {
alert(values[name]);
}
}
}
]
});
Sencha Fiddle Example
Your handler function will have the button and the event options in it's signature. Use the button and the "Up" function to get the form element and retrieve the record model attached to the form (assuming you are using models).
handler: function(btn, eOpts){
var form = btn.up('form');
form.getForm().updateRecord();
var record = form.getForm().getRecord();
alert('Saved!');
}
If you are not using a model and just want the values add an itemId to each field in your form and again use the up function with a "#" to retrieve a specific component. Then simply use the getValue method.
items: [
{ fieldLabel: 'FirstName', itemId: 'firstnamefield', allowBlank: false },
{ fieldLabel: 'LastName', itemId: 'lastnamefield', allowBlank: false },
]
handler: function(btn, eOpts){
var firstNameField = btn.up('#firstnamefield');
var firstNameValue = firstNameField.getValue();
alert('Saved!');
}
Seriously, why go with the up.up.down approach, if you can get to the thing straight away?
var form = Ext.ComponentQuery.query('[name="myfieldform"]').getForm()[0];
Or
values = Ext.ComponentQuery.query('[name="myfieldform"]').getForm()[0].getValues();
In other words, take above answer and make it like this:
var formPanel = Ext.create('Ext.form.Panel', {
name: 'myfieldform',
defaults: {
layout: 'form',
xtype: 'container',
defaultType: 'textfield',
labelWidth: 150,
width: 300
},
items: [{
items: [
{
fieldLabel: 'FirstName',
allowBlank: false
},
{
fieldLabel: 'LastName',
allowBlank: false
},
]
}, {
items: [
{ fieldLabel: 'Street' },
{ fieldLabel: 'Town' },
]
}]
});
Ext.define('MyApp.view.main.MyForm', {
...
items: [
formPanel
],
buttons: [
{
text: 'Save',
handler: function(btn) {
var form = Ext.ComponentQuery.query('[name="myfieldform"]').getForm()[0];
if (!form || !form.isValid())
{
alert('Check your form please!');
return;
}
values = form.getValues();
for(var name in values) {
alert(values[name]);
}
}
}
]
});

Resources