Virajsinh

Select2 With Pagination Using CodeIgniter Updated

Dec 31st, 2021 (edited)
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2.     $pglm = $this->input->post("page_limit", TRUE);
  3.     $page_lim = (empty($pglm) ? 10 : $pglm);
  4.     $pg = $this->input->post("page", TRUE);
  5.     $page = (empty($pg) ? 0 : $pg);
  6.     $sira = $page * $page_lim;
  7.     $search_text = (empty($this->input->post("q", TRUE)) ? '' : $this->input->post("q", TRUE));
  8.  
  9.     $this->db->select('id');
  10.     $this->db->where('status', 'active');
  11.     $this->db->like('state_name', $search_text);
  12.     $this->db->from('tbl_state');
  13.     $cnt = $this->db->count_all_results();
  14.  
  15.     $this->db->select('state_id as id, state_code, state_name as text');
  16.     $this->db->where('status', 'active');
  17.     $this->db->like('state_name', $search_text);
  18.     $this->db->order_by('state_name', 'asc');
  19.     $query = $this->db->get('tbl_state', $page_lim, $sira);
  20.     $filteredValues = $query->result_array();
  21.  
  22.     header('Content-Type: application/json; charset=utf-8');
  23.     $array = array(
  24.         'items' => $filteredValues,
  25.         'total' => $cnt // Total rows without LIMIT on SQL query
  26.     );
  27.     echo json_encode($array, JSON_PRETTY_PRINT);
  28. ?>
  29.  
  30. <script type="text/javascript">
  31.     window.onload = (event) => {
  32.         headerParams = {'Authorization':'bearer t-7614f875-8423-4f20-a674-d7cf3096290e'};
  33.         $('#select2_state').select2({
  34.             placeholder:'Select State',
  35.             // minimumResultsForSearch: Infinity,
  36.              // minimumInputLength: 3,
  37.             //  maximumInputLength: 20,
  38.             //  minimumResultsForSearch: 20,
  39.             width:'100%',
  40.             ajax: {
  41.                 url: '<?php echo base_url('select2/get_state_list'); ?>',
  42.                 dataType: 'json',
  43.                 delay: 300,
  44.                 type:'POST',
  45.                 // headers: headerParams,
  46.                 beforeSend: function (xhr) {
  47.                     xhr.setRequestHeader('Authorization', 'TOKEN');
  48.                 },
  49.                 data: function (params) {
  50.                     return {
  51.                         q: params.term, // search term
  52.                         page_limit: 10,
  53.                         page: params.page
  54.                     };
  55.                 },
  56.                 processResults: function (data, params) {
  57.                     // console.log('===  Select2.php [51] ===', data.items);
  58.                     params.page = params.page || 1;
  59.                     return {
  60.                         results: data.items,
  61.                         pagination: {
  62.                             more: (params.page * 10) < data.total
  63.                         }
  64.                     };
  65.                 },
  66.                 cache: true
  67.             }
  68.         });
  69.     };
  70. </script>
Add Comment
Please, Sign In to add comment