2017-10-11 160 views
0

我有4個選擇,每個繼續選擇基於前面選擇的選擇。我將如何發送動態數據與源項目的請求?X-editable如何通過請求發送數據

我試過一個函數,但發現jQuery getJSON()是異步的,並不會按照我需要的方式工作。

這是我目前的。是的,我知道,它也行不通。

// Location_Site X-editable init 
$("#location-aisle").editable({ 
    url: BASE_URL + "item/edit_item", 
    title: "Site aisle", 
    params: { 
     type: "location-site" 
    }, 
    sourceCache: false, 
    source: BASE_URL + "item/get_aisles", 
    sourceOptions: { 
     headers: { 
      "aisle-id": $("#location-site").val() 
     } 
    } 
}); 

回答

0

使用成功事件並手動設置源參數如下所示。

// Location_Site X-editable init 
$("#location-site").editable({ 
    url: BASE_URL + "item/edit_item", 
    title: "Location site", 
    params: { 
     type: "location-site" 
    }, 
    source: sites, 
    success: function (response, newValue) { 
     // Get aisles for site 
     $.getJSON(BASE_URL + "item/get_aisles/" + newValue, function(data) { 
      outputDebug(data); 

      // Load aisles into drop down 
      if (data && data.length > 0) { 
       $("#location-aisle").editable("option", "source", data); 
       $("#location-aisle").editable("enable"); 
      } else { 
       window.alert("Failed to load aisles for selected site."); 
      } 
     }, "json"); 
    } 
}); 

這是一個新的水平下降到給一個更好的例子

// Location_Site X-editable init 
$("#location-aisle").editable({ 
    url: BASE_URL + "item/edit_item", 
    title: "Site aisle", 
    params: { 
     type: "location-aisle" 
    }, 
    success: function (response, newValue) { 
     // Get columns for site 
     $.getJSON(BASE_URL + "item/get_columns/" + newValue, function(data) { 
      outputDebug(data); 

      // Load aisles into drop down 
      if (data && data.length > 0) { 
       $("#location-column").editable("option", "source", data); 
       $("#location-column").editable("enable"); 
      } else { 
       window.alert("Failed to load columns for selected site."); 
      } 
     }, "json"); 
     // 
     // Get rows for site 
     $.getJSON(BASE_URL + "item/get_rows/" + newValue, function(data) { 
      outputDebug(data); 

      // Load aisles into drop down 
      if (data && data.length > 0) { 
       $("#location-row").editable("option", "source", data); 
       $("#location-row").editable("enable"); 
      } else { 
       window.alert("Failed to load rows for selected site."); 
      } 
     }, "json"); 
    } 
}); 

注意給別人:此代碼不檢查和處理無效應答或無效的變量貴重物品。