Home > Blockchain >  How to use 2 url in 1 function ajax
How to use 2 url in 1 function ajax

Time:10-26

I have 2 table, warehouse and item category, i wanna show my table in select option ajax. I managed to display select option item category.

My Ajax Code

$(function () {
  var i = 0;
  $("#add-more-item").click(function () {
      i;
    $.ajax({
        type:"GET",
        url:"/admin-master/get_item_category",
        dataType: 'JSON',
        success:function(res){
          let option = "";
          $.each(res,function(itemcategory_name,id_item_category){
             option  = '<option value=' id_item_category '>'  itemcategory_name  '</option>'
          });
            $(".add-more").append(
              '<tr><td><select class="form-control" id="warehouse"></select</td>
               <td><input type="text" name="item_code[' i ']" class="form-control" placeholder="Kode Barang" required></td>
               <td><select class="form-control" name="itemcategory_id[' i ']"><option>-- Pilih Ketegori --</option>' option '</select></td>
               <td><input type="text" name="item_name[' i ']" class="form-control" placeholder="Nama Barang" required</td>
               <td><input type="text" name="item_weight[]" class="form-control" placeholder="Contoh : 200 kg" required></td>
               <td><input type="text" name="item_height[]" class="form-control" placeholder="Contoh : 200 cm" required></td>
               <td><input type="number" name="item_qty[]" class="form-control" placeholder="Contoh : 200 " required></td>
               <td><input type="text" name="description[]" class="form-control" placeholder="Keterangan Barang"></td>
               <td align="center"><button type="button" class="remove-item btn btn-danger btn-md"><i class="fas fa-trash-alt"></i></a></td></tr>'
                );
            }
        });
        
      });

how do i retrieve the data warehouse ? should i create 2 urls to fetch category and warehouse items? can i make 2 urls?

CodePudding user response:

why you not combine warehouse and item category to 1 url ajax

Example /admin-master/get_item_category_and_warehouse

and in controller

return response([
  'item_categories' => $itemCategory,
  'warehouse' => $warehouse,
]);

and ajax success function

success:function(res){
  let item_categories = res.item_categories,
      warehouse = res.warehouse;
  // process options
}
  • Related