1. #1
    Sencha User
    Join Date
    Nov 2011
    Posts
    25
    Vote Rating
    0
    danielleng is on a distinguished road

      0  

    Default Paging Grid with PHP backend

    Paging Grid with PHP backend


    Hey guys, I'm trying to implement a paging Ext.grid.Panel in my application.
    I have successfully posted [Object { page=1, start=0, limit=25}] to my PHP function, and queried the correct results. But I am still unable to get the paging to work.

    In the Ext.toolbar.Paging documentation it says that the server response must be in the following format:
    http://docs.sencha.com/ext-js/4-0/#!/api/Ext.toolbar.Paging
    Code:
    {     "success": true,     "results": 2000,     "rows": [ // ***Note:** this must be an Array         { "id":  1, "name": "Bill", "occupation": "Gardener" },         { "id":  2, "name":  "Ben", "occupation": "Horticulturalist" },         ...         { "id": 25, "name":  "Sue", "occupation": "Botanist" }     ] }
    However my server response is returned as:
    {"type":"rpc","tid":4,"action":"QueryEmployee","method":"readEmployee",
    "result":[{"emp_id":1,"emp_name":"Daniel Leng","emp_staff_no":".......
    Here is my PHP code...
    Code:
        public function readEmployee(stdClass $params)
        {
            $_db = $this->__construct();
            $start = $params->start;
            $limit = $params->limit;
            
            $results = array();
            
            if ($stmt = $_db->prepare(
                "SELECT " .
                "emp_id," .
                "emp_name," .
                "emp_staff_no," .
                "emp_email," .
                "emp_position," .
                "emp_datejoined," .
                "emp_username " .
                "FROM employee LIMIT ?, ?;")) {
                $stmt->bind_param('ii', $start, $limit);
                $stmt->execute();
                
                $result = $stmt->get_result();
                while ($row = $result->fetch_array(MYSQL_ASSOC))
                {
                    array_push($results, $row);
                }
                $stmt->close();
            }
            return $results;
        }

    How do I get my PHP function to return a response like the one mentioned above?

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,547
    Vote Rating
    872
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    To have paging work, it needs to have the total number of results
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Nov 2011
    Posts
    25
    Vote Rating
    0
    danielleng is on a distinguished road

      0  

    Default


    Yes I am well aware of that... I could rewrite my PHP code to query the total number of results first, and then only query for the results. But how do I send the Total Number Of Results back to the client ?

    {"type":"rpc","tid":4,"action":"QueryEmployee","method":"readEmployee",
    "result":[{"emp_id":1,"emp_name":"Daniel Leng","emp_staff_no":".......
    My server response is always in this format... how do I add an extra parameter for the Total Number Of Results?

  4. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,547
    Vote Rating
    872
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    It needs to be a level above the data:

    Code:
    {"type":"rpc","tid":4,"action":"QueryEmployee","method":"readEmployee", total : 100,
    "result":[{"emp_id":1,"emp_name":"Daniel Leng","emp_staff_no":".......
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  5. #5
    Sencha User
    Join Date
    Nov 2011
    Posts
    25
    Vote Rating
    0
    danielleng is on a distinguished road

      0  

    Default


    I managed to modify Router.php and got my JSON format returned like this:

    {"type":"rpc","tid":4,"action":"QueryEmployee",
    "method":"readEmployee",
    "results":"22",
    "result":[{"emp_id":1,"emp_name":"Daniel Leng"
    But i still can't get the paging toolbar to show the number of results.
    I called store.load() with the following parameters:

    Code:
    this.store.load({
                   Params: 
                   {
                       start: 0, 
                       limit: 20 
                   }
    });
    My PHP function correctly calls for 20 records. But the total number of records in actually 22... so I expect the paging toolbar to show
    "Showing records 1- 20 of 22"
    But it shows
    "Showing recods 1-20 of 20"
    It doens't know that there is more than 20 records.
    And since it doesn't know that there is more than 20 records, I can't get it to page.

  6. #6
    Sencha User
    Join Date
    Nov 2011
    Posts
    25
    Vote Rating
    0
    danielleng is on a distinguished road

      0  

    Default


    I still need help in this~

  7. #7
    Sencha User
    Join Date
    Jul 2011
    Location
    Belgrade / Serbia
    Posts
    6
    Vote Rating
    0
    stbeli is on a distinguished road

      0  

    Default


    Try format
    Code:
    "results":22
    insted of
    Code:
     "results":"22"
    Remove quotes from numbers.

  8. #8
    Sencha User
    Join Date
    Aug 2011
    Posts
    53
    Vote Rating
    1
    Erikvip is on a distinguished road

      0  

    Default


    have you specified the 'totalProperty' in your Direct Store? It defaults to 'total', so this will not work until you've set totalProperty: 'results' in your direct store.

    By default the name is 'total'. You could also just change the results key to 'total' instead of 'results'.

    You should not need to modify router.php to get this working - I use remote paging w/ direct and it works fine.

    See:
    http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Reader-cfg-totalProperty

  9. #9
    Sencha User
    Join Date
    Nov 2011
    Posts
    25
    Vote Rating
    0
    danielleng is on a distinguished road

      0  

    Default


    Tried it, casted the value to an integer. Still not getting it to page.

    {"type":"rpc",
    "tid":4,
    "action":"QueryEmployee",
    "method":"readEmployee",
    "results":1000,
    "result":[{"emp_id":1,"emp_name":"Daniel Leng","emp_staff_no":"8888"....
    Any other tips ? This is just strange.

  10. #10
    Sencha User
    Join Date
    Nov 2011
    Posts
    25
    Vote Rating
    0
    danielleng is on a distinguished road

      0  

    Default


    Hello Erikvp thank you for your reply. I missed your post earlier.

    Well I'm not using ajax requests so I thought readers would be useless for me... You need to specify a reader in order to set the totalProperty config right ? This is how I specify my proxy in my Store:
    Code:
    proxy: {
            type: 'direct',
            api: {
                //client-side function : server-side function.
                create: QueryEmployee.createEmployee,
                read: QueryEmployee.readEmployee,
                destroy: QueryEmployee.destroyEmployee
            }
    }
    Where should I be adding the reader ?

    The reason why I had to modify my router.php is because its not returning the JSON in the format that is required by the paging toolbar. It does not return "total" and "success", for that matter.