IbrahimMitko
IbrahimMitko

Reputation: 1207

ExtJS4 - Tooltip not displaying column value

I'm trying to write a tooltip that's specific for each column in a grid.

My current implementation is failing to pick up the value of the record that's being hovered over. How do I reference the specific column/row value of the row entry?

fiddle

Code

Ext.onReady(function() {
var trackStore = new Ext.data.Store({
  storeId: 'soundCloudStore',

    proxy: {
      type: 'memory',
      data: [
          {
           title: 'Test', duration: '3434', genre: 'stuff', created_at: '2011/06/18', id: '1'   
         }
      ]
    },
    fields:['duration', 'genre', 'created_at', 'title', 'id']
});

trackStore.load(
  function() { 
    Ext.create('Ext.grid.Panel', {
      title: 'Tracks',
      store: trackStore,
      stripeRows: false,
      columns: [
        { 
            header: 'Title', 
            dataIndex: 'title'

        },
        { header: 'Duration',  dataIndex: 'duration' },
        { header: 'Genre', dataIndex: 'genre' },
        { header: 'Created At', dataIndex: 'created_at'}
      ],
      height: 200,
      width: 475,
      renderTo: Ext.getBody(),
      listeners: {
        afterrender: function( )
        {
            view = this.getView();

            view.tip = Ext.create('Ext.tip.ToolTip', {
                target: view.el,
                delegate: view.itemSelector,
                trackMouse: true,
                renderTo: Ext.getBody(),
                listeners: {
                    beforeshow: function updateTipBody(tip) {
                        tip.update(this.data.title);
                    }
                }
            });
        }
    }

  });
});
});

Error

Uncaught TypeError: Cannot read property 'title' of undefined

Upvotes: 0

Views: 62

Answers (1)

Fabio Barros
Fabio Barros

Reputation: 1439

You could do it like this, use the event "viewready" instead "afterrender" :

listeners: {
    viewready: function(grid) {

        var view = this.getView();

        // record the current cellIndex
        grid.mon(view, {
            uievent: function(type, view, cell, recordIndex, cellIndex, e) {
                grid.cellIndex = cellIndex;
                grid.recordIndex = recordIndex;
            }
        });

        grid.tip = Ext.create('Ext.tip.ToolTip', {
            target: view.el,
            delegate: '.x-grid-cell',
            trackMouse: true,
            renderTo: Ext.getBody(),
            listeners: {
                beforeshow: function updateTipBody(tip) {
                    console.log(grid.cellIndex);
                    if (!Ext.isEmpty(grid.cellIndex) && grid.cellIndex !== -1) {
                        header = grid.headerCt.getGridColumns()[grid.cellIndex];
                        tip.update(grid.getStore().getAt(grid.recordIndex).get(header.dataIndex));
                    }
                }
            }
        });
    }
}

Upvotes: 2

Related Questions