0

I'm begginer coding with codeigniter. I have datatable to show my cart in view. But I cant pass value from datatable to controller. here is my jquery on my view.

my variable that i want to pass in controller is

$id_penjualan = '8377'

this is my datatable in my view :

function getdata() {
        var id_penjualan = $('#id_penjualan2').val();
        table = $("#table-sales").DataTable({
            "processing": true,
            "searching": false,
            "ordering": false,
            "paging":   false,
            "info":     false,
            "scrollY": "50vh",
            "scrollCollapse": true,
            "ajax": {
                // "url": `<?php echo base_url() ?>salesinvoice/get_cart_edit`,
                "url": `<?php echo base_url();?>salesinvoice/get_cart_edit`,
                "type": "GET",
                "data": {id_penjualan : id_penjualan},
            },
            "columns": [
                { "title" : "No", "render" : (data,type,row,meta) => {return meta.row + 1}, "width" : "5%" },
                { "title" : "Kode Barang", "data": "kode_barang", "orderable" : false },
                { "title" : "Nama Barang", "data": "nama_barang", "orderable" : false },
                { "title" : "Jumlah", "data": "jumlah", "orderable" : false },
                { "title" : "Satuan", "data": "satuan", "orderable" : false },
                { "title" : "Harga", "data": "harga", "orderable" : false },
                { "title" : "Diskon (%)", "data": "diskon", "orderable" : false },
                { "title" : "Diskon", "data": "total_diskon", "orderable" : false },
                { "title" : "Total", "data": "total", "orderable" : false },
                { "title" : "Action", "data": "action", "orderable" : false },
            ]
        });
    }

and here is my controller

function get_cart_edit()
    {
        $total_diskon = 0;
        $sub = 0;
        $subtotal = 0;
        $total_dpp = 0;
        $tax_amount = 0;
        $total_akhir = 0;
        
        $id_penjualan       = $this->input->post('id_penjualan2');
        $result= $this->salesinvoice_model->getdatadummy($id_penjualan);
        // print_r($id_penjualan);exit;
        $list       = [];
        $no = 1;
        foreach ($result as $i => $items) {
            // print_r($items);exit;
            $rowidx = $items['id_penjualan_item'];
            $satuan =db_get_where('satuan', ['id_satuan' => $items['id_satuan']])->row_array();
            $no++;
            $row['no']  = $no;
            $row['kode_barang'] = $this->salesinvoice_model->getkodeitem($items['id_item']).'<input type="hidden" name="id2" id="id2" value="'.$items['id_item'].'" />';
            $row['nama_barang'] = $this->salesinvoice_model->getnamaitem($items['id_item']);
            $row['jumlah']      = $items['jumlah'];
            $row['satuan']      = isset($satuan['nama_satuan']) ? $satuan['nama_satuan'] : "";
            $row['harga']       = number_format($items['harga_jual']);
            $row['diskon1']     = (float)$items['diskon_persen'];
            $row['total_diskon']= number_format($items['total_diskon']);
            $row['total']       = number_format(($items['harga_jual'] * $items['jumlah']) - $items['total_diskon']);
            $row['action']      = '
            <button type="button" class="update_cart btn btn-warning btn-sm" onclick="editCart(\''.$rowidx.'\')"><i class="fa fa-edit"></i></button>
            <button type="button" id="'.$items['id_penjualan_item'].'" class="romove_cart btn btn-danger btn-sm"><i class="fa fa-trash-o"></i></button>
            ';
            $list[] = $row;
            $total_diskon += $items['diskon_nominal'];
            $sub = $this->cart->total();
            $subtotal = $sub - $total_diskon;
            $total_dpp = $subtotal;                 
            $tax_amount = (11 * $total_dpp) / 100;              
            $total = $total_dpp + $tax_amount;
        }
        
        $metode_pembayaran = array(
            "1" => "CASH",
            "2" => "EDC",
            "3" => "TRANSFER",
        );
        $kas = create_double($this->salesinvoice_model->getkas(),'id_kas','nama_kas');
        echo json_encode([
            "data" => $list,
            "metode_pembayaran" => $metode_pembayaran,
            "total_diskon" => $total_diskon,
            "sub" => $sub,
            "subtotal" => $subtotal,
            "total_dpp" => $total_dpp,
            "tax_amount" => $tax_amount,
            "total_akhir" => $total_akhir,
            "kas" => $kas,
        ]);
    }

and this is my model

public function getdatadummy($id_penjualan){
        $this->db->select('*');
        $this->db->from('dummy_penjualan_item');        
        $this->db->where('id_penjualan',$id_penjualan);
        $result = $this->db->get()->result_array();
        return $result;
    }

When i run my codes, it's return $id_penjualan NULL value. Can anyone help me. Thanks

1 Answer 1

1

You should do only this change on ajax request:

ajax: {
        type: 'POST',
        url: '<?php echo base_url();?>salesinvoice/get_cart_edit',
        data: function(data) {
           data.id_penjualan = $('#id_penjualan2').val()
        },
        dataSrc: '',
   },
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much @Dori Rina, it's work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.