2012-04-04 9 views
1

私の問題は次のとおりです。いくつかのレコードに関するデータを取得してフォームで利用できるようにするのは簡単です。関連するドロップダウンリストを持つ編集するフォームをロードする

問題は:3つの関連するドロップダウンリストで表される場所(大陸、国、都市)です。

私が得るのは、ブラウザが応答を停止することです、私はより良い解決策を考え出すためにいくつかのアイデアが必要です。ここで

はので、多分誰かが私にヒントを与えることができ、それが失速場所に関するコメントとコードのサンプルです:

// To load the drop-down lists I use similar ajax code as the one shown below in the edit case 

function processRow(command, id) { 
    switch(command){ 

    case 'Delete': {  
     $("#deleteId").val(id); 
     $('#deleteEventModal').reveal(); 
    } 
    break; 
    case 'Edit': { 
     /* Fetch Data to Fill up the form */ 
     $.ajax({ 
      type: "GET", 
      url: "scripts/fetchEventById.php?eventId=" + encodeURIComponent(id), 
      dataType: "json", 
      success: function(response){ 
        /* On Successful Posting to server side */ 
           // THIS DROP DOWN LOADS AND SETS DB VAL OK 
        loadContinents("#editEventContinents"); 
        $("#editEventContinents").val(response.eventData.continentId);   

        // FROM THIS MOMENT ON IT WILL STALL 
        // last instruction sets continent drop-down with proper value BUT 
        // when fetching the countries for that continent (below) 
        // the continent drop-down value comes empty as if nothing 
        // was selected 
        // but it was, I visually confirmed that 
        // after setting it with val() above 
        loadCountries("#editEventContinents", "#editEventCountries", "#editEventCities"); 
        $("#editEventCountries").val(response.eventData.countryId); 

        loadCities("#editEventCountries", "#editEventCities"); 
        $("#editEventCities").val(response.eventData.cityId); 

        $("#editEventStartDate").val(response.eventData.startDate); 
        $("#editEventEndDate").val(response.eventData.endDate); 
        $("#editEventUserName").val(response.eventData.userName); 
        $("#editEventName").val(response.eventData.eventName); 
        $("#editEventDetails").val(response.eventData.details); 
      }, 
      error: function(jqXHR, textStatus, errorThrown){    
       /* log the error to the console */ 
       console.log(
        "The following error occured: " + 
        textStatus, errorThrown 
       ); 
      } 
     }); 

     // Get the overlay with the form for editing to pop up        
     $('#editEventModal').reveal(); 
    } 
    break; 
    default: 
     // oops, something wrong happened 
    break; 
} 
return false; 
} 

// Here is the load continents function 
function loadContinents(continentObj) {  
// fetch continent data 

$.ajax({ 
    type: "GET", 
    url: "scripts/fetchContinents.php", 
    dataType: "json", 
    success: function(data){ 
     /* On Successful Posting to server side */ 

     // Add fetched options to the select object responsible for holding the continents list 
     $(continentObj).empty(); //clear current available selections 
     if(data == ""){ 
      $(continentObj).append("<option value=\"\">No continents found</option>"); 
     } 
     else{ 
      for(i = 0; i < data.id.length; i++){ 
       $(continentObj).append("<option value=\"" + data.id[i] + "\">" + data.name[i] + "</option>"); 
      } 
     } 

    }, 
    error: function(jqXHR, textStatus, errorThrown){ 
     /* log the error to the console */ 
     console.log(
      "The following error occured: " + 
      textStatus, errorThrown 
     ); 

     $(countryObj).append("<option selected value=\"\">Select Continent</option>"); 

     $("#searchEventError").fadeOut(200); 
     $("#searchEventError").fadeIn(200).html("<div class=\"alert-box error\">Something went wrong with the server request, please try again later</div>"); 
    } 
}); 

return false; 
} 

// Load Countries 
function loadCountries(continentObj, countryObj, cityObj) { 
var continentOption = $(continentObj).val(); 

// clear/reset countries and cities selections 
$(countryObj).empty(); 
$(cityObj).empty().append("<option selected value=\"-1\">Please Select Country First</option>"); 

$.ajax({ 
    type: "GET", 
    url: "scripts/fetchCountries.php?continent=" + encodeURIComponent(continentOption), 
    dataType: "json", 
    success: function(data){ 
     /* On Successful Posting to server side */ 

     // Add fetched options to the select object responsible for holding the countries list 
     if(data == ""){ 
      $(countryObj).append("<option value=\"0\">No countries found</option>"); 
     } 
     else{ 
      for(i = 0; i < data.id.length; i++){ 
       $(countryObj).append("<option value=\"" + data.id[i] + "\">" + data.name[i] + "</option>"); 
      } 
     } 

    }, 
    error: function(jqXHR, textStatus, errorThrown){ 
     /* log the error to the console */ 
     console.log(
      "The following error occured: " + 
      textStatus, errorThrown 
     ); 

     $(countryObj).append("<option selected value=\"-1\">Please Select Continent First</option>"); 

     $("#searchEventError").fadeOut(200); 
     $("#searchEventError").fadeIn(200).html("<div class=\"alert-box error\">Something went wrong with the server request, please try again later</div>"); 

    } 
}); 

return false; 
} 

// Load Cities 
function loadCities(countryObj, cityObj) { 
var countryOption = $(countryObj).val(); 

// clear/reset cities selections 
$(cityObj).empty(); 

$.ajax({ 
    type: "GET", 
    url: "scripts/fetchCities.php?country=" + encodeURIComponent(countryOption), 
    dataType: "json", 
    success: function(data){ 
     /* On Successful Posting to server side */ 

     // Add fetched options to the select object responsible for holding the cities list  
     if(data == ""){ 
      $(cityObj).append("<option value=\"0\">No cities found</option>"); 
     } 
     else{ 
      for(i = 0; i < data.id.length; i++){ 
       $(cityObj).append("<option value=\"" + data.id[i] + "\">" + data.name[i] + "</option>"); 
      } 
     } 

    }, 
    error: function(jqXHR, textStatus, errorThrown){    
     /* log the error to the console */ 
     console.log(
      "The following error occured: " + 
      textStatus, errorThrown 
     ); 

     $(cityObj).append("<option selected value=\"-1\">Please Select Country First</option>"); 

     $("#searchEventError").fadeOut(200); 
     $("#searchEventError").fadeIn(200).html("<div class=\"alert-box error\">Something went wrong with the server request, please try again later</div>"); 
    } 
}); 

return false; 
} 
+0

ブラウザが応答しなくなったとすると、完全にロックされているとは限りませんか?あなたはアドレスバーに入力することはできませんし、別のサイトに行く?それともJavascriptが処理を停止するということですか? – AlexC

+0

ブラウザがロックされているように見えます。 問題は次のとおりです。なぜ$( "#editEventContinents")がval(continentId)でないのですか?その大陸の国を取得するために大陸のオプションで選択された値に固執しますか? フェッチの国のクエリー文字列で大陸のパラメータが空になっているということが起こりました(私はその間に見つけました)。結果として同じことが都市に起こります。 –

+0

完全なコードを見ると便利です。 loadContinentsが何をしているのか分からない限り、ブラウザに致命的なものとして私に飛び出すものはありません。完全なコードを投稿してください、私は見てみましょう。 – AlexC

答えて

1

あなたloadCountriesprocessRowの内部に含まれているので、イベント値が戻ってくるとされていますしかし、loadCountriesが来て、その値を再び上書きしています。

私はあなたの国と都市のデータをインラインロードで選択した値に基づいて引き出すことをお勧めしますので、複数回待つ必要はありません。選択した大陸のすべての国、選択した国のすべての都市、イベントの詳細をJSONで出力するように、イベントの詳細を取得します。あなたが試すことができます

他の事は(私はこれを示唆していない)それぞれが、このような次のを待つ必要があるように、あなたのAJAX呼び出しをネストされています。私は何を意味

function processRow(command, id) { 
    console.log('Starting the processing of row #' +id); 
    switch(command){ 

     case 'Delete': {  
      $("#deleteId").val(id); 
      $('#deleteEventModal').dialog("open"); 
     } 
     break; 
     case 'Edit': { 
      /* Fetch Data to Fill up the form */  

      $.ajax({ 
       type: "GET", 
       url: "stackAjax.php?Action=Event&eventId=" + encodeURIComponent(id), 
       dataType: "json", 
       success: function(response){ 
        $("#editEventStartDate").val(response.eventData.startDate); 
        $("#editEventEndDate").val(response.eventData.endDate); 
        $("#editEventUserName").val(response.eventData.userName); 
        $("#editEventName").val(response.eventData.eventName); 
        $("#editEventDetails").val(response.eventData.details); 
        $("#editEventContinents").val(response.eventData.continentId); 

        /* On Successful Posting to server side */ 
        window.currentContinent = response.eventData.continentId; 
        window.currentCountry = response.eventData.countryId; 
        window.currentCity = response.eventData.cityId; 

        var countryObj = $("#editEventCountries"), 
        cityObj = $("#editEventCities"); 

        $(countryObj).empty(); 
        $(cityObj).empty().append("<option selected value=\"-1\">Please Select Country First</option>"); 

        $.ajax({ 
         type: "GET", 
         url: "stackAjax.php?Action=Countries&continent=" + encodeURIComponent(window.currentContinent), 
         dataType: "json", 
         success: function(countryData){ 
          /* On Successful Posting to server side */ 

          // Add fetched options to the select object responsible for holding the countries list 
          if(countryData == ""){ 
           $(countryObj).append("<option value=\"0\">No countries found</option>"); 
          } 
          else{ 
           for(i = 0; i < countryData.id.length; i++){ 
            $(countryObj).append("<option value=\"" + countryData.id[i] + "\">" + countryData.name[i] + "</option>"); 
           } 
          } 

          $(cityObj).empty(); 

          console.log('about to set the country');   
          $("#editEventCountries").val(response.eventData.countryId); 
          $.ajax({ 
           type: "GET", 
           url: "stackAjax.php?Action=Cities&country=" + encodeURIComponent(window.currentCountry), 
           dataType: "json", 
           success: function(cityData){ 
            /* On Successful Posting to server side */ 

            // Add fetched options to the select object responsible for holding the cities list  
            if(cityData == ""){ 
             $(cityObj).append("<option value=\"0\">No cities found</option>"); 
            } 
            else{ 
             for(i = 0; i < cityData.id.length; i++){ 
              $(cityObj).append("<option value=\"" + cityData.id[i] + "\">" + cityData.name[i] + "</option>"); 
             } 
            } 

           console.log('about to set the city');  
           $("#editEventCities").val(response.eventData.cityId); 
           }, 
           error: function(jqXHR, textStatus, errorThrown){    
            /* log the error to the console */ 
            console.log(
             "The following error occured: " + 
             textStatus, errorThrown 
            ); 

            $(cityObj).append("<option selected value=\"-1\">Please Select Country First</option>"); 

            $("#searchEventError").fadeOut(200); 
            $("#searchEventError").fadeIn(200).html("<div class=\"alert-box error\">Something went wrong with the server request, please try again later</div>"); 
           } 
          }); 
         }, 
         error: function(jqXHR, textStatus, errorThrown){ 
          /* log the error to the console */ 
          console.log(
           "The following error occured: " + 
           textStatus, errorThrown 
          ); 

          $(countryObj).append("<option selected value=\"-1\">Please Select Continent First</option>"); 

          $("#searchEventError").fadeOut(200); 
          $("#searchEventError").fadeIn(200).html("<div class=\"alert-box error\">Something went wrong with the server request, please try again later</div>"); 

         } 
        }); 

        //console.log('A: Country DB: ' + response.eventData.countryId); 
        //$("#editEventCountries").change(); 

        //console.log('A: Selected Country: ' + $("#editEventCountries").val()); 


        //console.log('A: Selected City: ' + $("#editEventCities").val()); 

        //$('#editEventModal').dialog("open"); 
       }, 
       error: function(jqXHR, textStatus, errorThrown){    
        /* log the error to the console */ 
        console.log(
         "The following error occured: " + 
         textStatus, errorThrown 
        ); 
       }, 
       complete: function(){ 

       } 
      }); 
     } 
     break; 
     default: 
      alert("Don't know what to do but id is "+id); 
     break; 
    } 
    return false; 
} 
+0

よろしくお願いいたします。ありがとうございます!私は後で結果を投稿します:) –

+0

Hum ...もっと詳しく調べてみました。私はあなたが間違った場所を見ていたと思います。 これらのダイナミックボックスは他のものにも使用できます。 問題は次のとおりです。テーブルの行をクリックして[編集]を押します。私はその記録を更新したい。レコードには、大陸、国、都市に基づいた場所があります。 したがって、編集フォームにもこれらの動的ドロップダウンがあります。 問題は次のとおりです。大陸から来ている大陸は大丈夫です。私はval()メソッドを使用して、私が得たレコードを使用してフォームのドロップダウンを設定します。 ただし、国のデータを取得できません。等々。 –

+0

私が言っていることは、あなたが書いたものと私が書いたものとの間の唯一の実用的な違いは、あるボックスから別のボックスへの変更の引き金を呼び出すことです。 –

1

です:それはそうですあなたが書いたものと私が書いたものとの間の唯一の実用的な違いのように、あるボックスから別のボックスへの変更のトリガーを明示的に呼び出すことです。

一方、私もこのようなものを持っている:

$("#editEventContinents").change(function(){ // continents on change 
    return loadCountries("#editEventContinents", "#editEventCountries", "#editEventCities"); 
}); 

$("#editEventCountries").change(function(){ // countries on change 
    return loadCities("#editEventCountries", "#editEventCities"); 
}); 

を意味し、それは右、ほとんど同じでしょうか?

私の問題は実際にはテーブル行を編集して結果的に更新するためです。それがドロップダウンが失敗する場所です。

loadContinents("#editEventContinents"); 
$("#editEventContinents").val(response.eventData.continentId); 

のようなものを使用すると、大陸のドロップダウンボックスで変更イベントをトリガしません。私がそれを明示的に行った場合、その結果もうまくいきません。国を読み込んでも、選択されたオブジェクトはまだ識別されません。これは、クエリ文字列がバックエンドに空になり、すべての混乱が起こることを意味します。

関連する問題