我在使用dataTables顯示數據庫中的值時遇到問題。 我不能觸發$('#id').DataTable()
神奇我的HTML表成dataTables,而簡單和純HTML顯示正確。我認爲我的問題是在阿賈克斯..所以這裏是我的代碼:使用dataTables顯示動態表值
任何幫助將higicke感謝! :))
<script type="text/javascript" src="{!! asset('js/jquery.datatables.min.js') !!}"></script>
<script type="text/javascript" src="{!! asset('js/jquery-3.2.1.min.js') !!}"></script>
<script type="text/javascript">
$(function(){
$(".list-link").click(function(e) { //for my
e.stopPropagation(); //siderbar accordion
//animation
});
$("#schooltableDesc").DataTable();
$("#generateBtn").on("click", function() { //onClick, the ajax tables
$("#schooltableDesc").empty(); //will be empty and will redraw
$("#schooltableAsc").empty(); //with new values based on the dropdown
$("#collegetableDesc").empty();
$("#collegetableAsc").empty();
$("#programtableDesc").empty();
$("#programtableAsc").empty();
var yearData = {
from: $('#datefrom').val(),
to: $('#dateto').val(),
};
$.ajax({
url: '/university-analysis/where-between',
data: yearData,
dataType: 'json',
method: 'get',
success: function (response) {
// $("#totals").html(response.h);
$("#schooltableDesc").html(response.fsD);
$("#collegetableDesc").html(response.cD);
$("#programtableDesc").html(response.pD);
$("#schooltableAsc").html(response.fsA);
$("#collegetableAsc").html(response.cA);
$("#programtableAsc").html(response.pA);
$("#popu").text(response.total_enrolled);
$("#males").text(response.total_males);
$("#females").text(response.total_females);
$("#no_of_schools").text(response.schools);
$("#ave_age").text(response.avg_a);
}
});
而這裏的表
<table class="table table-bordered" id="schooltableDesc">
<thead>
<th>Age</th>
<th>Top 5 Feeder Schools</th>
<th>Male</th>
<th>Female</th>
<th>Total</th>
<th>Average rate</th>
</thead>
<tbody>
@foreach($schoolsD as $t)
@php
$age = $t->AverageAge;
$fs = $t->HS_School;
$bp = $t->Male;
$gp = $t->Female;
$total = $t->TOTAL;
$avg = $t->arate;
@endphp
<tr>
<td>{{$age}}</td>
<td>{{ucwords(strtolower($fs))}}</td>
<td>{{$bp}}</td>
<td>{{$gp}}</td>
<td>{{$total}}</td>
<td>{{number_format($avg, 3)}}%</td>
</tr>
@endforeach
</tbody>
</table>
的HTML,然後控制器的AJAX:
//desc
$schoolsD = DB::table("vw_es_students")
->selectRaw("AVG(DATEDIFF(year, [DateOfBirth], GETDATE())) AS \"AverageAge\", HS_School, SUM(IIF(Gender = 'M', 1, 0)) AS \"Male\", SUM(IIF(Gender = 'F', 1, 0)) AS \"Female\", count(*) as \"TOTAL\"")
->whereRaw(sprintf("DateAdmitted BETWEEN '%s-01-01' AND ('%s-12-31') and HS_School != ''", $yearFrom, $yearTo))
->whereRaw("HS_School != ''")
->groupBy("HS_School")->orderBy("TOTAL", "desc")->get();
$responseSchoolsD = "<table class='table no-border' id='schooltableDesc'>
<thead>
<th>Age</th>
<th>Top 5 Feeder Schools</th>
<th>Male</th>
<th>Female</th>
<th>Total</th>
</thead><tbody>";
foreach($schoolsD as $t){
$age = $t->AverageAge;
$fs = $t->HS_School;
$bp = $t->Male;
$gp = $t->Female;
$total = $t->TOTAL;
$responseSchoolsD .= "<tr>";
$responseSchoolsD .= "<td>" . $age . "</td>";
$responseSchoolsD .= "<td>" . $fs . "</td>";
$responseSchoolsD .= "<td>" . $bp . "</td>";
$responseSchoolsD .= "<td>" . $gp . "</td>";
$responseSchoolsD .= "<td>" . $total . "</td>";
}
$responseSchoolsD .= "</tr></table>";
您需要使用'$(「#schooltableDesc」)成功回調行更改後重繪它。DataTable()。fnDraw();'我猜! –
這樣的事情,先生? '成功:函數(響應){$(「#schooltableDesc」).DataTable()。html(response.fsD);' – jeanawhou