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


Can I load CodeIgniter view from ajax response and use $data obj?

我加载的视图使用它从控制器接收到的数据。然后使用jquery .change侦听器侦听选择选项列表中的更改。这将触发一个函数运行,实际上一个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控制器:

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);
}

你的控制器方法看起来正确。folderSelector方法中的$data['things']被正确使用-每个方法调用(来自单独的请求-像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);
            }
    }); 
}