2013-03-20 3 views
1

У меня есть сценарий, в котором я загружаю html в div в событии callback ajax. Моя цель - добавить еще один div в только что загруженный div после загрузки основного содержимого. Основная проблема заключается в том, что загружается большое количество html, и мне приходится ждать, пока нагрузка не наступит.Событие триггера после загрузки div с контентом из ajax-вызова

Основные шаги заключаются в следующем.

@using (Ajax.BeginForm 
(
    Model.PostAction, 
    Model.PostController, 
    null, 
    new AjaxOptions() { OnSuccess = "contentLoadSuccess", OnFailure = "contentLoadFailure" }, 
    new { id = "reportBase_frmViewer", name = "reportBase_frmViewer" }) 
) 
{ 
    ... 
} 

function contentLoadSuccess(ajaxContext) { 
    showWaitIndicator(false); 
    if (ajaxContext.Format == null) 
     //If the format is not there then the server reponded but the an unhandled exception was caught and thrown 
     //the content will be the user friendly version 
     setReportContent(ajaxContext); 
    else { 
     if (ajaxContext.Format == "HTML") { 
      //If the format is HTML then it is already in the result, inject it into the content container 
      showWaitIndicator(true); 
      updatePageNumber(ajaxContext.PageNumber, ajaxContext.TotalPageNumber); 
      setReportContent(ajaxContext.HTMLContent); 
     } else 
      //Export option is used. The render link has a url friendly format to start the file download 
      window.location.href = ajaxContext.RenderLink + "&format=" + ajaxContext.Format; 
    } 
} 

function setReportContent(content) { 
     $("#reportContent").html(content); 
     $("#toggleToolbar").appendTo("#reportContent");//<- Does not work content takes to long and will overwrite appended div 
     $("#reportContent").show();   
    } 

$(document).ready(function() { 
    //This does not work either 
    $("#reportContent").load(function (e) { 
     $("#toggleToolbar").appendTo("#reportContent"); 
     $("#toggleToolbar").show();     
    }); 
}); 

$("#reportContent").on("load", function (event) { 
    alert("This does not work either"); 
    $("#toggleToolbar").appendTo("#reportContent"); 
}); 

Я заглянул в .live() с триггером загрузки, но я прочитал, что он обесценивается. Возможно, есть совершенно логичный способ, о котором я не думал. Или, может быть, я ошибаюсь. Любая помощь будет оценена по достоинству.

ответ

0

Попробуйте использовать тайм-аут

function setReportContent(content) { 
    $("#reportContent").html(content); 
    setTimeout(function(){ 
     $("#toggleToolbar").appendTo("#reportContent");//<- Does not work content takes to long and will overwrite appended div 
     $("#reportContent").show();   
    }, 200); 
} 
Смежные вопросы