Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用dataTables显示动态表值_Javascript_Php_Jquery_Ajax_Datatables - Fatal编程技术网

Javascript 使用dataTables显示动态表值

Javascript 使用dataTables显示动态表值,javascript,php,jquery,ajax,datatables,Javascript,Php,Jquery,Ajax,Datatables,我在使用dataTables显示数据库中的值时遇到问题。 我无法触发$('#id').DataTable()将我的html表格变为dataTables,而简单而简单的html会正确地显示它。我认为我的问题在于ajax。。这是我的代码: 任何帮助都将不胜感激!:) $(函数(){ $(“.list link”)。单击(函数(e){//以获取我的 e、 stopPropagation();//侧手风琴 //动画 }); $(

我在使用dataTables显示数据库中的值时遇到问题。 我无法触发
$('#id').DataTable()
将我的html表格变为dataTables,而简单而简单的html会正确地显示它。我认为我的问题在于ajax。。这是我的代码:

任何帮助都将不胜感激!:)


$(函数(){
$(“.list link”)。单击(函数(e){//以获取我的
e、 stopPropagation();//侧手风琴
//动画
});                                     
$(“#schooltableDesc”).DataTable();
$(“#generateBtn”)。在(“单击”,function(){//onClick)上,ajax表
$(“#schooltableDesc”).empty();//将为空并将重新绘制
$(“#schooltableAsc”).empty();//带有基于下拉列表的新值
$(“#CollegetTableDesc”).empty();
$(“#CollegetTableAsc”).empty();
$(“#programtableDesc”).empty();
$(“#programtableAsc”).empty();
变量数据={
from:$('#datefrom').val(),
收件人:$('#dateto').val(),
}; 
$.ajax({
url:“/university analysis/where-between”,
数据:年份数据,
数据类型:“json”,
方法:“get”,
成功:功能(响应){
//$(“#总计”).html(response.h);
$(“#schooltableDesc”).html(response.fsD);
$(“#collegetableDesc”).html(response.cD);
$(“#programmabledesc”).html(response.pD);
$(“#schooltableAsc”).html(response.fsA);
$(“#collegetableAsc”).html(response.cA);
$(“#programtableAsc”).html(response.pA);
$(“#popu”).text(response.total#u登记);
$(“#男性”).text(回复:男性总数);
$(“#女性”).text(回复:女性总数);
$(“学校数量”)。文本(回复。学校);
$(“平均年龄”).text(response.avg_a);
}
}); 
这是表格的html

<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>

年龄
前五名附属学校
男性
女性
全部的
平均利率
@foreach(学校SD为$t)
@php
$age=$t->average;
$fs=$t->HS_学校;
$bp=$t->Male;
$gp=$t->女性;
$total=$t->total;
$avg=$t->arate;
@endphp
{{$age}
{{ucwords(strtolower($fs))}
{{$bp}
{{$gp}
{{$total}
{{number_格式($avg,3)}}
@endforeach
然后是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>"; 
//desc
$schoolsD=DB::table(“大众学生”)
->选择raw(“平均(日期差异(年份,[出生日期],GETDATE())作为“平均年龄”,HS_学校,SUM(IIF(性别='M',1,0))作为“男性”,SUM(IIF(性别='F',1,0))作为“女性”,计数(*)作为“总数”)
->whereRaw(sprintf(“在'%s-01-01'和('%s-12-31')之间录取的日期)和HS_学校!='',$yearFrom,$yearTo))
->whereRaw(“HS_学校!=”)
->groupBy(“HS_School”)->orderBy(“TOTAL”、“desc”)->get();
$responseSchoolsD=”
年龄
前五名附属学校
男性
女性
全部的
";
foreach(学校SD为$t){
$age=$t->average;
$fs=$t->HS_学校;
$bp=$t->Male;
$gp=$t->女性;
$total=$t->total;
$responseSchoolsD.=”;
$responseSchoolsD.=“.$age.”;
$responseSchoolsD.=.$fs.“;
$responseSchoolsD.=''.$bp.'';
$responseSchoolsD.=“.$gp.”;
$responseSchoolsD.=''.$total.'';
} 
$responseSchoolsD.=”;

您可以尝试获取项目,然后按如下方式填写表格:

   <script type="text/javascript">
        (function ($) {
        $(document).ready(function () {
        itemsTable = null,
        getItems = function () {
                            return $.Deferred(function () {
                                var that = this;
                                $.getJSON('/university-analysis/where-between', function (data) {
                                    that.resolve(data);
                                });
                            });
                        },

            $(".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(),

            }; 

            showItemsTable = function () {
                        return $.Deferred(function () {
                            var that = this;
                            getItems().done(function (itemsData) {
                                try {
                                   itemsTable = $("#schooltableDesc").DataTable({

                                        data: itemsData,
                                         columns: [
                                        DataTables.expandCol,
                                        {"data": 'age'},
                                        {"data": 'your data...'},
                                        {"data": 'your data...'},
                                        {"data": '...'},
                                        {"data": '...'},
                                        {"data": '...'},
                                        {"data": '...'}
                                    ]
                                    });
                                    // $("#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); 
                                    //console.log(itemsTable);

                                    that.resolve();
                                }catch (e) {
                                    console.log(e.message);
                                }
                            });
                        });
                    },
            showItemsTable();



             });//end of doc ready
       });</script>

(函数($){
$(文档).ready(函数(){
itemsTable=null,
getItems=函数(){
return$.Deferred(函数(){
var=这个;
$.getJSON('/university analysis/where-between',函数(数据){
解决(数据);
});
});
},
$(“.list link”)。单击(函数(e){//以获取我的
e、 stopPropagation();//侧手风琴
//动画
});                                     
$(“#schooltableDesc”).DataTable();
$(“#generateBtn”)。在(“单击”,function(){//onClick)上,ajax表
$(“#schooltableDesc”).empty();//将为空并将重新绘制
$(“#schooltableAsc”).empty();//带有基于下拉列表的新值
$(“#CollegetTableDesc”).empty();
$(“#CollegetTableAsc”).empty();
$(“#programtableDesc”).empty();
$(“#programtableAsc”).empty();
});
变量数据={
from:$('#datefrom').val(),
收件人:$('#dateto').val(),
}; 
showItemsTable
   <script type="text/javascript">
        (function ($) {
        $(document).ready(function () {
        itemsTable = null,
        getItems = function () {
                            return $.Deferred(function () {
                                var that = this;
                                $.getJSON('/university-analysis/where-between', function (data) {
                                    that.resolve(data);
                                });
                            });
                        },

            $(".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(),

            }; 

            showItemsTable = function () {
                        return $.Deferred(function () {
                            var that = this;
                            getItems().done(function (itemsData) {
                                try {
                                   itemsTable = $("#schooltableDesc").DataTable({

                                        data: itemsData,
                                         columns: [
                                        DataTables.expandCol,
                                        {"data": 'age'},
                                        {"data": 'your data...'},
                                        {"data": 'your data...'},
                                        {"data": '...'},
                                        {"data": '...'},
                                        {"data": '...'},
                                        {"data": '...'}
                                    ]
                                    });
                                    // $("#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); 
                                    //console.log(itemsTable);

                                    that.resolve();
                                }catch (e) {
                                    console.log(e.message);
                                }
                            });
                        });
                    },
            showItemsTable();



             });//end of doc ready
       });</script>
 $("#schooltableDesc").DataTable();