简体   繁体   中英

Pagination in Ajax with select option as main doesn't work in codeigniter

This is my select option :

<select id="slc_action" class="form-control" style="margin:0;">
    <option value="0"> Select Activity</option>
    <?php foreach($admin_action as $row){
        echo "<option  value='" . $row['action_id'] . "''>" . $row["event"] . "</option>";
     }?>
</select>

And this is my jquery :

<script>
$(document).ready(function() {
    $("#slc_action").change(function(){
        var action_id = $("#slc_action").val();
        jQuery.ajax({
            type: 'POST',
            url: '/ADMIN/ajax/activity_list',
            dataType: 'html',
            data: {action_id: action_id
            },
            success: function(res) {
                if (res)
                {
                    $("#activity_list").html(res);
                }
            }
        });
    });
});
</script>

this is my controller for ajax call :

function activity_list(){

    $action_id = $this->input->get("action_id");
    $config = array();
    $config["base_url"] = "/ADMIN/Account/admin_activity/";
    $config["total_rows"] = $this->Ajax_m->m_count_activity($action_id);
    $config["per_page"] = 10;
    $config["uri_segment"] = 5;

    //config for bootstrap pagination class integration
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';

    $config['num_links'] = 10;

    $this->pagination->initialize($config);
    $page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
    $data["no"]=$page;
    $data["activity_list"] = $this->Ajax_m->m_get_activity($action_id,$config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();
    $this->load->view("/ADMIN/".country_code."/Admin_activity_list",$data);
}

this for ajax model :

function m_get_activity($action_id,$limit,$start){
    $this->db->limit($limit,$start);
    $this->db->select("admin_name,action_id,querylog,time,");
    $this->db->from("uhd_admin_activity");
    $this->db->where("action_id=",$action_id);
    $query = $this->db->get();
    return $query->result_array();
}

function m_count_activity($action_id){
    if($action_id != null) $this->db->where("action_id = '$action_id'" , NULL, FALSE);

    $this->db->from("uhd_admin_activity");

    return $this->db->count_all_results();
}

That code in my first view, than if i select the option, it will show the activity_list based on the selected.

I put the pagination on that ajax url , than if i clicked the pagination, all my activity_list will be gone, the selected option will be back to Select Activity.

guys do you know how to fix this?

Try the below way.

In your controller create pagination base_url with your activity_id

$action_id = $this->input->post("action_id"); //please use post method as your are posting data using POST in your ajax call
 $config = array();
 $config["base_url"] = "/ADMIN/Account/admin_activity/".$action_id;

now into your admin_activity function add argument for receiving action_id

function admin_activity($activity_id,$offset) {
    //here $offset will be your page segment no.
   //pass the activity argument along with your view data
   $data['post_activity_id] = $activity_id;
   $this->load->view('view_name',$data);
}

into your view

<select id="slc_action" class="form-control" style="margin:0;">
    <option value="0"> Select Activity</option>
    <?php foreach($admin_action as $row){
        if(isset($post_activity_id) && $post_activity_id == $row['action_id']) {
            echo "<option selected="selected" value='". $row['action_id'] . "''>" . $row["event"] . "</option>";
        } else {
            echo "<option value='" . $row['action_id'] . "''>" . $row["event"] . "</option>";
        }
     }?>
</select>

so, basically you just create your pagination with action_id and after receiving that id in your controller function send it back along with view data and check in your input controller if it set than make that value selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM