Results 1 to 1 of 1

Thread: data download function from Grid and Chart

  1. #1
    Sencha User mashiki's Avatar
    Join Date
    Oct 2007
    Location
    Japan
    Posts
    139
    Vote Rating
    0
      0  

    Default data download function from Grid and Chart

    Download is a function that mounts originally on the server side. However, the method that downloaded from Store on the javascript side was examined this time.

    The made sample is two of the following.
    Both originals are examples of ExtJS, and changed only a little(add function() and buttons).

    I think that the meaning of download from Store is large because the example of Chart displays the data that doesn't exist in the server.

    http://www123.ddo.jp/grid/array-grid.html


    http://www123.ddo.jp/grid/reload-chart.html


    I prepared the following php script that only output POSTed data because I was not able to achieve download only by the client side in the mechanism. I would like you to advise by the comment by all means if there is a method it is possible to do only with around here and javascript.

    The php script is enough by three lines. However, it is long for the purpose of the following.
    - The degree of freedom of the header is given.
    - REFFERER is checked as minimum security.

    PHP Code:
    <?php
        
    if (!isSet($_SERVER['HTTP_REFERER']) 
         || !
    preg_match('#^http://([^:/]+)#',$_SERVER['HTTP_REFERER'],$mch)
         || 
    $mch[1]!==$_SERVER['SERVER_NAME']
        ) die(
    'Security Error'); // simple security check
        
    foreach ($_POST as $header=>$val) {
            if (
    $header==='body') continue;
            
    header("$header$val");
        }
     die(
    $_POST['body']);
    ?>
    The function is the following.

    Code:
    	Ext.data.Store.prototype.download = function(fn){
    		var output=[],o=[],
    			fld = this.fields;
    		fld.eachKey(function(key){o.push(key)});
    		output.push(o.join('\t'));
    		this.each(function(rec) {
    			var o=[];
    			fld.eachKey(function(key){o.push(rec.get(key))});
    			output.push(o.join('\t'));
    		});
    		var form = Ext.DomHelper.append(document.body, {
    			tag: 'form',
    			style: 'display:none',
    			action: 'download.php',
    			method: 'post',
    			cn:[{
    				tag:'textarea',
    				name:'body',
    				html:output.join('\n')
    			},{
    				tag:'input',
    				name:'Content-Disposition',
    				value:'attachment;filename='+'&quot;'+fn+'&quot;'
    			},{
    				tag:'input',
    				name:'Content-Type',
    				value:'text/tab-separated-values'
    			}]
    		});
    		form.submit();
    		document.body.removeChild(form);
    	}
    This is a little dirty. Could anyone advise though I want to write smarter?

    Thank you.
    Last edited by mashiki; 2 Mar 2011 at 4:51 PM. Reason: Add image

Similar Threads

  1. Does the Ext.chart has a print function?
    By soulgen in forum Ext: Discussion
    Replies: 7
    Last Post: 28 Sep 2011, 12:40 PM
  2. How to download and save extjs-grid data in a txt-file
    By philipp_sidarion in forum Ext 3.x: Help & Discussion
    Replies: 1
    Last Post: 20 Apr 2011, 5:00 AM
  3. How to link grid data to a chart?
    By robertdbuckley in forum Ext 3.x: Help & Discussion
    Replies: 1
    Last Post: 25 Jan 2011, 10:43 AM
  4. setting grid data from a function.. how to refer
    By coooolmagic in forum Ext 3.x: Help & Discussion
    Replies: 4
    Last Post: 5 Nov 2010, 3:45 AM

Posting Permissions

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