PDA

View Full Version : get folder items



elona
1 Jul 2009, 1:59 AM
hi ,

i'm having some troubles with getting folder items. i can't realize what i'm missing/doing wrong.
here is what i have
js file


var folderItems = new Ext.data.JsonStore({
autoLoad:true,
url:'util.php',
method:'POST',
baseParams:{task:'getFolderList'},
root: 'dirList'
});
var fp = new Ext.FormPanel({
width: 300,
frame: true,
title: 'import',

bodyStyle: 'padding: 10px 10px 0 10px;',
labelWidth: 80,
defaults: {
anchor: '95%',
allowBlank: false,
msgTarget: 'side'
},
items: [{
xtype: 'combo',
fieldLabel: 'Nome tabella',
name:'tableName',
store:['studenti', 'test']
},{
xtype: 'combo',
id: 'filePath',
emptyText: 'Select a file',
fieldLabel: 'File',
name: 'filePath',
buttonCfg: {
text: '',
iconCls: 'upload-icon'
},
store: folderItems
}],.....


the php file


function readFolder(){
$dir = 'uploads/';
$folder=dir($dir);
$listFiles = array();
while($folderEntry=$folder->read()){
array_push($listFiles, $folderEntry);
}
$folder->close();
$listFiles = implode($listFiles,', ' );
//return $listFiles;
if (version_compare(PHP_VERSION,"5.2","<"))
{
require_once("JSON.php"); //if php<5.2 need JSON class
$json = new Services_JSON();//instantiate new json object
$data=$json->encode($listFiles); //encode the data in json format
} else
{
$data = json_encode($listFiles); //encode the data in json format
}

$cb = isset($_GET['callback']) ? $_GET['callback'] : '';

echo $cb . '({"dirList":"' . $data. '"})';
}


i hope sombedy can help cause i'ts drivin me crazy

thnx

Condor
1 Jul 2009, 2:00 AM
I'm not seeing a 'fields' config option in the JsonStore and no 'displayField' config option in the combobox.

elona
1 Jul 2009, 4:20 AM
I'm not seeing a 'fields' config option in the JsonStore and no 'displayField' config option in the combobox.

you are right, but i'm not sure how to get the fields

here is what i get back from the function

({"dirList":""., .., Array Grid Demo printing.pdf, Array Grid Example.pdf, email_list.txt, export_test.csv, _notes""})

can u give me any hint on how to proceed?

thnx

Condor
1 Jul 2009, 4:57 AM
That's not valid JSON! Shouldn't it be encoding the dirList as a JSON array?

elona
1 Jul 2009, 7:00 AM
thnx Condor x the reply.
i can't find the way how to insert the field name in json encode. until i came up with this:

$obj_result=json_encode($listFiles);
$cb = isset($_GET['callback']) ? $_GET['callback'] : '';
echo $cb . '({"dirList":"' . $obj_result. '"})';and the result is this:

{"dirList":"["Array Grid Example.pdf","export_test.csv","_notes"]"}). i know that in order to have the fields and displayField it should be in this form:

"dirList":[{"filename":"Array Grid Example.pdf"}, {"filename":"export_test.csv"}] so that the filename will be in the field and in displayField.
i've been googling and can't find the right way to do it.
can someone help?

Foggy
1 Jul 2009, 7:03 AM
Don't do the implode bevore you encode your array to the json format...

Condor
1 Jul 2009, 7:22 AM
i know that in order to have the fields and displayField it should be in this form:

"dirList":[{"filename":"Array Grid Example.pdf"}, {"filename":"export_test.csv"}]

This would also be valid:

{"dirList":["Array Grid Example.pdf","export_test.csv"]}
but you need a convert function in the field config.

ps. Sorry, I'm not a PHP programmer, so I can't help you there...

makana
1 Jul 2009, 10:41 AM
Like Condor said, you need a fields config in JsonStore and displayField in ComboBox.
Just like this:


var folderItems = new Ext.data.JsonStore({
autoLoad:true,
url:'util.php',
method:'POST',
baseParams:{task:'getFolderList'},
root: 'dirList',
fields: ['filename']
});
var fp = new Ext.form.FormPanel({
...
},{
xtype: 'combo',
id: 'filePath',
emptyText: 'Select a file',
fieldLabel: 'File',
name: 'filePath',
buttonCfg: {
text: '',
iconCls: 'upload-icon'
},
store: folderItems,
displayField: 'filename',
valueField: 'filename'
}
...
});


And create your php-json like this. Like Foggy said, don't use implode or things like that.
Do:


$filesList = array(
array(
'filename' => 'Array Grid Example.pdf'
),
array(
'filename' => 'export_test.csv'
),
...
);
echo '{"dirList":'.json_encode($filesList).'}';

Lukman
1 Jul 2009, 6:20 PM
Do:


$filesList = array(
array(
'filename' => 'Array Grid Example.pdf'
),
array(
'filename' => 'export_test.csv'
),
...
);
echo '{"dirList":'.json_encode($filesList).'}';

I wonder why all you guys didn't do:


$dirList = array(
'dirList' => array(
array(
'filename' => 'Array Grid Example.pdf'
),
array(
'filename' => 'export_test.csv'
),
...
)
);
echo json_encode($dirList);
Just let the json_encode do all the dirty work. ;)

makana
1 Jul 2009, 10:53 PM
Thanks for your hint! You're absolutely right!
That's a real vice ;) If you always do so, you don't see, that there is an easier way...
But it has one advantage: you always see on your last line, how the json will look like ;)

elona
1 Jul 2009, 11:54 PM
thank you all..
i'm still not getting it.:((

i'm keeping it really simple

<?php
include('JSON.php');
$json = new Services_JSON();
$dirlist = array(
'dirlist' => array(
array(
'filename' => 'Array Grid Example.pdf'
),
array(
'filename' => 'export_test.csv'
)
)
);
echo $data=$json->encode($dirlist);
?> the response is correct:
{"dirlist":[{"filename":"Array Grid Example.pdf"},{"filename":"export_test.csv"}]}

var folderItems = new Ext.data.JsonStore({
autoLoad:true,
url:'getFolderList.php',
method:'POST',
root: 'dirlist',
fields: ['filename']
});

{
xtype: 'combo',
id: 'filePath',
emptyText: 'Select a filename',
fieldLabel: 'File',
name: 'fff',
store: folderItems,
displayField:'filename',
valueField:'filename'
}

it MUST work but it doesn't :((

Condor
2 Jul 2009, 12:01 AM
Are you sure the response is correct?

Did you try adding a loadexception listener to the store to check if it fires?

ps. You don't need a valueField in the combobox.

Lukman
2 Jul 2009, 12:14 AM
What does

echo $data=$json->encode($dirlist);
suppose to mean? You are echoing the result of a variable assignment!

I thought it's crystal clear the code should be:

echo json_encode($dirlist);
or

$data = json_encode($dirlist);
echo $data;

elona
2 Jul 2009, 1:19 AM
omg..i'm so dumb..i hadn't specified the mode in the combo box. mode:'local'

and thnx condor, u were right. i checked it in firebug and the response was incorrect (syntax error, "invalid label".). now i got the response right

thank you all guys :D

elona
2 Jul 2009, 9:20 AM
so..i'm back

again with json... now i have a fileuploadfield and want to check if the filename already exists. so how can i use the the previus folderItems store to check filename existance?:-?


{
xtype: 'fileuploadfield',
id: 'formFile',
emptyText: 'Select a file',
fieldLabel: 'File',
name: 'filePath',
validator : function(){
if(this.value.trim().length != 0 && (this.value != folderItems)){
Ext.Msg.confirm('Messaggio','Filename already exists. Select new???', function(yesno) {
if (yesno == 'yes') {
Ext.getCmp('fn').enable();
}
});
}}
}

makana
2 Jul 2009, 10:17 AM
check out http://extjs.com/deploy/ext-3.0-rc3/docs/?class=Ext.data.Store&member=findExact

elona
2 Jul 2009, 1:17 PM
so it i'm trying the following way:


function getFilenames() {
var count = filenames.getTotalCount();
var res = new Array();
var tmp;
for (var i=0; i++; i <count){
tmp = filenames.getAt(i);
tmp = tmp.get('filename');
res.push(tmp);
}var t = filenames.getAt(0); t = t.get('filename');
return res;}
i guess the problem is with getTotalCount method which returns a NaN value. if i try just
tmp = filenames.getAt(0);
tmp = tmp.get('filename'); it works fine

makana
2 Jul 2009, 1:34 PM
The problem is, that your for-syntax isn't correct.
it has to be

for (var i = 0; i < count; i++) {...}

but what do you want to do with your getFilenames-method?
Didn't you want to check whether the value of a combo is in the values of the store?
Like this?

validator : function(){
if(this.value.trim().length != 0 && folderItems.findExact('filename', this.value) > -1){
Ext.Msg.confirm('Messaggio','Filename already exists. Select new???', function(yesno) {
if (yesno == 'yes') {
Ext.getCmp('fn').enable();
}
});
}
}

elona
2 Jul 2009, 10:52 PM
thnx. it works your way :)