ExtJs4 Context Menu Examples

Hi Guys,

So with my current project I did this couple of times and I though may be before I forget how to do it again I better document it somewhere and what better place than this website? Atleast this post will serve a reference to me and could help someone looking for exact same information.

Alright Enough of chit chat and lets get to the job in hand. I will show you how to do contect menus both in a Grid and with a HTML element such as DIV.

A demo is available from another site of mine called DemoPill.com, A link is provided at the end of this article (Funny Eh!!)

First up lets do context menus with Grid.

ExtJs Context Menu With Grid

HTML markup for this Demo

  1.   <div id="playcontainer" style="margin:50px">This string will be replaced</div>
  2. <div id="contextMenuGridExample" style="margin:50px">
  3. </div>

It all will make sense in a while. Just keep an eye on the container IDs

Ok now that the Markup has been taken care of, we need data to display in the Grid right? So lets use an array store with some hard coded data

  1. var gridData = [
  2. ['Jaspreet'],
  3. ['Chahal'],
  4. ['Yuvraaj']
  5. ];

Now lets create a Array Store that will be used by our Grid in a while.

  1. var store = Ext.create('Ext.data.ArrayStore', {
  2. fields: [
  3. {name: 'name'}
  4. ],
  5. data: gridData // we are feed our array data here
  6. });

Now that we have our store ready lets put our Context menu together. A context Menu has various Menu Items which respond to user Action such as Mouse Click. So we will create a Action Handler for our Context Menu

  1. var replaceAction = Ext.create('Ext.Action', {
  2. text: 'Replace above text with selected value',
  3. handler: function(widget, event) {
  4. var rec = grid.getSelectionModel().getSelection()[0];
  5. if (rec) {
  6. // here we are replacing innerHTML of our container with ID playcontainer
  7. Ext.get("playcontainer").update(rec.get('name'));
  8. } else {
  9. alert('Please select a name from the grid');
  10. }
  11. }
  12. });

Now lets create our context menu object

  1. var gridContextMenu = Ext.create('Ext.menu.Menu', {
  2. items: [
  3. // we are feeding our declared actions to Context Menu Items array
  4. replaceAction
  5. ]
  6. });

So hopefully you are still with me right? Nothing much has happened as yet. What we did uptil now is

- wrote our markup
- create a DATA array
- created a array STORE
- create an Ext Action
- Fed Ext action to Ext.menu.Menu

Ok lets move on and create our Grid Object

  1. var grid = Ext.create('Ext.grid.Panel', {
  2. store: store, // our array store is Fed
  3. columns: [
  4. {
  5. text : 'Something Random',
  6. flex : 1,
  7. dataIndex: 'name' // remember that dataIndex value should be named exactly what you declared in your store
  8. }
  9. ],
  10. viewConfig: {
  11. listeners: {
  12. itemcontextmenu: function(view, rec, node, index, event) {
  13. event.stopEvent(); // stops the default event. i.e. Windows Context Menu
  14. gridContextMenu.showAt(event.getXY()); // show context menu where user right clicked
  15. return false;
  16. }
  17. }
  18. },
  19. height: 350,
  20. width: 400,
  21. title:'context Menu Grid Example',
  22. renderTo: 'contextMenuGridExample', // remember this is from Markup that we included earlier
  23. stateful: false
  24. });

That’s it. You will see something like this on screen

ExtJS Context menu example

 

Now lets see how we can do exact same thing with say a DIV container. I mean calling a Context Menu when someone right click on you DIV element.

 

Advertisement

 

ExtJS Context Menu on DIV

Ok so first up is Markup. On you page put this code somewhere.

  1. <div style="padding:15px;background:#EEE;border:3px solid #AAA;border-radius:10px;color:#000;width:500px;margin:50px" id="elementContextMenu">Right click here</div>
Now we will write our Ext.Action so that it can be fed to our Menu itemsproperty
  1. var removeAction = Ext.create('Ext.Action', {
  2. text: 'Remove this element, because I don\'t like it',
  3. handler: function(widget, event) {
  4. if(confirm("This element will be removed from DOM. You will need to refresh your browser to make it reappear. Continue?")) {
  5. Ext.get("elementContextMenu").remove();
  6. }
  7. return false;
  8. }
  9. });

That’s pretty much what we have done above. The only code I’ve removed is the one that was related to Grid and added a line that will remove our DIV element from DOM (document object model)

Now lets create our Context Menu object

  1. var contextMenu = Ext.create('Ext.menu.Menu', {
  2. items: [
  3. removeAction
  4. ]
  5. });

Now lets bind contextmenu event to our DIV, this is done so that when user right clicks on our DIV a context menu is shown.

  1. Ext.onReady(function() {
  2. Ext.get("elementContextMenu").on("contextmenu",function(event,element){
  3. event.stopEvent();
  4. contextMenu.showAt(event.getXY());
  5. return false;
  6. })
  7. });

That’s pretty much it.

To see above example in action you can browse to Demo Pill here

http://jaspreetchahal.org/examples/extjs-context-menu-example.html

I hope that this post was helpful

Cheers,

ExtJS 4 API docs

http://docs.sencha.com/ext-js/4-1

Advertisement

 

 

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

Speak Your Mind

*

CommentLuv badge