select row ExtJS6.2 viewport extends grid.panel - extjs

I am trying to delete a selected row in my Viewport component that extends Ext.grid.panel. The below example allows me to select rows but it is instantiated as a grid.Panel not a Viewport. Is it possible to select a specific row with a viewPort that extends grid.Panel
similar: https://fiddle.sencha.com/#view/editor&fiddle/2ech
I've had various attempts but I believe my issue is that I cannot grab the grid.
My code
var mainView = Ext.create('Ext.container.Viewport', {
//extends grid.panel
extend: 'Ext.grid.Panel',
items: [{
//xtype is used to re-use components or classes
xtype: 'grid',
store: userStore,
columns: [{
text: 'Name',
dataIndex: 'name',
flex: 1
}, {
text: 'Email Address',
dataIndex: 'email',
flex: 2
}, {
text: 'Phone Number',
dataIndex: 'phone',
flex: 2
}]
}, {
xtype: 'button',
text: 'NEW',
height: 50,
width: '33%',
handler: function () {
var win = Ext.create('innerWindow');
win.show();
}
}, {
xtype: 'button',
text: 'VIEW',
height: 50,
width: '33%',
handler: function () {
var win = Ext.create('innerWindow');
win.show();
}
}, {
xtype: 'button',
text: 'DELETE',
height: 50,
width: '33%',
handler: function () {
let selected = grid.getSelection();
//want to delete selected row
//var record = userStore.getAt(0);
userStore.remove(selected);
}
}]
});

First, give an id to your grid:
id: 'MyGrid'
Now you can reference the grid:
handler: function () {
var grid = Ext.getCmp("MyGrid");
var store = grid.getStore();
var selection = grid.getView().getSelectionModel().getSelection()[0];
if (selection) {
store.remove(selection);
}
}

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!");
}
}
}]
}]
});
}
}
});
}
});

how to send value from grid (pop up) to form panel in extjs

how to show data in form panel after click one of data in pop up in grid view, I have try another way but always wrong,
this is my grid panel :
var tt = Ext.define('Rendering.view.beli.dataSupplier', {
extend: 'Ext.form.Panel',
//extend: 'Ext.window.Window',
// xtype: 'beligrid',
alias : 'widget.contatoform',
frame: true,
// id: 'detailPanelis',
title: 'Company data',
bodyPadding: 5,
layout: 'column',
requires: [
'Ext.grid.*',
'Ext.form.*',
'Ext.layout.container.Column'
],
initComponent: function() {
this.items = [
{
columnWidth: 0.65,
xtype: 'gridpanel',
reference: 'customerGrid',
store: 'BeliStore',
columns : [{
text: 'Nama',
dataIndex: 'namaSupplier',
flex: 1
}, {
text: 'Alamat',
dataIndex: 'address',
flex: 1
}],
listeners: {
scope: this,
selectionchange: 'onSelectionChanges'
}
}];
//];
// });
this.callParent(arguments);
},
onSelectionChanges: function(model, records) {
//alert('yuhuuuu');
var editt = Ext.create('Rendering.view.beli.bg_beli');
var c = editt.onSelectionChange(model, records);
}
});
the function for to send data to form panel is
listeners: {
scope: this,
selectionchange: 'onSelectionChanges'
}
this is function of onSelectionChanges :
onSelectionChanges: function(model, records) {
//alert('yuhuuuu');
var editt = Ext.create('Rendering.view.beli.bg_beli');
var c = editt.onSelectionChange(model, records);
}
and form panel :
var tt = Ext.define('Rendering.view.beli.bg_beli', {
extend: 'Ext.form.Panel',
xtype: 'beligrid',
controller: 'binding-dynamic',
frame: true,
title: 'Company data',
bodyPadding: 5,
layout: 'column',
requires: [
'Ext.grid.*',
'Ext.form.*',
'Ext.layout.container.Column'
],
// In this example, configuration is applied at initComponent time
// because it depends on profileInfo object that is generated for the
// FormGrid instance.
initComponent: function() {
//Ext.apply(this, {
this.items = [
{
columnWidth: 0.65,
xtype: 'gridpanel',
store: 'BeliStore',
columns : [{
text: 'Nama',
dataIndex: 'namaSupplier',
flex: 1
}, {
text: 'Alamat',
dataIndex: 'address',
flex: 1
}],
listeners: {
scope: this,
selectionchange: 'onSelectionChange'
}
},{
columnWidth: 0.35,
margin: '20 0 0 10',
xtype: 'form',
title:'Company detailsss',
layout: 'anchor',
defaultType: 'textfield',
items: [
{
name : 'id_supplier',
fieldLabel: 'id',
hidden:true
},{
fieldLabel: 'Nama Supplier',
name: 'namaSupplier'
},{
fieldLabel: 'email',
name: 'email'
},{
fieldLabel: 'alamat',
name: 'address'
},{
xtype: 'button',
text: 'YUY',
action: 'add'
}]
}];
//];
// });
this.callParent(arguments);
},
onSelectionChange: function(model, records) {
alert('asdasd');
console.log(records);
var rec = records[0];
console.log(rec);
if (rec) {
var c = this.getForm().loadRecord(rec);
// this.getBeliStoreStore().load();
console.log(this.getForm().loadRecord(rec));
}
}
});
I send data from grid to form panel, and the function that accepted data in form panel is :
onSelectionChange: function(model, records) {
alert('asdasd');
console.log(records);
var rec = records[0];
console.log(rec);
if (rec) {
var c = this.getForm().loadRecord(rec);
// this.getBeliStoreStore().load();
console.log(this.getForm().loadRecord(rec));
}
}
please help, I have looking for to any reference but I don't get answer yet, thanks before
My suggestion is modify the call for event this.onSelectionChange or like that this.on('selectionchange',this.onSelectionChange) it is another way for call events in extjs
initComponent: function() {
var me = this ;
this.items = [
{
columnWidth: 0.65,
xtype: 'gridpanel',
reference: 'customerGrid',
store: 'BeliStore',
columns : [{
text: 'Nama',
dataIndex: 'namaSupplier',
flex: 1
}, {
text: 'Alamat',
dataIndex: 'address',
flex: 1
}],
listeners: {
scope: this,
selectionchange: me.onSelectionChanges
}
}];
//];
// });
this.callParent(arguments);
}
OR
initComponent: function() {
var me = this ;
var grid = Ext.create('Ext.grid.Panel', {
reference: 'customerGrid',
store: 'BeliStore',
columns : [{
text: 'Nama',
dataIndex: 'namaSupplier',
flex: 1
}, {
text: 'Alamat',
dataIndex: 'address',
flex: 1
}
]});
me.items = [
{
columnWidth: 0.65,
grid
}];
grid.on('selectionchange',me.onSelectionChange)
this.callParent(arguments);
}
http://docs.sencha.com/extjs/4.2.5/#!/example/build/KitchenSink/ext-theme-neptune/#form-grid

extjs proper way to replace main center panel

In ExtJS, on a menu toolbar button, I am trying to remove the current panel in my center window, then recreate it with the new selection. I do not understand the proper way to do this. So far when I click the menu item, it removes whatever is currently there successfully, then it will add the new panel successfully. The problem is the 2nd time I hit the button I get the following error:
REGISTERING DUPLICATE COMPONENT ID 'mainportalID'.
I realize its telling me I already used this ID, but then what would be the correct way to remove the current panel, and replace with a new one?
Here is my view controller:
selectMenuButton: function (button, e) {
console.log('select menu button section was hit')
console.log(button);
console.log(e);
var optionString = button.text;
var myDetailsPanel = Ext.getCmp('navview');
console.log(myDetailsPanel);
var count = myDetailsPanel.items.items.length;
if (count > 0) {
myDetailsPanel.items.each(function (item, index, len) {
myDetailsPanel.remove(item, false);
});
}
myDetailsPanel.add({
xtype: optionString
});
}
var myStore = Ext.create('ExtApplication1.store.PositionsStore');
var gridSummary = Ext.create('Ext.grid.Panel', {
store: myStore,
width: 600,
title: 'my first grid',
columns: [
{
text: 'AcctNum',
dataIndex: 'AcctNum',
width: 100
},
{
text: 'AcctShortCode',
dataIndex: 'AcctShortCode',
flex: 1
},
{
text: 'Exchange',
dataIndex: 'Exchange',
width: 200
}
]
});
This is my view
Ext.define('ExtApplication1.view.main.MainPortal', {
extend: 'Ext.panel.Panel',
xtype: 'mainportal',
alias: 'widget.mainportal',
id: 'mainportalID',
html: 'user... this is the main portal window',
autoScroll: true,
bodyPadding: 10,
items: [
gridSummary
]
});
adjusted panel
Ext.define('ExtApplication1.view.main.MainPortal', {
extend: 'Ext.panel.Panel',
xtype: 'mainportal',
alias: 'widget.mainportalAlias',
reference: 'gridtextfield',
//id: 'mainportalID',
html: 'user... this is the main portal window',
autoScroll: true,
bodyPadding: 10,
items: [
gridSummary
]
});
adjusted view controller
onComboboxSelect: function (combo, record, eOpts) {
console.log('new listener was hit');
//return selected Item
var selectedValue = record.get('ClientName');
var selectedCID = record.get('ClientID');
//find the grid that was created
var me = this;
console.log(me);
var xxx = this.lookupReference('gridtextfield');
debugger;
//debugger;
var mainPortalView = Ext.getCmp('mainportalID');
var targetGrid = mainPortalView.down('grid');
//find the store associated with that grid
var targetStore = targetGrid.getStore();
//load store
targetStore.load({
params: {
user: 'stephen',
pw: 'forero',
cid: selectedCID
}
//callback: function (records) {
// Ext.each(records, function (record) {
// console.log(record);
// });
// console.log(targetStore);
//}
});
},
added listeners to MainPortal.js
var myStore = Ext.create('ExtApplication1.store.PositionsStore');
var gridSummary = Ext.create('Ext.grid.Panel', {
store: myStore,
width: 600,
title: 'my first grid',
columns: [
{
text: 'AcctNum',
dataIndex: 'AcctNum',
width: 100
},
{
text: 'AcctShortCode',
dataIndex: 'AcctShortCode',
flex: 1
},
{
text: 'Exchange',
dataIndex: 'Exchange',
width: 200
}
],
listeners: {
destroy: function () {
debugger;
}
}
});
Ext.define('ExtApplication1.view.main.MainPortal', {
extend: 'Ext.panel.Panel',
xtype: 'mainportal',
alias: 'widget.mainportalAlias',
//id: 'mainportalID',
itemId: 'mainportalID',
html: 'user... this is the main portal window',
autoScroll: true,
bodyPadding: 10,
items: [
gridSummary
],
listeners: {
destroy: function () {
debugger;
}
}
});

Why is event not fired again when click inside ExtJS 4.2.2 text input field

In the below ExtJS 4.2.2 code, you can click repeatedly on the "Search" and "Show Label" controls, and the label "here is the text" will toggle visible/hidden.
But if you click in the search text input field, the label is only hidden the first time you click there. If you then click "Show Label" to once again display the label, and then again click the search text input field, the label if not hidden.
Ext.define('MyToolbar', {
extend: 'Ext.grid.feature.Feature',
alias: 'feature.myToolbar',
requires: ['Ext.grid.feature.Feature'],
width: 160,
init: function () {
if (this.grid.rendered)
this.onRender();
else{
this.grid.on('render', this.onRender, this);
}
},
onRender: function () {
var panel = this.toolbarContainer || this.grid;
var tb = panel.getDockedItems('toolbar[dock="top"]');
if (tb.length > 0)
tb = tb[0];
else {
tb = Ext.create('Ext.toolbar.Toolbar', {dock: 'top'});
panel.addDocked(tb);
}
this.createSearchBox(tb);
},
createSearchBox: function (tb) {
tb.add({
text: 'Search',
menu: Ext.create('Ext.menu.Menu'),
listeners: {
click: function(comp) {
MyApp.app.fireEvent('onGridToolbarControlClicked', comp);
}
}
});
this.field = Ext.create('Ext.form.field.Trigger', {
width: this.width,
triggerCls: 'x-form-clear-trigger',
onTriggerClick: Ext.bind(this.onTriggerClear, this)
});
this.field.on('render', function (searchField) {
this.field.inputEl.on('click', function() {
MyApp.app.fireEvent('onGridToolbarControlClicked', searchField);
}, this, {single: true});
}, this, {single: true});
tb.add(this.field);
}
});
Ext.define('MyPage', {
extend: 'Ext.container.Container',
alias: 'widget.myPage',
flex: 1,
initComponent: function () {
var me = this;
Ext.applyIf(me, {
items: [{
xtype: 'container',
layout: {
type: 'vbox',
align: 'middle'
},
items: [{
xtype: 'button',
text: 'Show Label',
handler: function(comp) {
comp.up('myPage').down('label').setVisible(true);
}
},{
xtype: 'label',
itemId: 'testLbl',
text: 'here is the text'
},{
xtype: 'gridpanel',
width: 250,
height: 150,
store: Ext.create('Ext.data.Store', {
fields: ['name'],
data: [
{name: 'one'},
{name: 'two'},
{name: 'three'}
]
}),
columns: [{
text: 'Text',
flex: 1,
dataIndex: 'name'
}],
features: [{
ftype: 'myToolbar'
}]
}]
}]
});
me.callParent(arguments);
MyApp.app.on({onGridToolbarControlClicked: function(comp) {
if('function' == typeof comp.up && !Ext.isEmpty(comp.up('myPage')) &&
'function' == typeof comp.up('myPage').down &&
!Ext.isEmpty(comp.up('myPage').down('label'))) {
comp.up('myPage').down('label').setVisible(false);
}
}});
}
});
Ext.onReady(function() {
Ext.application({
name: 'MyApp',
launch: function() {
Ext.create('Ext.container.Viewport', {
renderTo: Ext.getBody(),
width: 700,
height: 500,
layout: 'fit',
items: [{
xtype: 'myPage'
}]
});
}
});
});
Here
this.field.inputEl.on('click', function() {
MyApp.app.fireEvent('onGridToolbarControlClicked', searchField);
}, this, {single: false});
instead of {single:true} in your code. onRender IS single, onClick - (in your case) is not.

EXTJS Dynamically put a component into a container with other components

I'd like to achieve this behavior:
If a field (combo, text, date ...) in a form panel has a custom property set true
{
xtype: 'textfield',
fieldLabel: 'Name',
name: 'Name',
customProp: true
},
then add a button or other components behind the actual component. Writen in json it would look like this:
{
xtype: 'container',
margin: '0 0 8 0',
layout: 'hbox',
items: [
{
xtype: 'textfield',
fieldLabel: 'Name',
name: 'Name',
},
{
xtype: 'button',
text: 'xxx',
tooltip: 'I\'m a custom tooltip'
}
]
}
I'm wondering how i could achieve this. Is this even possible ?
It is.
Ext.require('*');
Ext.onReady(function() {
var form = new Ext.form.Panel({
renderTo: document.body,
width: 400,
height: 100,
items: {
xtype: 'textfield',
fieldLabel: 'Foo'
}
});
setTimeout(function() {
var field = form.down('textfield');
// We have a reference to the field, let's go!
var owner = field.ownerCt;
owner.remove(field, false);
owner.add({
xtype: 'container',
layout: 'hbox',
items: [field, {
xtype: 'button',
text: 'Foo'
}]
});
}, 1000);
});
Where container is a reference to the container with the hbox layout.

Resources