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']メソッド内のが正しく使用されています。folderSelector各メソッド呼び出し (別のリクエストから - ajax リクエストなど) は他のメソッドを認識しないため、モデルを呼び出して set にする必要があります$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 に答える