ExtJS 4 Grid enabling cell data editing with button clicks

Hello,

I may sound a little vague in this post but it will all make sense at the end.

There are many a times when we want to use ExtJS’ powerful grid system with row or cell editing enabled. In this post I will show you how you can enable cell editing for specific cells in a row by click of a button. Actually button is not required as it is but the problem I fixed today has this requirement but you will get the idea.

The Problem

Allow specific cell to be edited when user clicks on a button called Edit and then click on top of the cell.

Let me create an example grid first.

The Grid Data

  1. var gridData = {'names': [
  2. {'name':'Jaspreet'},
  3. {'name':'Chahal'},
  4. {'name':'Yuvraaj'}
  5. ]
  6. };

The Grid Store

This is just an example store. In real life application you will have much complex stores

  1. var store = Ext.create('Ext.data.ArrayStore', {
  2. fields: [
  3. {name: 'name'}
  4. ],
  5. data: gridData,
  6. proxy: {
  7. type: 'memory',
  8. reader: {
  9. type: 'json',
  10. root:'names'
  11. }
  12. }
  13. });

Temporary variable to hold of current cell value

  1. var enable_editing_for_value = '';

 

 

The Grid

  1. var grid = Ext.create('Ext.grid.Panel', {
  2. store: store,
  3. columns: [
  4. {
  5. text : 'Something Random',
  6. flex : 1,
  7. dataIndex: 'name',
  8. editor: {
  9. xtype:'textfield'
  10. }
  11. }
  12. ],
  13. height: 350,
  14. width: 400,
  15. title:'Cell editing on demand Example',
  16. renderTo: 'playcontainer',
  17. plugins: [
  18. Ext.create('Ext.grid.plugin.CellEditing', {
  19. clicksToEdit: 1
  20. // clicksToEdit can be either 1 (single click editing) or 2 (double click editing)
  21. // in our example we want our cell value to be edited on click
  22. })
  23. ],
  24. dockedItems: [{
  25. xtype: 'toolbar',
  26. dock: 'top',
  27. items: [{
  28. xtype:'button',
  29. text:'Edit value',
  30. handler:function() {
  31. var selection = grid.getView().getSelectionModel().getSelection()[0];
  32. // so when user clicks on Edit value button out cell value is temporarily store in our holder variable
  33. // this is just an example you may want to do the same on column index or column name
  34. enable_editing_for_value = selection.data.name;
  35. }
  36. }]
  37. }]
  38.  
  39. });

Now that our Grid is declared and the stage is set now is the time to introduce one final thing that will check on if a cell is editable or not.

Remember that our cell is on editable if user selects and click our ‘Edit value‘ button

The Grid Event

  1. grid.on('beforeedit',function(editor,a,eOpts) {
  2. if(a.value == enable_editing_for_value) {
  3. return true;
  4. }
  5. return false;
  6. });

Let me explain what is happening in our event handler. As you can see that event ‘beforeedit‘ is handled by our code. What this means is that when user try to click on our editable cell ‘beforeedit‘ fires and thus giving us a chance to compare it with what we got in enable_editing_for_value variable which hold the value of cell that was selected and then making it editable by click on our button ‘Edit value‘.

Now this is just a starting point. You can take it to another level and you can work on rows and cells, thus making cells of your choice editable.

 

 

The Demo

Demo for this post is located here

http://jaspreetchahal.org/examples/extjs-cell-editing-on-demand.html

I hope that if this post was not what you were after then it would open up another way which you can follow to achieve what you are trying to achieve.

Cheers

 

 

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

Comments

  1. Please check your demo once.
    its not working.
    please work it on, else remove the code /link

    • Well the example is working but if you haven’t read the article then there is no use viewing the example, isn’t it.
      Try this: select a row, hit the button “Cell editing works when you click me” and then click on the cell again, try to edit others too (that shouldn’t work unless you hit that button again)
      I hope this makes sense. This example covers editing on demand.

  2. interface of beforeedit is (editor, e, eOpts)

    grid.on(‘beforeedit’,function(editor, e, eOpts) {
    if(e.value == enable_editing_for_value) {
    // or e.record.get(‘name’) = …
    return true;
    }
    return false;
    })

    anyway… nice, used it for a ajax/closure holding some locking logic before “return false”

Speak Your Mind

*

CommentLuv badge