TypeError: $(...).DataTable is not a function

View Data

<?= $this->extend('main/layout') ?>

<?= $this->section('judul') ?>
Manajemen Data Transaksi Barang Keluar
<?= $this->endSection('judul') ?>

<?= $this->section('subjudul') ?>
<button type="button" class="btn btn-success" onclick="location.href=('/barangkeluar/input')">
    <i class="fa fa-plus-circle"></i> Input Transaksi
</button>
<?= $this->endSection('subjudul') ?>

<?= $this->section('isi') ?>
<!-- DataTables -->
<link rel="stylesheet" href="<?= base_url() ?>/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
<link rel="stylesheet" href="<?= base_url() ?>/plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
<!-- DataTables  & Plugins -->
<script src="<?= base_url() ?>/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="<?= base_url() ?>/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="<?= base_url() ?>/plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="<?= base_url() ?>/plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<table style="width: 100%" id="databarangkeluar" class="table table-bordered table-hover dataTable dtr-inline collapsed">

    <thead>
        <tr>
            <th>No</th>
            <th>Faktur</th>
            <th>Tanggal</th>
            <th>pelanggan</th>
            <th>Total Harga (Rp)</th>
            <th>Aksi</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

<script>
    function listDataBarangKeluar() {
        var table = $('#databarangkeluar').DataTable({
            destroy: true,
            "processing": true,
            "serverSide": true,
            "order": [],
            "ajax": {
                "url": "/barangmasuk/listDataBarangKeluar",
                "type": "POST",
            },
            "columnDefs": [{
                "targets": [0, 5],
                "orderable": false,
            }, ],
        });
    }

    $(document).ready(function() {
        listDataBarangKeluar();
    });
</script>

<?= $this->endSection('isi') ?>

controller

public function listDataBarangKeluar()
    {
        $request = Services::request();
        $datamodel = new ModelDataBarangKeluar($request);
        if ($request->getMethod(true) == 'POST') {
            $lists = $datamodel->get_datatables();
            $data = [];
            $no = $request->getPost("start");
            foreach ($lists as $list) {
                $no++;
                $row = [];

                // $tombolPilih = "<button type=\"button\" class=\"btn btn-sm btn-success\" onclick=\"pilih('". $list->pelid ."','". $list->pelnama ."')\">Pilih</button>";
                // $tombolHapus = "<button type=\"button\" class=\"btn btn-sm btn-danger\" onclick=\"hapus('". $list->pelid ."','". $list->pelnama ."')\">Hapus</button>";

                $row[] = $no;
                $row[] = $list->faktur;
                $row[] = $list->tglfaktur;
                $row[] = $list->pelnama;
                $row[] = number_format($list->totalharga,0,",",".");
                $row[] ='';
                $data[] = $row;
            }
            $output = [
                "draw" => $request->getPost('draw'),
                "recordsTotal" => $datamodel->count_all(),
                "recordsFiltered" => $datamodel->count_filtered(),
                "data" => $data
            ];
            echo json_encode($output);
        }
    }

model

<?php

namespace App\Models;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\Model;

class ModelDataBarangKeluar extends Model
{
    protected $table = "barangkeluar";
    protected $column_order = array(null, 'faktur', 'tglfaktur', 'pelnama', 'totalharga', null);
    protected $column_search = array('faktur', 'tglfaktur', 'pelnama');
    protected $order = array('faktur' => 'ASC');
    protected $request;
    protected $db;
    protected $dt;

    function __construct(RequestInterface $request)
    {
        parent::__construct();
        $this->db = db_connect();
        $this->request = $request;
    }
    private function _get_datatables_query()
    {
        $this->dt = $this->db->table($this->table)->join('pelanggan', 'idpel=pelid', 'left');
        $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)->join('pelanggan', 'idpel=pelid', 'left');
        return $tbl_storage->countAllResults();
    }
}

error.PNG

ini kenapa ya gan apa ada yang bisa bantu ?

saya mau nampilin data dengan data table-server side tapi data nya gk tampil nahh error nya tuhh yang seperti di gambar, saya menggunakan CI 4

avatar yusufbachtiar
@yusufbachtiar

8 Kontribusi 1 Poin

Diperbarui 1 tahun yang lalu

3 Jawaban:

<div>Kamu pakai library apa. Coba diperhatikan cara penggunannya.<br>Contoh DataTables, cek <a href="https://datatables.net/manual/installation">dokumentasi datatables</a><br><br></div><pre><strong>let</strong> table = <strong>new</strong> DataTable('#myTable', {&nbsp; &nbsp; // config options...});</pre>

avatar hilmanski
@hilmanski

2685 Kontribusi 2135 Poin

Dipost 1 tahun yang lalu

Tanggapan

Saya pakai library dari bootstrap 4 gan, dari template admin LTE 3 tapi saya juga nerapin di function yang lain gk bermasalah gan

<div>Kayanya ini masalah di import jquerynya deh.<br><br>Coba sebelum atau diatas ini</div><pre>&lt;script src="&lt;?= base_url() ?&gt;/plugins/datatables/jquery.dataTables.min.js"&gt;&lt;/script&gt;</pre><div>import dulu jquerynya</div><pre>&lt;script src="<a href="https://code.jquery.com/jquery-3.5.1.js">https://code.jquery.com/jquery-3.5.1.js</a>"&gt;&lt;/script&gt;</pre><div><br>Jadinya gini</div><pre>&lt;script src="<a href="https://code.jquery.com/jquery-3.5.1.js">https://code.jquery.com/jquery-3.5.1.js</a>"&gt;&lt;/script&gt; &lt;script src="&lt;?= base_url() ?&gt;/plugins/datatables/jquery.dataTables.min.js"&gt;&lt;/script&gt;</pre>

avatar Nandar
@Nandar

648 Kontribusi 204 Poin

Dipost 1 tahun yang lalu

<div>udh solved gan terimakasih referensi nya ternyata jquery.js nya yang di view layout belum saya pindahin ke atas hehe</div>

avatar yusufbachtiar
@yusufbachtiar

8 Kontribusi 1 Poin

Dipost 1 tahun yang lalu

Login untuk ikut Jawaban