2017-10-31 7 views
0

빈 테이블로 시작하는 일부 테이블이 있고 DataTables가 데이터에 대해 WebServer를 요청합니다.DataTables에서 AJAX 데이터를로드하는 동안로드 시간을 알려줍니다.

로드하는 데 몇 분이 걸릴 수도 있습니다. DataTables는 기본 Loading 메시지를 표시합니다. 하지만 간단한로드 텍스트 또는 이상한 애니메이션 대신로드가 실행되는 시간을 알려주는 카운터를 추가하고 싶습니다.

해당 설명서에서 수행 할 수있는 방법을 찾을 수 없습니다. 가능한가?

업데이트 : MonkeyZeus의 답변이 완벽하게 작동했습니다. 여기 내 마지막 코드입니다 : 또한

// ... 
,dataTablesLoading: function(e, settings, processing){ 
    setTimeout(function(){ 
     var targetJs = e.target; 
     var target = $(targetJs); 
     var timerContainer = target.find('.dataTables_empty'); 
//tlog(targetJs,'targetJs'); 
//tlog(target,'target'); 
//tlog(timerContainer,'timerContainer'); 

     if(processing){ 
       var timer = 0; 
       var timerHandler = setInterval(function(){ 
        timer++; 
        var hours = Math.floor(timer/3600); 
        var minutes = Math.floor((timer-(hours*60))/60); 
        var secs = timer-(hours*3600)-(minutes*60); 

        var timerText = hours+':'+minutes.lpad("0",2)+':'+secs.lpad("0",2); 
tlog(timerText,'timerText'); 
//tlog(timerContainer,'timerContainer'); 
        timerContainer.text("Loading... "+timerText); 
       },1000); 


      targetJs.setAttribute("data-loading-timer",timerHandler); 
tlog(timerHandler,'timerHandler processing'); 
     }else{ 
      var timerHandler = parseInt(targetJs.getAttribute("data-loading-timer")); 
tlog(timerHandler,'timerHandler not processing'); 

      if(timerHandler>0) 
       clearInterval(timerHandler); 
     } 

    },1000); 
} 
// ... 
$('#...') 
    .on('processing.dt', Framework.utils.dataTablesLoading) 
    .DataTable({...}) 
+0

상태, 행 수 등을 되돌릴 수있는 서버에 연결하지 않으면 절대로 진행률 표시 줄을 생각할 수 없습니다. 경과 시간을 보여주는 시계는 쉬워. – davidkonrad

+0

답장은 Tnx입니다. progressbar ou % 완료가 필요 없습니다. 나는 단지 1 분 1 초 분량의 카운터가 얼마나 오랫동안 로딩되었는지를 알기를 원한다. – Hikari

답변

1

First, you will need to enable processing when invoking the datatable:

$('#example').dataTable({ 
    "processing": true 
}); 

Next, you will need to declare what happens instead of the default Loading message using the dt namespace's processing event listener:

// This event will fire twice so pay attention to the processing parameter 
$('#example').on('processing.dt', function (e, settings, processing) { 
    if(processing === true) { 
     alert('Hey, we are processing!'); 
     // some custom code which targets #processingIndicator and applies some timer plug-in or whatever; you figure it out. 
    } 
    else { 
     alert('Hey, we are done processing!'); 
     // some custom code which targets #processingIndicator and hides it; you figure it out. 
    } 
}) 
.dataTable(); 

확실히 경우 https://ux.stackexchange.com/a/80858/45170을 확인하므로, 긴 로딩 시간도 고려해야 할 UX 측면을 당신은 더 좋은 경험을하고 싶습니다.

+0

감사합니다. 캐시 기능이 있습니다. 일단 데이터가로드되면 파일에 저장되고 해당 파일은 DB를 쿼리하는 대신 사용됩니다. 캐시는 1h 정도 지속되지만 데이터를 너무 오래되게 할 수 없으므로 업데이트해야합니다. – Hikari

+0

@Hikari 알겠습니다. cron 작업/예약 된 작업을 만들 수있는 능력이 있다면 사용자에게 부담을주지 않고 캐시 파일을 매시간 새로 고치도록 설정할 수 있습니다. – MonkeyZeus

+0

@Hikari 이걸 가진 행운? – MonkeyZeus