簡體   English   中英

使用laravel db查詢中的json數據填充選擇框

[英]Populate select box with json data from laravel db query

我有一個html選擇框,需要使用帶有來自php(Laravel)數據庫查詢的選擇選項的jquery ajax進行填充。 我嘗試了許多方法,但沒有結果,結果只有“未定義”。 Laravel腳本:

public function loturi($articol) {
$loturi = DB::select("select mgla_lotto from MG_V_DispoLottiBarcode d
join MG_AnaArt art on art.MGAA_Id = d.MGAA_Id join MG_LottiAnag  l on         
l.MGLA_Id = d.MGLA_Id where MGAA_MBDC_Classe IN ('SEM','FIN') and mbmg_id = 55 
and mgaa_matricola in (select  child.MGAA_Matricola component
from DB_Legami join MG_AnaArt  child on child.MGAA_Id = DBLG_Com_MGAA_Id
join MG_AnaArt  p on p.MGAA_Id = DBLG_MGAA_Id
where p.MGAA_Matricola = '$articol')  and mgla_datacreazione > '2014-01-01'
group by mgla_lotto having sum(dispo) > 0");
return compact('loturi');
}

結果是這樣的:

{"loturi":[{"mgla_lotto":"1282\/15"},{"mgla_lotto":"1316\/15"},{"mgla_lotto":"1339\/15"},{"mgla_lotto":"1349\/15"},{"mgla_lotto":"1354\/15"},{"mgla_lotto":"1404\/15"},{"mgla_lotto":"1405\/15"},{"mgla_lotto":"1412\/15"}]}

jQuery腳本是這樣的:

$(document).ready(function() {
$("#cod").change(function(){
var cod_articol = $("#cod").val();
$.ajax ({
url: "/internal/" + cod_articol ,
datatype: "json",
success: function(data) {
$.each(data, function(value){
$("#lot").append('<option id="' + value.index + '">' + value.name +   
'</option>');
      })
     }
   });
 });
});

嗨,這是您的答案。

更改

$.each(data, function(value){

$.each(data.loturi, function(value){

並使用以下語法

$.each(data.loturi, function( index, value ){
    $("#lot").append('<option value="' + index + '">' + value.mgla_lotto  + '</option>');
}

轉到鏈接以查看我為您制作的示例。

https://jsfiddle.net/ovht7fkh/2/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM