如何解决未捕获的TypeError:无法读取codeigniter中未定义的属性“ length”
晚上好,我尝试在codeigniter中编写库存系统代码。我想在数据表卡中显示数据。每当我运行该程序时,它都会说“ Uncaught TypeError:无法读取未定义的属性'length'”这是代码。
控制器
class Inventory extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('inventory_model');
}
public function index(){
$this->load->helper('url');
$this->load->view('inventory');
}
public function fetch(){
if ($this->input->is_ajax_request()) {
if ($posts = $this->inventory_model->get_entries()) {
$data = array('response' => 'success','posts' => $posts);
}else{
$data = array('response' => 'error','message' => 'Failed to fetch data');
}
echo json_encode($data);
}else{
echo "No direct";
}
}
}
?>
型号
<?php
/**
*
*/
class Inventory_model extends CI_Model{
public function get_entries()
{
$query = $this->db->get('inventory');
if (count($query->result())>0) {
return $query->result();
}
}
}
?>
脚本
function fetch(){
var table = $('#example').DataTable({
dom:
"<'row'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6'<'float-md-right ml-2'B>f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",buttons: [
'csv',{
'text': '<i class="fa fa-id-badge fa-fw" aria-hidden="true"></i>','action': function (e,dt,node) {
$(dt.table().node()).toggleClass('cards');
$('.fa',node).toggleClass(['fa-table','fa-id-badge']);
dt.draw('page');
},'className': 'btn-sm','attr': {
'title': 'Change views',}
}],select: 'single','ajax': {
'url':'<?=site_url()?>inventory/fetch','type': "post",'dataType': "json",},'columns': [
{
'orderable': false,'data': null,'className': 'text-center','render': function(data,type,full,meta){
if (type === 'display'){
data = '<i class="fa fa-user fa-fw"></i>';
data = '<img src="https://i.ibb.co/wYwKxdr/vaccine.png" alt="vaccine" border="0" class="avatar border rounded-circle">';
}
return data;
}
},{ "data": 'vaccine_name' },{ "data": 'stock' },{ "data": 'number_of_shots' },{ "data": 'shot_taken' }
],'drawCallback': function (settings) {
var api = this.api();
var $table = $(api.table().node());
if ($table.hasClass('cards')) {
// Create an array of labels containing all table headers
var labels = [];
$('thead th',$table).each(function () {
labels.push($(this).text());
});
// Add data-label attribute to each cell
$('tbody tr',$table).each(function () {
$(this).find('td').each(function (column) {
$(this).attr('data-label',labels[column]);
});
});
var max = 0;
$('tbody tr',$table).each(function () {
max = Math.max($(this).height(),max);
}).height(max);
} else {
// Remove data-label attribute from each cell
$('tbody td',$table).each(function () {
$(this).removeAttr('data-label');
});
$('tbody tr',$table).each(function () {
$(this).height('auto');
});
}
}
})
.on('select',function (e,indexes) {
var rowData = table.rows(indexes).data().toArray()
$('#row-data').html(JSON.stringify(rowData));
})
.on('deselect',function () {
$('#row-data').empty();
})
};
fetch();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。