Displaying Menu On Context Menu Extjs - extjs

Up until 6.2, context menu on a grid worked fine by doing
itemcontextmenu: function (a,b,c,d, e) {
contextmenu.showAt(e.getXY());
}
But with 6.5, the menu doesn't show at the given XY coordinate if the menu is shown outside of the context menu. I have included an example below. Anyone seen this issue? I have tried turning on the animation option too, but the menu doesn't constrain within the panel, so when you right click at the bottom of the grid, the menu shows at the bottom below the panel.
Any input is highly appreciated
Working example
Right click on any grid row
Context Menu shows where you clicked.
Non-working example
Click on the Menu Button (menu shows below the button)
Right click on any grid row
Context menu shows where it was displayed below Menu Button instead of where you clicked.
var mymenu = new Ext.menu.Menu({
items: [
// these will render as dropdown menu items when the arrow is clicked:
{text: 'Item 1', handler: function(){ alert("Item 1 clicked"); }},
{text: 'Item 2', handler: function(){ alert("Item 2 clicked"); }}
]
});
Ext.create('Ext.button.Split', {
renderTo: Ext.getBody(),
text: 'Menu Button',
menu: mymenu
});
Ext.create('Ext.data.Store', {
storeId: 'simpsonsStore',
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: '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(),
listeners: {
scope: this,
itemcontextmenu: function (a,b,c,d,e){
e.stopEvent();
mymenu.showAt(e.getXY());
}
}
});

I made a fiddle in 6.2 and it has the exact same behaviour as 6.5
https://fiddle.sencha.com/#view/editor&fiddle/23kn
The issue is you are assigning the same menu for context menus to the split button. You would need to destroy and recreate the menu each time. Also as a side note you should cache the context menu on the grid otherwise every time you right-click you are creating a new menu and the old one still remains...big memory leak.

You can prevent memory leak like this.
new Ext.grid.Panel({
plugins: 'viewport',
title: 'Users',
dockedItems: [{
xtype: 'toolbar',
dock: 'top',
items: [{
xtype: 'splitbutton',
text: 'menu',
menu: mymenu
}]
}],
store: {
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'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
height: 200,
width: 400,
listeners: {
scope: this,
itemcontextmenu: function (a, b, c, d, e) {
e.stopEvent();
var mymenu = new Ext.menu.Menu({
items: [
{
text: 'Item 1',
handler: function () {
alert("Item 1 clicked");
}
}, {
text: 'Item 2',
handler: function () {
alert("Item 2 clicked");
}
}
],
listeners:{
hide:function(){
setTimeout(function(){
mymenu.destroy();
},2000);
}
}
});
mymenu.showAt(e.getXY());
}
}
});

Related

ExtJS 7 How to select grid row on rightclick

I'd like to both open a context menu and select the right-clicked row in a ExtJS 7 modern grid. The context menu works with the code below. However, I cannot find a way to select the row. The grid.getSelectionModel() seems to be no longer available in ExtJS 7.
// Listener in my Ext.app.ViewController manages to update and show context menu but not to select the row.
onContextMenu: function (e) {
const grid = this.getView();
const target = e.getTarget(grid.itemSelector);
if (target) {
e.stopEvent();
const item = Ext.getCmp(target.id);
if (item) {
// Would like to select row here with something like grid.getSelectionModel().selectRow(rowindex);
this.updateMenu(item.getRecord(), item.el, e);
}
}
}
Have a look at the following fiddle sample (Modern toolkit 7.3.1)
Ext.application({
name: 'Fiddle',
launch: function () {
const menu = new Ext.menu.Menu({
items: [{
text: 'Menu Item 1'
}, {
text: 'Menu Item 2'
}]
});
Ext.Viewport.add({
xclass: 'Ext.grid.Grid',
store: Ext.create('Ext.data.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'
}]
}),
columns: [{
text: 'Name',
dataIndex: 'name',
width: 200
}, {
text: 'Email',
dataIndex: 'email',
width: 250
}, {
text: 'Phone',
dataIndex: 'phone',
width: 120
}],
listeners: {
childcontextmenu: function (grid, location) {
const {
record,
event
} = location;
grid.deselectAll();
grid.setSelection(record);
menu.showAt(event.getX(), event.getY());
event.stopEvent()
}
}
})
}
});

Ext.Net Grid Panel with Selection Model readonly

I have a grid panel with a checkbox selection model. I also have an initial set of data to fill the grid: some of the rows that are inserted from code behind ( in the page load ) are checked (example in the image below);
My problem is that after the initial insertion of the rows I need to make the checkbox uncheckable.
Any suggestions?
I can't use gridPanel.setReadOnly(true) and also
BeforeDeselect Handler="return false;"
(because with that also the initial inserted rows appears unchecked).
Thanks
You can do following code
var store = Ext.create('Ext.data.Store', {
fields: ['id', 'name', 'email', 'phone'],
data: [{
id: 1,
name: 'Lisa',
email: 'lisa#simpsons.com',
phone: '555-111-1224'
}, {
id: 2,
name: 'Bart',
email: 'bart#simpsons.com',
phone: '555-222-1234'
}, {
id: 3,
name: 'Homer',
email: 'homer#simpsons.com',
phone: '555-222-1244'
}, {
id: 4,
name: 'Marge',
email: 'marge#simpsons.com',
phone: '555-222-1254'
}]
});
var records = [store.getById(1), store.getById(3)];
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: store,
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
height: 200,
width: 400,
renderTo: Ext.getBody(),
selModel: {
selType: 'checkboxmodel'
},
listeners: {
boxready: function() {
this.getSelectionModel().select(records, false, true);
},
selectionchange: function() {
this.getSelectionModel().deselectAll(true);
this.getSelectionModel().select(records, false, true);
}
}
});
Records are rows that you want to be selected all the time
Note: It's in ExtJs not Ext.Net

How to uncheck the checkcolumn of perticular row in Ext grid

I have a grid which has a column of checkboxes. I have used xtype checkcolumn
for this. follow fiddle here: https://fiddle.sencha.com/#view/editor&fiddle/2ano
Now I want to uncheck checkbox at perticular(in this case 2nd) row on click of given button. I tried getting them using xtypes but no luck.
finally I found a workaround.
store.getAt(1).data.active = false;
grid.getView().refresh();
It works but not sure if its the correct way to do so.
I will be glad for any suggestions.
Thank you.
One way as per your code.
You can use this methods of grid store store.each(), store.clearFilter() and store.filter('active',true).
In this FIDDLE, you can check here uncheck checked columns using record.set('active',false).
var store = Ext.create('Ext.data.Store', {
fields: ['name', 'email', 'phone', 'active'],
data: [{
name: 'Lisa',
email: 'lisa#simpsons.com',
phone: '555-111-1224',
active: false
}, {
name: 'Bart',
email: 'bart#simpsons.com',
phone: '555-222-1234',
active: true
}, {
name: 'Homer',
email: 'homer#simpsons.com',
phone: '555-222-1244',
active: false
}, {
name: 'Marge',
email: 'marge#simpsons.com',
phone: '555-222-1254',
active: false
}]
});
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
itemId: 'test',
renderTo: Ext.getBody(),
store: store,
buttons: [{
text: 'Un check',
handler: function () {
var store = this.up('grid').getStore();
store.clearFilter();
store.filter('active', true);
store.each(function (rec) {
rec.set('active', false);
});
store.clearFilter();
}
}],
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}, {
xtype: 'checkcolumn',
text: 'Active',
dataIndex: 'active'
}]
});
Another way as per your code.
You can use selModel and selType configs for grid.
In this FIDDLE, I have created a demo using selType and selModel config. it will help you or guide you more about grid checkbox selection.
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: store,
id: 'testGrid',
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
renderTo: Ext.getBody(),
selModel: {
checkOnly: false,
//injectCheckbox: 'last',
mode: 'SIMPLE'
},
selType: 'checkboxmodel',
buttons: [{
text: 'Select All',
handler: function () {
Ext.getCmp('testGrid').getSelectionModel().selectAll();
}
}, {
text: 'Deselect All',
handler: function () {
Ext.getCmp('testGrid').getSelectionModel().deselectAll();
}
}]
});

Set a renderer for a column with a click of a button

I want to create a button that when I click on it, it will set a renderer for a column on my grid. I'm looking through the API for columns http://docs.sencha.com/extjs/5.1/5.1.1-apidocs/#!/api/Ext.grid.column.Column
and I do not see a method that says setRenderer, how can I achieve this?
Edit: I do not want to set it when I create the column ( i know there is a property to set the renderer)
You could try simply changing the renderer, no need for a setRenderer
Ext.create('Ext.data.Store', {
storeId: 'simpsonsStore',
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: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ text: 'Name', dataIndex: 'name',
renderer: function (value) {
return value + ' Simpson';
}},
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: 'Phone', dataIndex: 'phone' }
],
height: 400,
width: 400,
renderTo: Ext.getBody(),
bbar: [{
text: 'Change First Column Renderer',
handler: function(b) {
var grid = b.up('grid'),
columns = grid.getColumnManager().getColumns(),
column = columns[0];
column.renderer = function(value) {
return value === 'Lisa' ? 'L Simpson' : value;
}
grid.view.refreshView();
}
}]
});
Fiddle: https://fiddle.sencha.com/#fiddle/1a5e

ExtJS Gridpanel selected rows

I am design ExtJs Gridpanel with Checkboxes...
How to get checked records for save the data
Use getSelections to get all selected records and getSelected to get the first record.
var selected = checkBoxSelectionModelObj.getSelections();
for (var i = 0; i < selected.length; i++)
{
alert(selected[i].data.code);
}
In ExtJs docs provide method to get selected record in grid grid.getSelection(). You can refer ExtJs docs
I have create small demo to show you, how it work. Sencha fiddle example
var store = Ext.create('Ext.data.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'
}, {
name: 'AMargeia',
email: 'marge#simpsons.com',
phone: '555-222-1254'
}]
});
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: store,
id: 'testGrid',
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
height: 200,
width: 400,
renderTo: Ext.getBody(),
selModel: {
checkOnly: false,
injectCheckbox: 'last',
mode: 'SIMPLE'
},
selType: 'checkboxmodel',
buttons: [{
text: 'Select All',
handler: function () {
Ext.getCmp('testGrid').getSelectionModel().selectAll();
}
}, {
text: 'Deselect All',
handler: function () {
Ext.getCmp('testGrid').getSelectionModel().deselectAll();
}
},{
text:'Print Selected Recod',
handler:function(){
var selection = Ext.getCmp('testGrid').getSelection();
if(selection.length){
let name='';
selection.map(item=>{
name+=item.get('name')+'<br>';
});
Ext.Msg.alert('Selected Record',name);
}else{
Ext.Msg.alert('Error','Please select record');
}
}
}]
});

Resources