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>
avatar yusufbachtiar
@yusufbachtiar

8 Kontribusi 1 Poin

Diperbarui 1 tahun yang lalu

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-&gt;db = db_connect();
    $this-&gt;request = $request;

    $this-&gt;dt = $this-&gt;db-&gt;table($this-&gt;table);
}
private function _get_datatables_query()
{
    $i = 0;
    foreach ($this-&gt;column_search as $item) {
        if ($this-&gt;request-&gt;getPost('search')['value']) {
            if ($i === 0) {
                $this-&gt;dt-&gt;groupStart();
                $this-&gt;dt-&gt;like($item, $this-&gt;request-&gt;getPost('search')['value']);
            } else {
                $this-&gt;dt-&gt;orLike($item, $this-&gt;request-&gt;getPost('search')['value']);
            }
            if (count($this-&gt;column_search) - 1 == $i)
                $this-&gt;dt-&gt;groupEnd();
        }
        $i++;
    }

    if ($this-&gt;request-&gt;getPost('order')) {
        $this-&gt;dt-&gt;orderBy($this-&gt;column_order[$this-&gt;request-&gt;getPost('order')['0']['column']], $this-&gt;request-&gt;getPost('order')['0']['dir']);
    } else if (isset($this-&gt;order)) {
        $order = $this-&gt;order;
        $this-&gt;dt-&gt;orderBy(key($order), $order[key($order)]);
    }
}
function get_datatables()
{
    $this-&gt;_get_datatables_query();
    if ($this-&gt;request-&gt;getPost('length') != -1)
        $this-&gt;dt-&gt;limit($this-&gt;request-&gt;getPost('length'), $this-&gt;request-&gt;getPost('start'));
    $query = $this-&gt;dt-&gt;get();
    return $query-&gt;getResult();
}
function count_filtered()
{
    $this-&gt;_get_datatables_query();
    return $this-&gt;dt-&gt;countAllResults();
}
public function count_all()
{
    $tbl_storage = $this-&gt;db-&gt;table($this-&gt;table);
    return $tbl_storage-&gt;countAllResults();
}

} &lt;/pre&gt;&lt;div&gt;&lt;br&gt;Controller ListDataBarang:&lt;br&gt;&lt;br&gt;&lt;/div&gt;&lt;pre&gt;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-&gt;brgkode;
        $row[] = $list-&gt;brgnama;
        $row[] = number_format($list-&gt;brgharga, 0, ",", ".");
        $row[] = number_format($list-&gt;brgstok, 0, ",", ".");
        $data[] = $row;
    }
    $output = [
        "draw" =&gt; $request-&gt;getPost('draw'),
        "recordsTotal" =&gt; $datamodel

->count_all(), "recordsFiltered" => $datamodel->count_filtered(), "data" => $data ]; echo json_encode($output); } } &lt;/pre&gt;&lt;div&gt;&lt;br&gt;View Datatable nya:&lt;br&gt;&lt;br&gt;&lt;/div&gt;&lt;pre&gt;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>

&lt;/tbody&gt;

</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>

avatar adamajalah27
@adamajalah27

119 Kontribusi 40 Poin

Dipost 1 tahun yang lalu

Tanggapan

Maaf gan ini maksudnya gmna yahh ?

Login untuk ikut Jawaban