PDA

View Full Version : [SOLVED] Browse csv and import to grid



verbi
3 Nov 2010, 4:22 AM
How to browse csv file and import to a grid?

like in
http://www.sencha.com/forum/showthread.php?106945-Ext.ux.CSVImport-import-data-from-CSV-to-Ext.grid.GridPanel&highlight=csv, but not with drag-drop.

Thankyou.

verbi
22 Dec 2010, 8:36 AM
something like :

var store = new Ext.data.JsonStore
({
url: 'parser.php',
fields: ['0', '1','2','3','4','5']
});

store.load();

var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{ width: 75, sortable: true, dataIndex: '0'},
{ width: 75, sortable: true, dataIndex: '1'},
{ width: 75, sortable: true, dataIndex: '2'},
{ width: 75, sortable: true, dataIndex: '3'},
{ width: 75, sortable: true, dataIndex: '4'},
{ width: 75, sortable: true, dataIndex: '5'}


],
stripeRows: true,
height: 350,
width: 800,
title: 'CSV'
});

grid.render(document.body);

parser.php

$filecsv='file1.csv';
define('DATA_FILE','data.json'); // must be writable

$fh = fopen($filecsv,'r');
$rows = array();
while($rows[] = fgetcsv($fh)) { }
$json = json_encode($rows);
file_put_contents(DATA_FILE,$json);
echo $json;
fclose($fh);