我可以从ajax响应加载CodeIgniter视图并使用$ data obj吗?

时间:2012-12-21 13:55:24

标签: php ajax codeigniter

好吧,所以我加载的视图使用从控制器接收的数据。然后我使用jquery .change监听器来监听select-option列表中的更改。然后,它将触发一个函数运行,并且基本上ajax请求将被发送到我在控制器中的函数。在该函数中,我需要尝试“重新加载”通过控制器索引加载的数据,但是使用基于下拉列表值的新参数。截至目前,带数据的初始页面加载工作正常。 jquery .change和ajax请求也正常工作,但是,控制器功能没有重置页面数据。这就是我的......任何帮助都会非常感激。

jQuery(在我的members_home.php视图中):

$('#folder_select').change(function() {
    var data = $('#folder_select').val();
    loadTables(data);
});

function loadTables(str) {
    console.log("dfgdfg");
    $.ajax({
            url: '<?php echo base_url().'account/members_home/folderSelector';?>', 
            type: 'POST',
            data: { folderCat: str },
            success: function(output_string){

            }
    }); 
}

Members_home.php查看(html):

<table cellspacing="0" cellpadding="0">
    <tr>
        <th width="50%">File Name:</th>
        <th width="30%">File Size:</th>
        <th>Download Link:</th>
    </tr>

    <?php
    $c = true;
    foreach((array)$things as $files){
         if (!empty($files) > 0) {
             if ($files['size'] >= 1000000000) {
                $files['size'] = round( ($files['size'] / 1000000000) , 2).' GB';
             }
             else if ($files['size'] >= 1000000) {
                    $files['size'] = round( ($files['size'] / 1000000) , 2).' MB';
             }else{
                $files['size'] = round( ($files['size'] / 1000) , 2).' KB';
             }

            echo '<tr'.(($c = !$c)?' class="odd"':'').">
                <td>$files[name]</td>
                <td>$files[size]</td>
                <td><a href='$files[location]' >Download</a></td>
            </tr>";
         }else {
            echo '<tr><td colspan="3">Folder is empty.</td></tr>';
             break;
         }
    } ?>

 </table><br />

Members_home Controller:

function index()
{
    $is_logged_in = $this->loggedin->loggedin();
    if(!$is_logged_in)
    {
        redirect('account/createaccount', 'refresh');
    }
    else
    {   
        $q = $this->account_model->folder();
        $data['folder'] = $q;

        // This part gets the data that's used in the html table
        $userID = $this->session->userdata('id');
        $data['things'] = $this->account_model->folder_Homepage($userID);

        $data['main_content'] = 'account/members_home';
        $this->load->view('includes/template2', $data);
    }
}

// This function is used during the ajax request
public function folderSelector()
{
    $fID = $this->input->post('folderCat');
    $limit = 10;
    $userID = $this->session->userdata('id');
    $data['things'] = $this->account_model->folder_page($fID, $userID, $limit);

    // This is the part that is not working, shouldn't I be able to reload 
    // $data['things'] and send it to the view with the new parameters?
    $data['main_content'] = 'account/members_home';
    $this->load->view('includes/template2', $data);
}

1 个答案:

答案 0 :(得分:1)

您的控制器方法看起来正确。正确使用$data['things']方法中的folderSelector - 每个方法调用(来自单独的请求 - 如ajax请求)都不知道其他方法,因此您需要调用模型来设置$data['things']

您的ajax成功处理程序不会对返回的数据执行任何操作。您应该将loadTables功能更改为:

function loadTables(str) {
    console.log("dfgdfg");
    $.ajax({
            url: '<?php echo base_url().'account/members_home/folderSelector';?>', 
            type: 'POST',
            data: { folderCat: str },
            success: function(output_string){
                // here you need to inject the new data in the page 
                // in place of the old data
                $('table').replaceWith(output_string);
            }
    }); 
}