codeigniter 3 上的 loadData() 函数

loadData() function on 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();
}
})

我的交易明细只包含 table 还有我的控制器

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

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

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

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