1

好的,所以我加载的视图使用它从控制器接收的数据。然后,我使用 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);
}
4

1 回答 1

1

您的控制器方法看起来正确。$data['things']in方法使用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);
            }
    }); 
}
于 2012-12-21T15:12:05.420 回答