codeigniter 3上的loadData()函数

时间:2016-10-26 03:13:22

标签: javascript php codeigniter codeigniter-3

如何在CodeIgniter 3上使用JavaScript loadData函数? 我有一个交易视图,其中包含要购买的详细信息,我想使用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 个答案:

答案 0 :(得分:1)

看起来您正在尝试使用jQuery和CodeIgniter在div中加载HTML视图。我会利用jQuery提供的$.ajax method$.get method,因为你已经在使用它了。

javascript代码示例如下:

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

如果您认为这可以解决您的问题,请标记为已接受的答案。