Search code examples
phpjqueryzend-frameworkmodel-view-controllerjquery-jtable

How use jtable in PHP mvc1 (zend framework)?


I got jtable from jtable.org.

When I use jtable in simple PHP projects it works fine, but when I change it to PHP mvc it returned the following error and it didn't show any data in jtable.

An error occured while communicating to the server. but in the chrom console it print JSON:

{
 "Result":"OK",
 "TotalRecordCount":"20",
 "Records":[
            {"PersonId":"1","0":"1","Name":"Benjamin Button","1":"Benjamin Button","Age":"17","2":"17","RecordDate":"2011-12-27 00:00:00","3":"2011-12-27 00:00:00"},
            {"PersonId":"12","0":"12","Name":"damir","1":"damir","Age":"27","2":"27","RecordDate":"2015-09-17 12:17:53","3":"2015-09-17 12:17:53"}
           ]
}

That shows every thing is correct, but jtable didn't show anything.

What am I doing wrong?

In adminlayout.phtml I use other jQuery functions in $(document).ready but when I deleted them it didn't work either.

$(document).ready(function () {

//Prepare jTable
$('#PeopleTableContainer').jtable({
    title: 'Table of people',
    paging: true,
    pageSize: 2,
    sorting: true,
    defaultSorting: 'Name',
    actions: {
        listAction: root_url+'admin/index/personpagedsorted?action=list',
        },
    fields: {
        PersonId: {
            key: true,
            create: false,
            edit: false,
            list: false
        },
        Name: {
            title: 'Author Name',
            width: '40%'
        },
        Age: {
            title: 'Age',
            width: '20%'
        },
        RecordDate: {
            title: 'Record date',
            width: '30%',
            type: 'date',
            create: false,
            edit: false
        }

    }

});

//Load person list from server

$('#PeopleTableContainer').jtable('load');

in controller: I only use list action.

public function personpagedsortedAction()
{

     if ($_GET["action"]=="list")
    {
        $dbh=Zend_Registry::get('db');
        $sth = $dbh->prepare("SELECT COUNT(*) AS RecordCount FROM people;");
        $sth->execute();
        $row = $sth->fetch(PDO::FETCH_BOTH);

         $recordCount = $row['RecordCount'];
        $jtSorting=$_GET["jtSorting"];
       $jtStartIndex= $_GET["jtStartIndex"];
      $jtPageSize= $_GET["jtPageSize"];
        //Get records from database

     //  $result =$db->personselectinput($jtSorting, $jtStartIndex, $jtPageSize);

       $str_sql="SELECT * FROM people ORDER BY " . $jtSorting . " LIMIT " . $jtStartIndex . "," . $jtPageSize . ";";
       // $res=$db->query($str_sql);
       // $res=$db->fetchAll($str_sql);

       $sth = $dbh->prepare($str_sql);
       $sth->execute();

        //Add all records to an array
        $rows = array();

       while ( $result = $sth->fetch(PDO::FETCH_BOTH))
       {
           $rows[]=$result;
       }

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $recordCount;
        $jTableResult['Records'] = $rows;
     // i change print to echo and return but it didnt work too

      print json_encode($jTableResult);

    }
}

in edittest.phtml: I wrote

<div id="PeopleTableContainer"></div>

that show the table without any data


Solution

  • I found the answer I disabled the layout of my action in controller and everything works great because when json returns it returns with the master layout and the response wasn't correct when i disabled the layout by adding new layout without any html tag it works.