Results 1 to 2 of 2

Thread: Grid Clipboard override - Include Headers

  1. #1
    Ext User
    Join Date
    May 2007
    Posts
    6
    Vote Rating
    1
      1  

    Default Grid Clipboard override - Include Headers

    I have created an override for the grid clipboard that includes the column headers with the copied text. This way, when you paste the values from grid to something like Excel, you can see what the values actually represent. Maybe someone else might find it useful.

    To use, create:
    ClipboardExt.js (I saved it in the overrides folder)
    Code:
    Ext.define('YourAppName.override.ClipboardExt', {
        extend: 'Ext.grid.plugin.Clipboard',
    
        alias: 'plugin.clipboardext',
        
        getCellData: function (format, erase) {
        	//debugger;
            var cmp = this.getCmp(),
                selModel = cmp.getSelectionModel(),
                ret = [],
                isRaw = format === 'raw',
                isText = format === 'text',
                viewNode,
                cell, data, dataIndex, lastRecord, record, row, view,
                hI, headerIndex = [], headerText = [], headerAdded = false;
    
            selModel.getSelected().eachCell(function (cellContext) {
                view = cellContext.column.getView();
                record = cellContext.record;
    
                if (lastRecord !== record) {
                    lastRecord = record;
                    if(cmp.includeHeaders && !headerAdded){
                    	ret.push(headerText);
                    	headerAdded=true;
                	}
                    ret.push(row = []);
                }
                
                dataIndex = cellContext.column.dataIndex;
                if(cmp.includeHeaders){
    	            hI = headerIndex.indexOf(dataIndex);
    	            if(hI==-1){
    	            	headerIndex.push(dataIndex);
    	            	headerText.push(cellContext.column.text.replace(" ",""));
    	            } else {
    	            	headerText[hI] =cellContext.column.text.replace(" ",""); 
    	            }            	
                }
    
                if (isRaw) {
                    data = record.data[dataIndex];
                } else {
                    // Try to access the view node.
                    viewNode = view.all.item(cellContext.rowIdx);
                    // If we could not, it's because it's outside of the rendered block - recreate it.
                    if (!viewNode) {
                        viewNode = Ext.fly(view.createRowElement(record, cellContext.rowIdx));
                    }
                    cell = viewNode.down(cellContext.column.getCellInnerSelector());
                    data = cell.dom.innerHTML;
                    if (isText) {
                        data = Ext.util.Format.stripTags(data);
                    }
                }
    
                row.push(data);
    
                if (erase && dataIndex) {
                    record.set(dataIndex, null);
                }
            });
    
            return Ext.util.TSV.encode(ret);
    
        },
    
        getCells: function (format, erase) {
        	console.log("getCells");
            var cmp = this.getCmp(),
                selModel = cmp.getSelectionModel(),
                ret = [],
                dataIndex, lastRecord, record, row;
    
            selModel.getSelected().eachCell(function (cellContext) {
                record = cellContext.record;
                if (lastRecord !== record) {
                    lastRecord = record;
                    ret.push(row = {
                        model: record.self,
                        fields: []
                    });
                }
    
                dataIndex = cellContext.column.dataIndex;
    
                row.fields.push({
                    name: dataIndex,
                    value: record.data[dataIndex]
                });
    
                if (erase && dataIndex) {
                    record.set(dataIndex, null);
                }
            });
    
            return ret;
        },
    
        getTextData: function (format, erase) {
            return this.getCellData(format, erase);
        }
        
    });
    Then in the view, add:
    Code:
    requires: ['YourAppName.override.ClipboardExt"]
    and in the grid object in the view:
    Code:
    plugins: ['clipboardext'],
    includeHeaders: true

  2. #2
    Sencha Premium User
    Join Date
    Feb 2017
    Posts
    3
    Vote Rating
    0
      0  

    Default

    Hi htbeef,

    This plugin is very useful. +1

Similar Threads

  1. Replies: 0
    Last Post: 24 Apr 2015, 9:54 AM
  2. Replies: 3
    Last Post: 3 Dec 2013, 3:47 PM
  3. Theme override include order
    By cnelissen in forum Sencha Cmd
    Replies: 2
    Last Post: 16 Jun 2013, 9:49 PM
  4. Replies: 6
    Last Post: 27 May 2007, 2:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •