Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $pglm = $this->input->post("page_limit", TRUE);
- $page_lim = (empty($pglm) ? 10 : $pglm);
- $pg = $this->input->post("page", TRUE);
- $page = (empty($pg) ? 0 : $pg);
- $sira = $page * $page_lim;
- $search_text = (empty($this->input->post("q", TRUE)) ? '' : $this->input->post("q", TRUE));
- $this->db->select('id');
- $this->db->where('status', 'active');
- $this->db->like('state_name', $search_text);
- $this->db->from('tbl_state');
- $cnt = $this->db->count_all_results();
- $this->db->select('state_id as id, state_code, state_name as text');
- $this->db->where('status', 'active');
- $this->db->like('state_name', $search_text);
- $this->db->order_by('state_name', 'asc');
- $query = $this->db->get('tbl_state', $page_lim, $sira);
- $filteredValues = $query->result_array();
- header('Content-Type: application/json; charset=utf-8');
- $array = array(
- 'items' => $filteredValues,
- 'total' => $cnt // Total rows without LIMIT on SQL query
- );
- echo json_encode($array, JSON_PRETTY_PRINT);
- ?>
- <script type="text/javascript">
- window.onload = (event) => {
- headerParams = {'Authorization':'bearer t-7614f875-8423-4f20-a674-d7cf3096290e'};
- $('#select2_state').select2({
- placeholder:'Select State',
- // minimumResultsForSearch: Infinity,
- // minimumInputLength: 3,
- // maximumInputLength: 20,
- // minimumResultsForSearch: 20,
- width:'100%',
- ajax: {
- url: '<?php echo base_url('select2/get_state_list'); ?>',
- dataType: 'json',
- delay: 300,
- type:'POST',
- // headers: headerParams,
- beforeSend: function (xhr) {
- xhr.setRequestHeader('Authorization', 'TOKEN');
- },
- data: function (params) {
- return {
- q: params.term, // search term
- page_limit: 10,
- page: params.page
- };
- },
- processResults: function (data, params) {
- // console.log('=== Select2.php [51] ===', data.items);
- params.page = params.page || 1;
- return {
- results: data.items,
- pagination: {
- more: (params.page * 10) < data.total
- }
- };
- },
- cache: true
- }
- });
- };
- </script>
Add Comment
Please, Sign In to add comment