ExtJS 4 Grid Column value dependent on another Column value in same row

Hi Guys,

May be I don’t know how to write article titles but let me explain what I am trying to say here.

This Example code use version 4 of 

ExtJs4

Say you have a Grid and one of your Column value is Dependent on another Column value. i.e.

Pseduo code

Col A = {
           if(ColB.value =='yes') 
             then value is "Green" 
           else 
             value is "Red"
        }

Alright that doesn’t make sense, lets take an Example here and then we will use Column Renderer to manipulate a value

Say you have a Model as shown below

  1. Ext.define('Summaries', {
  2. extend: 'Ext.data.Model',
  3. fields: ['ResultKey', 'Status']
  4. });

And you have a Store that make use of above model

  1. var Summaries = Ext.create('Ext.data.Store', {
  2. autoLoad: true,
  3. autoSync: true,
  4. model: 'Summaries',
  5. proxy: {
  6. type: 'rest',
  7. url: '/Summaries/?format=json',
  8. reader: {
  9. type: 'json',
  10. root: 'Summaries'
  11. }
  12. }
  13. });

In above model I have properties ResultKey and Status, 

Lets assume I have two Statuses returned A and B but I want to another one called C which by default is not returned by the Server but is finalized at the client side based on the value of ResultKey

So say if my ResultKey value is 3 then Status will be shown as C

I hope that you are still with me. If not then re-read the above lines.

Alright lets write some code for our Grid.

  1. var grid = Ext.create('Ext.grid.Panel', {
  2. renderTo: Ext.getBody(),
  3. width: 1000,
  4. height: 600,
  5. store: Summaries,
  6. iconCls: 'icon-user',
  7. columns: [{
  8. text: 'Result key',
  9. width: 50,
  10. sortable: true,
  11. dataIndex: 'ResultKey'
  12. }, {
  13. header: 'Status',
  14. width: 100,
  15. sortable: true,
  16. // renderer: function(){},
  17. dataIndex: 'Status'
  18. }
  19. });

So above will create a Grid. Notice the commented code

// renderer: function(){},

We will be using that to render our Status

For column renderer reference please visit

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.grid.column.Column-cfg-renderer

renderer function takes the following params

  • value : Object

    The data value for the current cell, this could be string, number etc

  • metaData : Object

    Cell Metadata

  • record : Ext.data.Model

    Current row

  • rowIndex : Number

    Current row index

  • colIndex : Number

    Current Column index

  • store : Ext.data.Store

    The data store

  • view : Ext.view.View

    The current view

 

Lets change our column renderer function to something like this

renderer: function(value,metaData,record){
               if(record.ResultKey == 3){
                    return 'C';
               }
               else {
                    return value;  
               } 
          },

 

So the full Grid Code will be

  1. var grid = Ext.create('Ext.grid.Panel', {
  2. renderTo: Ext.getBody(),
  3. width: 1000,
  4. height: 600,
  5. store: Summaries,
  6. columns: [{
  7. text: 'Result key',
  8. width: 50,
  9. sortable: true,
  10. dataIndex: 'ResultKey'
  11. }, {
  12. header: 'Status',
  13. width: 100,
  14. sortable: true,
  15. renderer: function(value,metaData,record){
  16. if(record.ResultKey == 3){
  17. return 'C';
  18. }
  19. else {
  20. return value;
  21. }
  22. },
  23. dataIndex: 'Status'
  24. }
  25. });

 

If you see any bugs above that's Ok because I did much of the typing in the Post itself rather than copy pasting it :) Bottom line is that you get the Idea on how to access cross column properties in Column Renderers

I hope this helps

Cheers,

Advertisement

 

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

Speak Your Mind

*

CommentLuv badge