Server-Side Pagination involving multiple tables Code Igniter

Server-Side Pagination involving multiple tables Code Igniter

syedmdanialsyedmdanial Posts: 4Questions: 2Answers: 0

Hi guys, I'm rather new in the programming world. Trying yo figure out how to use the server-side pagination. I've used DataTables before but only the normal, client-side pagination way. When I tried making it into a server-side, I don't know how to make it work. Here is how I get the data from the db using the model:-

        public function getAllProjects(){
              $allProjects = json_decode(json_encode($this->db->query("SELECT project_info.project_name,
             project_info.project_id,
             project_info.screenshot,
             project_info.user_id,
             project_info.project_index_page,
             project_info.last_updated_timestamp,
             project_info.analytics_id,
             project_template.project_template_id
             FROM project_info  
             INNER JOIN project_template ON project_info.project_id = project_template.project_id
             WHERE project_info.is_deleted = 0")->result()), true);
             if($allProjects){
                 return $allProjects; 
             }
            else{
                 return false;
             }
         }

The this is how my controller looks like:-

      public function index(){  
               $data["title"] = "Codeigniter Ajax CRUD with Data Tables and Bootstrap Modals";  
               $this->load->model('crud_model');
               $projectsData  = $this->crud_model->getAllProjects();
               echo json_encode($projectsData);
      } 

Currently getting this as the result:-

[{"project_name":"aa","project_id":"202","screenshot":"1","user_id":"34","project_index_page":"283","last_updated_timestamp":"0000-00-00 00:00:00","analytics_id":null,"project_template_id":"283"},{"project_name":"aa","project_id":"203","screenshot":"1","user_id":"34","project_index_page":"284","last_updated_timestamp":"0000-00-00 00:00:00","analytics_id":null,"project_template_id":"284"}]

This is how my view looks like:-

     $(document).ready(function ()
      {
           $('#projectsTB').DataTable( {
                "processing": true,
                "serverSide": true,
                "ajax": {
                "url":"<?php echo base_url(); ?>crud/index",
                "type": "POST"
                }

           });
      });

I know that the data needs to be structured like this for the DataTables to understand:-

     {
      "draw": 1,
      "recordsTotal": 57,
      "recordsFiltered": 57,
      "data": [
                     [
                        "Airi",
                        "Satou",
                        "Accountant",
                        "Tokyo",
                        "28th Nov 08",
                        "$162,700"
                     ],
                     [
                        "Angelica",
                        "Ramos",
                        "Chief Executive Officer (CEO)",
                        "London",
                        "9th Oct 09",
                        "$1,200,000"
                     ]
                 ]
       }

but i don't know how to do so.
Can anyone help me with this and provide me any insight or what modifications i can do?

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @syedmdanial ,

    This page here talks about server-side processing and what you need to do. This here should also help. If you download the DataTables repo, there's examples of the server-side scripts in /examples/server_side/scripts,

    Hope that helps,

    Cheers,

    Colin

This discussion has been closed.