Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Using JSONReader

  1. #1

    Default Using JSONReader

    I'm a newbie to extjs and so far it looks great. I'm trying to integrate it with PHP but having some issues. The grid is defined as below and it tries to use a jsonreader to load data from the database
    PHP Code:
    var ds = new Ext.data.Store({
        
    proxy: new Ext.data.HttpProxy({
            
    url'dataload.php'        
        
    }),

        
    reader: new Ext.data.JsonReader({
            
    root'countries'            
        
    }, [
        {
    name'id'},
        {
    name'code'},
        {
    name'country'}
        ]),

        
    remoteSorttrue
    });
    ds.setDefaultSort('id''desc');

    var 
    countrygrid = new Ext.grid.GridPanel({
        
    storeds,
        
    columns: [
            {
    id:'id'header"id"width200sortabletruedataIndex'id'},
            {
    header"Code"width120sortabletruedataIndex'code'},
            {
    header"Country"width120sortabletruedataIndex'country'}        
        ],
        
    viewConfig: {
            
    forceFittrue
        
    },
        
    sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
        
    width:600,
        
    height:300,
        
    frame:true,
        
    title:'Countries',
        
    iconCls:'icon-grid'
        
    });
    countrygrid.render('country-grid');
    countrygrid.getSelectionModel().selectFirstRow(); 

    ds.loadData(); 
    the dataload.php file simply selects countries from the database and encodes it as follows

    PHP Code:
    $jsonEncodedCountries json_encode($arr);    
    header('Content-Type''text/javascript');
        
     echo 
    $jsonEncodedCountries
    Even after doing this i do not see the data displayed in the grid? Please let me know what i'm missing

  2. #2
    Ext User halkon_polako's Avatar
    Join Date
    Nov 2007
    Location
    Barcelona
    Posts
    193

    Default

    Use ds.load() instead of ds.loadData()

  3. #3

    Default

    this does not work either. Turning on Firebug to view the response shows that the response is null.
    POST infor shows
    PHP Code:
    dir DESC
    sort id 
    Header shows

    PHP Code:
    Date Sat03 May 2008 16:12:17 GMT
    ServerApache
    /2.0.63 (Win32
    PHP/5.2.5X-Powered-ByPHP/5.2.5
    ExpiresThu
    19 Nov 1981 08:52:00 GMT
    Cache
    -Controlno-storeno-cachemust-revalidatepost-check=0pre-check=0
    Pragmano
    -cache
    Content
    -Length4
    Keep
    -Alivetimeout=15max=85Connection
    Keep
    -AliveContent-
    Typetext/htmlRequest 
    HeadersHostlocalhost
    User
    -AgentMozilla/5.0 (WindowsUWindows NT 5.1en-GBrv:1.8.1.14Gecko/20080404 Firefox/2.0.0.14
    Accepttext
    /xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5Accept-Languageen-gb,en;q=0.5Accept-Encodinggzip,deflateAccept-CharsetISO-8859-1,utf-8;q=0.7,*;q=0.7Keep-Alive300Connectionkeep-aliveX-Requested-WithXMLHttpRequestContent-Typeapplication/x-www-form-urlencodedRefererhttp://localhost/seedcomputers/startpage.phpContent-Length16CookiePHPSESSID=92bcg8clomcc8nge2acvs4o914Pragmano-cacheCache-Controlno-cache 
    for completeness the dataload.php is

    PHP Code:
    $query1 "select * from countries";
        
    $resultmysql_query($query1$connGFriday) or die(mysql_error());
        
    $row mysql_fetch_array($result);
        
    $countries  = array();
        while (
    $row mysql_fetch_assoc($countries)) {
            
    $country = array();
            
    $country['id'] = $row['id'];
            
    $country['code'] = $row['code'];
            
    $country['country'] = $row['country'];
            
    $countries[] = $country;        
        }
        
    $jsonEncodedCountries json_encode($arr);    
        
    header('Content-Type''text/javascript');
        
        echo 
    $jsonEncodedCountries

  4. #4
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    dir DESC
    sort id
    That's the request.

    What is your PHP scrips sending back?

  5. #5

    Default

    it sends back a jsonencoded string made from an array of arrays. see below
    PHP Code:
    $jsonEncodedCountries json_encode($arr);    
        
    header('Content-Type''text/javascript');
        
        echo 
    $jsonEncodedCountries

  6. #6
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    yehbut what does that look like? From Firebug.

  7. #7
    Ext User halkon_polako's Avatar
    Join Date
    Nov 2007
    Location
    Barcelona
    Posts
    193

    Default

    I don't know PHP, but ...what is $arr ?
    Maybe you want to do :
    Code:
    $jsonEncodedCountries = json_encode($countries);
    Also, as animal says, what is the response showed in firebug?

  8. #8

    Default

    Firebug shows the following. This is only part of it as the string is quite long
    PHP Code:
    [["4"," AFG","Afghanistan"],["5"," ALB","Albania"],["6"," ALG","Algeria"],["7"," ASA","American Samoa"],["8"," ADR","Andorra"],["9"," ANG","Angola"],["10"," AGI","Anguilla"],["11"," ANT","Antarctica"]] 
    Do i need some sort of listner to get the data into the table after it's returned?

  9. #9
    Sencha User
    Join Date
    Jan 2008
    Location
    Toronto, Canada
    Posts
    481

    Default

    This is a multi-dimensional array. In order for JsonReader to work your server-side script has to return an array of object literals. Something like this:
    Code:
    [{id:"4",code:"AFG",country:"Afghanistan"},{id:"5",code:"ALB",country:"Albania"]}...]

  10. #10

    Default

    interestingly the way you create the array has an impact on the json encoding. using
    a = array(b=>'itemb',c=>'itemc') generates didfferent json encoding from a['b'] = "itemb";a['c']="itemc".

    None the less getting the encoding the eli mentioned did not work either, the json encoding is shown below. not usre if the quote around the identifiers make a difference. I haven't figured out a way to get them to disappear. Any other ideas?
    PHP Code:

    [{"id":"4","code":" AFG","country":"Afghanistan"},{"id":"5","code":" ALB","country":"Albania"

Page 1 of 2 12 LastLast

Posting Permissions

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