2016-08-31 29 views
0

我想重新加載/刷新從哪裏我可以獲取更新database.But表的ID,但問題是刷新只有表整個頁面重新加載。但我在這裏已經看到了'身份證'。如果有人罰款ajax的問題​​是什麼。希望能幫助我找到它..如何使用Ajax在特定時間後重新加載/刷新'id'?

我使用laravel這裏:

這裏是我的看法部分:

<table class="table table-striped table-bordered dataTable" id="example"> 
<thead> 
    <tr> 
     <td>Serial No</td> 
     <td>Title</td> 
     <td>Description</td>      
     <td>Action</td> 
    </tr> 
</thead> 
<tbody> 
    <?php $i=1; ?> 
    @foreach($items as $row) 
    <tr> 
     <td>{{$i}}</td> 
     <td class="title" data-id1="{{$row->id}}" contenteditable>{{$row->title}}</td> 
     <td class="description" data-id2="{{$row->id}}" contenteditable>{{$row->description}}</td> 
     <td>  
      <button type="button" onclick="deleteItem({{ $row->id }})" class="btn btn-danger">Delete</button> 
     </td> 
    </tr> 
    <?php $i++; ?> 
    @endforeach 
</tbody> 
</table> 

這裏是JavaScript和Ajax的部分:

<script> 
function autoRefresh_div() 
{ 
    $("#example").load(ajax());// a function which will load data from other file after x seconds 
} 
function ajax(){ 
    $.ajax({ 
     url:"{{url('listA')}}", 
     method:"get",              
     success: function(result) { 
      location.reload(); 
     } 
    }); 
} 
setInterval('autoRefresh_div()', 2000); // refresh div after 5 secs 
</script> 

這裏是路由部分:

Route::get('listA',function(Request $request){ 
    $items=\App\Item::all(); 
    return Response::json($items); 
}); 
+0

是在你的實際代碼的ID 「榜樣」?如果您在代碼中將您的ID更改爲「example」,它會出錯嗎? –

+0

我使用了'

' – User57

+1

因爲'location.reload();' – Pugazh

回答

2

你在這裏,如果你想在成功或之前想要這個,這取決於你。

setInterval(function() 
{ 
    $('#YOUR DIV').load(document.URL + ' #YOUR DIV'); 
}, 30000); 
+0

你確定嗎? 'load(document.URL +'#YOUR DIV');' – User57

+0

是的,我確定。請親自嘗試一下。它重新加載該div。 – Noman

+0

在這裏看到你的解決方案的問題是你代表它錯了.. 它應該是:'$(「#example」)。load(「list #example」);' 意味着你應該避免''+ ''sign – User57

0

由於location.reload();整個頁面正在刷新。嘗試下面的代碼。

function autoRefresh_div() { 
 
    $.ajax({ 
 
    url: "{{url('listA')}}", 
 
    method: "get", 
 
    success: function(result) { 
 
     $("#example").html(result); 
 
    } 
 
    }); 
 
} 
 

 
setInterval(autoRefresh_div(), 5000); // refresh div after 5 secs

+0

我用過你的代碼,但是它刷新了整個表格 – User57

+1

看起來你正在返回JSON,在這種情況下,你將不得不重建表格。可能更容易返回一些部分HTML。 –