Here is my code You can try this
<table class="table table-striped table-bordered first" id="stockSummary"> <thead> <tr> <th>Item Name</th> <th>Units</th> <th>Closing Qty</th> <th>Closing Value</th> </tr> </thead> <tbody> </tbody> </table>
and you must add this cdn
<script src="//cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
Js :
<script type="text/javascript"> var DataTable = $("#stockSummary").DataTable({ processing: true, serverSide: true, searching: true, ordering: true, order: [[0, "asc"]], "ajax": { "url": '<?php echo base_url('client/stock-summary-datatable'); ?>', "type": "POST", "data": function(data){ } }, 'columns': [ { "data": "item_name", "name": "item_name"}, { "data": "item_UOM", "name": "item_UOM"}, { "data": "item_Close_Qty", "name": "item_Close_Qty"}, { "data": "item_Close_Amt", "name": "item_Close_Amt"}, ], "dom": "<'row'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6 text-right'f>>" + "<'row'<'col-sm-12'tr>>" + "<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>", }); </script>
Controller :
public function stock_summary_datatable() { $offset = $this->input->post('start'); $limit = $this->input->post('length'); $total_table = $this->Tally_Stock_Summary->getData(); $table = $this->Tally_Stock_Summary->getData($limit,$offset); $data=[]; if (count($table) > 0) { foreach ($table as $row) { $tab = array(); $tab["item_name"] = $row['item_name']; $tab["item_UOM"] = $row['item_UOM']; $tab["item_Close_Qty"] = $row['item_Close_Qty']; $tab["item_Close_Amt"] = $row['item_Close_Amt']; $data[] = $tab; } } $results = array( "draw" => $this->input->post('draw'), "recordsTotal" => count($data), "recordsFiltered" => count($total_table), "data" => $data ); echo json_encode($results); }
Model :
function getData($limit='',$offset='') { $this->db->select("item_name, item_UOM, item_Close_Qty, item_Close_Amt")->from('tally_stock_summary'); if($limit !='' && $offset!='') { $this->db->limit($limit, $offset); } $qry = $this->db->get(); $data = $qry->result_array(); return $data; }
Question and answer is powered by AnsPress.io