Category Archives: ExtJs editor grid

Small Tips for ExtJs Programming

Hello,

Here I am sharing some tips that would be useful for you while working with ExtJs. These tips are about issues I faced while working on ExtJs projects.

1) Get-Set ExtJs Panel HTML

I don’t know how many of you faced this issue. While working on one project, requirement was to set panel HTML dynamically. If you check Panel class there is not any method like getHtml() setHtml(). Following is the solution for it.

var dynamicHtml = ‘<div> This is dynamic HTML</div>’;
myPanelObj.body.dom.innerHTML = dynamicHtml;

This will set panel HTML. Same way you can get panel HTML using myPanelObj.body.dom.innerHTML.

2) ExtJs Window me.Events are undefined error

If you are extending ExtJs window and on creating object of extended control, if you get above error then you forgot to call base class constructor. Call the base class constructor using following code.

ExtendedWindowObject.superclass.constructor.call(this, config);

3) Set padding while using ExtJs panel table layout.

When you use table layout in ExtJs panel layout. All the controls are arranged in a table. Sometimes it does not look good if you don’t give padding to it. It looks very clumsy. So to give padding give id to panel and create a class in CSS with name of that id. See the example below.

#myPanelObj td{
    padding:5px;
},

4) Use renderer to format data in ExtJs grid cell.
If you want to format your data in grid cell use renderer. For example if you are displaying currency in ExtJs grid and you want to append $ sign to it. 
renderer: function(value, cell, record, rowIndex, colIndex, store)
{
          return value + ‘$’;
}
5) Dynamically add event to ExtJs controls. 
Sometimes it may happen that you have to sue some ExtJs controls and you are not allowed to change the control and you need to add some listeners. Following is the code to add listeners dynamically.
myExtJsObj.on(‘hide’,function(){ 
//function body.
});
6) Add Panel Click event
ExtJs panel does not support click event directly. However using following code you can make clickable panel.
listeners: {
                    render: function(p) {                                                                            
                                          p.getEl().on(‘click’, handleProductPanelClick.createDelegate(null, p, true,this.id));
                     }
}
7) Another use of renderer to give style.
You can also use renderer  to give some styles to cells.
renderer: function(value, metaData, r) { 
                  metaData.attr = ‘ style=”color:red;” ‘;
                  return value;
}
This will assign red color to cell.
That’s it for now. I will update this post whenever I will have some more tips.

ExtJs Grid Paging – Go To Last Page After Inserting New Record

Hello,

Recently I was working on a ExtJs editor grid where user can enter new row by pressing button in the top toolbar. A row is inserted at bottom of grid and user can insert data in row. When user tab out or click on Save, the data goes to database and grid store reloaded. See the image below.

Now here is the issue. Consider grid page size as 25 and number of records are 35 and you are on page 1. That means you are viewing record 1 to 25. Now when you insert the row and when grid is refreshed, newly inserted record is in page 2 and you are still on page 1 so sometimes user feels that data is not inserted. So how to overcome this. I will describe solution that I have used. There may be other better solution. If you have any suggestion please add a comment.

First you need to add afteredit event in you grid. See the code below.

listeners:{
            afteredit:function(e){                       
                         Ext.Ajax.request({
                                    url : ‘http://www.example.com/insertData.php’,
                                    method:’GET’,
                                    params:{                                 
                                        id : e.record.id,
                                        value : e.value
                                    },
                                    success:function(resp,opt){                                     
                                    },
                                    failure:function(resp,opt){
                                       
                                    }
                                });
             }
}

When you tab out of editor or Click on Save this event will be fired and it will send data to database using ajax request. Now we all know that for paging in MySql we have to use start and limit. For example.

select * from myTable limit 0,25

Above query will give first 25 records. to fetch next page use following query

select * from myTable limit 25,25

here first 25 indicates first row number of second page. So my trick was to return first row index of last page of table in which new record is inserted. Check the following code.

$start = 25;
$result = $start;

$output[ ‘success’ ] = true;
$output[ ‘result’ ] = $result; 
echo json_encode($output);

Above code will return JSON data back to browser. Now use that to reload the store. Following code will come in Success methods of Ajax Request mentioned in above code.

var returnValue= eval(‘(‘ + resp.responseText + ‘)’);                                  
var start = returnValue.result;
if(start){
              myGrid.store.baseParams.start = start;                                      
}
myGrid.getView().refresh();
myGrid.store.reload();

ExtJs grid store uses start and limit parameter along with paging toolbar. So trick was to set start in base parameters of grid store and reload the grid. This will lead you to the last page in grid after inserting new record so user can see newly added record.

Refresh single ExtJs grid row after editing without refreshing grid

Hello,

This is another ExtJs blog. Recently I was working with ExtJs editor grid and I got requirement that after editing data in a row grid should be refreshed and at the same time row should be back to its normal position.
Those who have worked with ExtJs editor grid know that when you edit values in row cells, it will display red mark on top left corner. It states that row data is edited. See the image below.

To remove it, we need to reload grid data and refresh the grid view. Following is the code for it.
myGrid.store.reload();
myGrid.getView().refresh();
Grid will be refreshed and updated data gets loaded from server. Now my requirement was to refresh the only single row, without refreshing the whole grid. When you tab out of edit field or you click outside of the row it will fire afteredit event. Normally in afteredit event there is a Ajax request to send updated data to server and when we get success response we normally reload the grid data store. But in this case its different situation. Following is the code for it.
‘afteredit’:{
fn : function(e){
Ext.Ajax.request({
url:”savedata.php”,
method:’GET’,
params:{
param1:value1,
param2:value2
},
success:function(resp,opt){
e.record.set(e.field,gridValue);
e.record.commit();
myGrid.getView().refresh();
},
failure:function(resp,opt){
}
});
}
}
Checkout the code in success function of Ajax request. This does the trick.
e.reocord.set() function set’s the value. For example if you have some renderer for columns then this code will set the cell value according to renderer.
e.record.commit() commits the changes and grid row is back to its normal view. It will remove all the red marks from the edited cell.
So using this small trick you have normal row after editing it without refresh the grid data. This trick is useful when you have large number of data in the grid and you want to avoid unnecessary data flow.