Postingan lainnya
Bagaimana cara menampilkan limit stok barang menggunakan datatable
selamat malam, ada yang bisa bantu, saya masih belajar utk di codeigniter 4, rencana nya saya mau coba bikin tampilan data tapi berdasarkan stok barang yang kurang dari 10 gtu, jadi nanti data yang tampil hanya berdasarkan stok yang kurang dari 10, tapi menggunakan datatable, saya sudah coba nyari di google dan youtube belum ketemu
modelstokbarang
<?php
namespace App\Models;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\Model;
class Modelstokbarang extends Model
{
protected $table = "barang";
protected $column_order = array(null,'brkode','brgnama', 'brgharga', 'brgstok',);
protected $column_search = array('brgkode', 'brgnama','brgstok');
protected $order = array('brgstok' => 'ASC');
protected $request;
protected $db;
protected $dt;
function __construct(RequestInterface $request)
{
parent::__construct();
$this->db = db_connect();
$this->request = $request;
$this->dt = $this->db->table($this->table);
}
private function _get_datatables_query()
{
$i = 0;
foreach ($this->column_search as $item) {
if ($this->request->getPost('search')['value']) {
if ($i === 0) {
$this->dt->groupStart();
$this->dt->like($item, $this->request->getPost('search')['value']);
} else {
$this->dt->orLike($item, $this->request->getPost('search')['value']);
}
if (count($this->column_search) - 1 == $i)
$this->dt->groupEnd();
}
$i++;
}
if ($this->request->getPost('order')) {
$this->dt->orderBy($this->column_order[$this->request->getPost('order')['0']['column']], $this->request->getPost('order')['0']['dir']);
} else if (isset($this->order)) {
$order = $this->order;
$this->dt->orderBy(key($order), $order[key($order)]);
}
}
function get_datatables()
{
$this->_get_datatables_query();
if ($this->request->getPost('length') != -1)
$this->dt->limit($this->request->getPost('length'), $this->request->getPost('start'));
$query = $this->dt->get();
return $query->getResult();
}
function count_filtered()
{
$this->_get_datatables_query();
return $this->dt->countAllResults();
}
public function count_all()
{
$tbl_storage = $this->db->table($this->table);
return $tbl_storage->countAllResults();
}
}
Controller ListDataBarang
function listDataBarang()
{
$request = Services::request();
$datamodel = new Modelstokbarang($request);
if ($request->getMethod(true) == 'POST') {
$lists = $datamodel->get_datatables();
$data = [];
$no = $request->getPost("start");
foreach ($lists as $list) {
$no++;
$row = [];
$row[] = $no;
$row[] = $list->brgkode;
$row[] = $list->brgnama;
$row[] = number_format($list->brgharga, 0, ",", ".");
$row[] = number_format($list->brgstok, 0, ",", ".");
$data[] = $row;
}
$output = [
"draw" => $request->getPost('draw'),
"recordsTotal" => $datamodel->count_all(),
"recordsFiltered" => $datamodel->count_filtered(),
"data" => $data
];
echo json_encode($output);
}
}
View Datatable nya
<h3>Daftar Stok Barang Yang Limit</h3>
<hr>
<table style="width: 100%" id="databarang" class="table table-bordered table-hover dataTable dtr-inline collapsed">
<thead>
<tr>
<th>No</th>
<th>Kode Barang</th>
<th>Nama Barang</th>
<th>Harga</th>
<th>Stok</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
function listDataBarang() {
var table = $('#databarang').DataTable({
destroy: true,
"processing": true,
"serverSide": true,
"order": [],
"ajax": {
"url": "/Dashboard/listDataBarang",
"type": "POST",
},
"columnDefs": [{
"targets": [0],
"orderable": true,
}, ],
});
}
$(document).ready(function() {
listDataBarang();
});
</script>
1 Jawaban:
<div>Modelstokbarang:<br><br></div><pre>```php <?php
namespace App\Models;
use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\Model;
class Modelstokbarang extends Model { protected $table = "barang"; protected $column_order = array(null,'brkode','brgnama', 'brgharga', 'brgstok',); protected $column_search = array('brgkode', 'brgnama','brgstok'); protected $order = array('brgstok' => 'ASC'); protected $request; protected $db; protected $dt;
function __construct(RequestInterface $request)
{
parent::__construct();
$this->db = db_connect();
$this->request = $request;
$this->dt = $this->db->table($this->table);
}
private function _get_datatables_query()
{
$i = 0;
foreach ($this->column_search as $item) {
if ($this->request->getPost('search')['value']) {
if ($i === 0) {
$this->dt->groupStart();
$this->dt->like($item, $this->request->getPost('search')['value']);
} else {
$this->dt->orLike($item, $this->request->getPost('search')['value']);
}
if (count($this->column_search) - 1 == $i)
$this->dt->groupEnd();
}
$i++;
}
if ($this->request->getPost('order')) {
$this->dt->orderBy($this->column_order[$this->request->getPost('order')['0']['column']], $this->request->getPost('order')['0']['dir']);
} else if (isset($this->order)) {
$order = $this->order;
$this->dt->orderBy(key($order), $order[key($order)]);
}
}
function get_datatables()
{
$this->_get_datatables_query();
if ($this->request->getPost('length') != -1)
$this->dt->limit($this->request->getPost('length'), $this->request->getPost('start'));
$query = $this->dt->get();
return $query->getResult();
}
function count_filtered()
{
$this->_get_datatables_query();
return $this->dt->countAllResults();
}
public function count_all()
{
$tbl_storage = $this->db->table($this->table);
return $tbl_storage->countAllResults();
}
}
</pre><div><br>Controller ListDataBarang:<br><br></div><pre>
php
function listDataBarang()
{
$request = Services::request();
$datamodel = new Modelstokbarang($request);
if ($request->getMethod(true) == 'POST') {
$lists = $datamodel->get_datatables();
$data = [];
$no = $request->getPost("start");
foreach ($lists as $list) {
$no++;
$row = [];
$row[] = $no;
$row[] = $list->brgkode;
$row[] = $list->brgnama;
$row[] = number_format($list->brgharga, 0, ",", ".");
$row[] = number_format($list->brgstok, 0, ",", ".");
$data[] = $row;
}
$output = [
"draw" => $request->getPost('draw'),
"recordsTotal" => $datamodel
->count_all(),
"recordsFiltered" => $datamodel->count_filtered(),
"data" => $data
];
echo json_encode($output);
}
}
</pre><div><br>View Datatable nya:<br><br></div><pre>
html
<h3>Daftar Stok Barang Yang Limit</h3>
<hr>
<table style="width: 100%" id="databarang" class="table table-bordered table-hover dataTable dtr-inline collapsed">
<thead>
<tr>
<th>No</th>
<th>Kode Barang</th>
<th>Nama Barang</th>
<th>Harga</th>
<th>Stok</th>
</tr>
</thead>
<tbody>
</tbody>
</table> </div> </div> </div> </div>
<script> function listDataBarang() { var table = $('#databarang').DataTable({ destroy: true, "processing": true, "serverSide": true, "order": [], "ajax": { "url": "/Dashboard/listDataBarang", "type": "POST", }, "columnDefs": [{ "targets": [0], "orderable": true, }, ], }); }
$(document).ready(function() { listDataBarang(); }); </script>
Tanggapan
Maaf gan ini maksudnya gmna yahh ?