Javascript 如何使用CodeIgniter和jquery调用SQL查询并将其加载到DATATABLE插件?
我希望你能帮助我。我的问题是我想在我的datatable插件中加载总计50000行的大量数据,但我不知道如何将其与CodeIgniter框架结合起来。 这是我的样本数据 在我的控制器中,我创建了这样一个函数,出于测试目的,我没有将它放在我的模型中Javascript 如何使用CodeIgniter和jquery调用SQL查询并将其加载到DATATABLE插件?,javascript,jquery,mysql,codeigniter,datatables,Javascript,Jquery,Mysql,Codeigniter,Datatables,我希望你能帮助我。我的问题是我想在我的datatable插件中加载总计50000行的大量数据,但我不知道如何将其与CodeIgniter框架结合起来。 这是我的样本数据 在我的控制器中,我创建了这样一个函数,出于测试目的,我没有将它放在我的模型中 public function displayListItem(){ $sqlSelectAll = "select * from items"; $resultSelectAll = $this->db->query($sql
public function displayListItem(){
$sqlSelectAll = "select * from items";
$resultSelectAll = $this->db->query($sqlSelectAll);
echo json_encode($resultSelectAll->row_array());
}
<?php
class Cmodel extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get_result()
{
$query=$this->db->query("your custom query;");
return $query->result_array();//return the result_array to the controller.
}
}
?>
下面是调用SQL的视图:
<!-- jquery part -->
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "<?php echo site_url("item_controller/displayListItem"); ?>"
} );
} );
$(文档).ready(函数(){
$('#示例')。数据表({
“bProcessing”:正确,
“bServerSide”:正确,
“源”:”
} );
} );
下面是我的表格,它将从mysql数据库填充我的数据
<table id="example">
<thead>
<tr>
<th></th>
<th>CODE</th>
<th>NAME</th>
<th>DESCRIPTION</th>
<th>BRAND</th>
<th>UNIT</th>
<th>CATEGORY NAME</th>
<th>ENTRY DATE</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<!-- THIS IS THE PART WHERE I NEED TO PUT THE DATA, BUT I DON'T KNOW HOW -->
</tbody>
</table>
代码
名称
描述
烙印
单位
类别名称
参赛日期
行动
我希望你们能帮我。谢谢首先将媒体文件夹保存在codeigniter项目文件夹中。媒体文件夹是包含js、图像和css文件的datatables所需的文件夹。在codeigniter config.php中设置基本url。在codeigniter database.php中设置数据库。设置url帮助程序$autoload['helper']=array('url');在autoload.php中 这是我的控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller
{
public function index()
{
$this->load->model('cmodel');
$data['d']=$this->cmodel->get_result();
$this->load->view('welcome_message.php',$data);//$data is an array which is sent to view
}
}
这就是观点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> CodeIgniter</title>
<style>
*{
font-family: arial;
}
</style>
<style type="text/css">
@import "<?php echo base_url();?>media/css/demo_table_jui.css";
@import "<?php echo base_url();?>media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>
<script src="<?php echo base_url();?>media/js/jquery.js" type="text/javascript"></script>
<script src="<?php echo base_url();?>media/js/jquery.dataTables.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#datatables.dataTables_filter input').focus()
$('#datatables').dataTable({
"sPaginationType":"full_numbers",
"aaSorting":[[2, "desc"]],
"bJQueryUI":true
});
})
</script>
</head>
<body>
<div>
<table id="datatables" class="display">
<thead>
<tr>
<th>id</th> <!-- These are the table heading-->
<th>name</th>
<th>order</th>
</tr>
</thead>
<?php foreach($d as $row):?><!--This is how you access the fields -->
<tr>
<td>
<?php echo $row['id'];?>
</td>
<td>
<?php echo $row['name'];?> <!-- here id,name,order are my column names-->
</td>
<td>
<?php echo $row['order'];?>
</td>
</tr>
<?php endforeach?>
</table>
</div>
</body>
</html>
共点火器
*{
字体系列:arial;
}
@导入“media/css/demo_table_jui.css”;
@导入“media/themes/smoothness/jquery-ui-1.8.4.custom.css”;
好的,先生,但我在装货方面有问题。如何使用datatable加载大量数据?我在手册中读到它有一个服务器端,但我不知道如何实现它。你可以看看这个链接,你会知道这将指导你了解如何使用datatables服务器端处理。基本上,您必须将JSON输出从服务器发送到视图,视图将被更新。“sAjaxSource”用于发出xhr请求。