2016-10-26 5 views
1

CodeIgniter 3のJavaScript loadData関数の使い方は? 購入する詳細アイテムを持つトランザクションビューがあります。詳細ビューをロードするためにloadDataを使用します。codeigniter 3のloadData()関数

これは私のファイル構造である:

myproject: 
-application 
    --controller 
    ---admin 
     -----dashboard.php 
     -----transaction.php 
    ---home.php 
    --model 
    ---mymodel.php 
    --view 
    ---transaction 
     ----transaction_index.php <- the view that I want to use for calling 
     ----transaction_detail.php <- the view that I want to call 
    ---home.php 

そして、私のtransaction_index本体:

<div id="t_detail"></div> 

スクリプト

$(document).ready(function(){ 
function loadData(args){ 
    $("#t_detail").load("<?php echo base_url('admin/transaction/getDetail'); ?>"); 
} 
loadData(); 

function emptying(args){ 
    $("#id").val(); 
    $("#name").val(); 
    $("#category").val(); 
    $("#price").val(); 
    $("#total").val(); 
} 
}) 

マイ取引の詳細ちょうどテーブル そして、私のコントローラ

が含まれています
public function getDetail(){ 
    $data['temporary'] = $this->mymodel->getAllData('temporary')->result(); 
    $data['limit'] = $this->mymodel->countAll('temporary'); 
    $this->load->view('transaction/transaction_detail', $data); 
} 

答えて

1

jQueryとCodeIgniterを使用してdivの内部にHTMLビューを読み込もうとしているようです。すでに使用しているので、jQueryが提供する$.ajax methodまたは$.get methodを利用します。あなたは、これはあなたの問題を解決感じる場合は受け入れ答えとしてマークしてください

// var site_url returns your codeigniter application's URL by splitting it before index.php. 
// if you're using .htaccess or mod_rewrite for pretty urls (no index.php, you may need to find another way to get the URL of your site) 

var site_url = window.location.protocol + '//' + window.location.hostname + window.location.pathname.split('index.php')[0] + 'index.php'; 

//do a GET request for the transaction/getDetail endpoint 
$.get(site_url + 'index.php/transaction/getDetail', function(html) { 
    //load the HTML returned by the codeigniter controller into the #t_detail element 
    $("#t_detail").html(html); 
}); 

javascriptのコード例は次のようになります。

+0

まだ何も表示されていないため、「index.php」を含める必要がありますか?私はURLにindex.phpを使用しません。.htaccess rewriteを使用しますが、 – Teletubbies

+0

の前に感謝します。あなたが.htaccessを使用しない場合は、 'window.location.protocol + '//' + windowのようなものを試すことができます。 location.hostname' ...あなたのアプリがサブディレクトリにない場合に動作します。サブディレクトリにある場合は、サイトのURLを別の方法で取得したいと考えています。 – KFE

+0

あなたはまだ何も表示されていないと言っています... PHPやJavascriptから何かエラーが出ていますか(コンソール経由で確認してください) – KFE