如何传递值从数据表到控制器在Codeigniter?



我是用codeigniter编程的初学者。我有数据表来显示我的购物车。但是我不能从datatable传递值到控制器。这是我的jquery的视图

我想传入控制器的变量是

$id_penjualan = '8377'

这是我的数据表在我的视图:

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 },
            ]
        });
    }

这里是控制器

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,
        ]);
    }

这是我的模型

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;
    }

当我运行我的代码,它返回$id_penjualan NULL值。有人能帮我吗?由于

您应该只对ajax请求做这样的更改:

ajax: {
        type: 'POST',
        url: '<?php echo base_url();?>salesinvoice/get_cart_edit',
        data: function(data) {
           data.id_penjualan = $('#id_penjualan2').val()
        },
        dataSrc: '',
   },

相关内容

  • 没有找到相关文章

最新更新