Welcome Guest, Not a member yet? Register   Sign In
[Solved] Refresh table with Ajax
#1

[eluser]Unknown[/eluser]
Hello everybody Big Grin
First of all i want to give thanks to those who read this post and wanna say this framework its really amazing Big Grin.
well the thing is that im doing a little example of DB maintenance and im doing it with jquery. its not a big deal, i just done a loggin using ajax x) and everything work good.
but what i want to do now its, from my dataTable (the grid) delete some user and refresh just the dataTable and not the entire page. what i have achieve at this moment is to call the controller from the view and delete the record i selected. but i need to refresh the grid.
to List all user from db i used the following code:
Code:
public function userList($offeset = '') {
        $this->table->set_heading('User Id', 'First Name', 'Last Name', 'NickName', 'Registration Date', ' ');
        $template = array('table_open' => '<table border="0" cellpadding="1" cellspacing="0" class="stripeTable"');
        $this->table->set_caption('UserList (show a maximum # of ' . $this->limit . " per page)");
        $this->table->set_template($template);
        $config['base_url'] = site_url("userController/userList") . "/";
        $config['total_rows'] = $this->UserModel->getTotalUserCount();
        $config['first_link'] = 'Le First';
        $config['last_link'] = 'Le Last';
        $config['per_page'] = $this->limit;
        $config['full_tag_open'] = '<p class="pagination">';
        $config['full_tag_close'] = '</p>';
        $this->pagination->initialize($config);
        $data['userList'] = $this->UserModel->getUserList($config['per_page'], $this->uri->segment(3));
        $this->load->view('viewTest/userView', $data);
    }

i put an extra column with blank spaces to add the delete imagen button.
and the view its:

Code:
&lt;html&gt;

    &lt;head&gt;
        <scrip/t type="text/javascript" src="&lt;?= base_url("js/jquery.js"); ?&gt;"></scrip/t>
        <scrip/t type="text/javascript" src="&lt;?= base_url('js/jquery-ui.min.js'); ?&gt;"></scrip/t>        
        &lt;link rel="stylesheet" href="&lt;?= base_url("css/main.css"); ?&gt;" /&gt;
        &lt;link rel="stylesheet" href="&lt;?= base_url('css/smoothness/jquery-ui-1.8.16.custom.css'); ?&gt;"&gt;&lt;/link>
        <scrip/t type="text/javascript" >
            $(document).ready(function(){
                $('.deleteLink').live('click', function(e){
                    e.preventDefault();
                    var thisLink = $(this);
                    var doDeleteUserMethodURL = "&lt;?= site_url('userController/deleteUserWithId'); ?&gt;";
                    var cssFolder = "&lt;?= base_url('css'); ?&gt;";
                    doDeleteUserMethodURL = doDeleteUserMethodURL +"/"+ thisLink.data('user_id');
                    var response = confirm("Are you sure you want to delete the selected user?");
                    if (response) {
                        $.ajax({
                            url: doDeleteUserMethodURL,
                            dataType: 'json',
                            success: function(data) {
                                $(".userDialog")
                                .html(data.message)
                                .css('background', 'url('+cssFolder+'/'+data.background+') no-repeat right')
                                .dialog({
                                    autoOpen: true,
                                    show: "explode",
                                    hide: "hide"
                                })
                            }
                        });
                    }
                });
            });
        </scrip/t>
        &lt;style type="text/css" &gt;
            .userDialog {
                display: none;
            }
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <div id="divUserList">
            &lt;?php
            foreach ($userList->result() as $user) {
                $this->table->add_row($user->id, $user->first_name, $user->last_name, $user->nickname, $user->registration_date,
                        '<aa hre/f="#" data-user_id="' . $user-&gt;id . '" class="deleteLink"><img class="imageLink" src="' . base_url(" /></a>');
            }
            echo $this->table->generate();
            echo $this->pagination->create_links();
            ?&gt;
        </div>
        <div class="userDialog"></div>
    &lt;/body&gt;
&lt;/html&gt;

to delete the user i use this method from controller:
Code:
public function deleteUserWithId($userId) {
        $deleteUser = $this->UserModel->deleteUser($userId);
        if ($deleteUser) {  # user was deleted
            $message = 'user Deleted successfully';
            $status = 'ok';
            $background = 'images/trashCan.png';
        } else {    # something went wrong
            $message = 'user couldnt be deleted, something went wrong';
            $status = 'error';
            $background = 'images/error.png';
        }
        $returnedData = array(
            'message' => $message,
            'status' => $status,
            'background' => $background
        );
        $json_encode = json_encode($returnedData);
        echo $json_encode;
    }

with all these code, the user selected is deleted from databse, but i dont know how to refresh the just the table.
i tried to do this with jquery but ir didnt work:
Code:
thisLink.parent('tr').hide('slow');
i hope some of you could help me. anyway i will continue searching and if i find a way to achieve this i'll post it.





Theme © iAndrew 2016 - Forum software by © MyBB