Как передать идентификатор продукта в параметр ajax для запроса на code2 на стороне сервера select2 - PullRequest
0 голосов
/ 16 марта 2020

я хотел спросить. У меня была проблема, тоже поймать идентификатор из продукта продукта и передать его контроллеру, и моя первая попытка показывает [объект объекта] $ abc123. как сделать так, чтобы из объекта Object в строку типа [object Object] $ abc123 стало только $ abc123? я тоже хотел, чтобы мой select2 просто показывал, какой номер доступен только для продукта abc123.

это мой исходный код JS.

$("#selUom").select2({
        ajax: {
            url: '<?=base_url("transaksi/get/uom")?>',
            type: "post",
            dataType: 'json',
            data: function(params) {
                return {
                    idprod: params+'<?php echo $detprod["productcode"]; ?>',
                    searchTerm: params.term // search term
                };
            },
            processResults: function(response) {
                return {
                    results: response
                };
            },
            cache: true
        }
    });

Контроллер

public function allUom(){
    $searchTerm = $this->input->post('searchTerm');
    $idprod     = $this->input->post('idprod');
    $response   = $this->M_Mstuom->allBasedUom($idprod,$searchTerm);

    echo json_encode($response);
}

модель

public function allBasedUom($id, $searchTerm=""){
    $this->db->select('m.uomid, m.uom');
    $this->db->join("msuom m", "m.uomid = pu.uomid");
    $this->db->where("m.uom like '%" . $searchTerm . "%' ");
    $this->db->where("pu.productcode", $id);
    $this->db->where("m.isactive", "t");
    $fetched_records = $this->db->get('msproductuom pu');
    $uom = $fetched_records->result_array();
    $data = array();
    foreach ($uom as $uom) {
        $data[] = array("id" => $uom['uomid'], "text" => $uom['uom']);
    }
    return $data;
}

1 Ответ

0 голосов
/ 16 марта 2020
Try to use :
$("#selUom").select2({
        ajax: {
            url: '<?=base_url("transaksi/get/uom")?>',
            type: "post",
            dataType: 'json',
            data: function(params) {
                return {
                    idprod: params.'<?php echo $detprod["productcode"]; ?>',
                    searchTerm: params.term // search term
                };
            },
            processResults: function(response) {
                return {
                    results: response
                };
            },
            cache: true
        }
    });
...