$(document).ready(function () 无法调用控制器中的函数

Posted

技术标签:

【中文标题】$(document).ready(function () 无法调用控制器中的函数【英文标题】:$(document).ready(function () unable to call function in controller 【发布时间】:2020-02-15 09:30:00 【问题描述】:

我正在使用数据表来显示数据库中的大量行。我使用下面的代码来显示从数据库中获取的数据,但随着数据开始增加,加载需要大量时间,因为选择查询中有多个连接。

以下是加载视图需要大量时间的代码:

    <script>
        $(document).ready(function() 
                $('#fileData').dataTable(
                    "aLengthMenu": [[5,10, 25, 50, 100, -1], [5,10, 25, 50, 100, "All"]],   
                    //"aaSorting": [[ 4, "desc" ]], 
                    "iDisplayLength": <?php echo 5; ?>, 
                    'bProcessing'    : true,
                    'bServerSide'    : false,
                    "oTableTools": 
                        "sSwfPath": "assets/media/swf/copy_csv_xls_pdf.swf",
                        "aButtons": []
                    ,
                    "oLanguage": 
                        "sSearch": "Filter: "
                    ,  
                    "aoColumns": [ 
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null
                    ],
                    "aoColumns": [
                        "bVisible": true,
                        "bVisible": true,
                        "bVisible": true,
                        "bVisible": true,
                        "bVisible": true,
                        "bVisible": true,
                        "bVisible": true,
                        "bVisible": true,
                        "bVisible": true,
                        "bVisible": true,
                                        "bVisible": true
                    ]   
                ).columnFilter( aoColumns: [
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                     type: "text", bRegex:true ,
                                 type: "text", bRegex:true 
                ]);
            );
    </script>
    <table id="fileData" class="table table-striped table-bordered table-hover table-full-width">
                                        <thead>
                                            <tr>
                                                <th>Sl.No</th>
                                                <th>Type</th>
                                                <th>No </th>
                                                <th>0-15 yrs M</th>
                                                <th>0-15 yrs F</th>
                                                <th>15-45 yrs M</th>
                                                <th>15-45 yrs F</th>
                                                <th>Above 45 yrs M</th>
                                                <th>Above 45 yrs F</th>
                                                                                            <th>Cumulative Since April</th>
                                                <th>Remarks</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php //if(is_object($proj_workers_report))   
                                        ?>
                                            <?php foreach ($nreports->result() as $index=>$row)  ?>
                                                <tr> 
                                                            <td> <?php echo $index + 1; ?> </td>
                                                            <td><?php echo $row->test1; ?></td>
                                                            <td><?php echo $row->test2; ?></td>
                                                            <td><?php echo $row->test3; ?></td>
                                                            <td><?php echo $row->test4; ?></td>
                                                            <td><?php echo $row->test5; ?></td>
                                                            <td><?php echo $row->test; ?></td>

                                                              </td>
                                                            <td></td>
                                                </tr>
                                            <?php
                                             ?>
                                        </tbody>
                                    </table>
After searching for solution to reduce loading time i found solution as enabling "serverSide":true.so i changed the code as below

$(document).ready(function () 
        var year="<?php echo base_url() . 'reportc/new_disease_morbidity_report'; ?>";
        alert(year);
        var dataTable = $('#example2').DataTable(  
          "processing":true,  
          "serverSide":true,  
          "order":[[ 0, "desc" ]],  
          "ajax":  
        url:"<?php echo base_url() . 'test/nreport'; ?>",   
        type:"POST",
        data:"'id':year",
        success: function (data) 
                alert("success");
            ,
            error: function () 
                alert('error');
            
        ,  
        'language': 
                "emptyTable":"No patient available"
            ,
        "columnDefs":[  
          
        //"targets":[0, 3],  
        //"orderable":false,  
        ,  
          ],  
         );  
        controller:
        function new_disease_morbidity_report()
            
                $year = $this->input->post('id');
                echo $year; 
        

但是新代码的问题是它无法调用控制器中的函数。谁能帮我解决这个问题。我有什么遗漏吗?

【问题讨论】:

请看这里***.com/a/17567235/5193536 How to call a function inside $(document).ready的可能重复 table datatable 有多少行一次可以轻松渲染50000条记录 首先检查使用您的 api 返回的行数 “无法调用函数”以什么方式?您观察到的错误或具体故障是什么?您在一个相当脱节的帖子中显示了很多代码。第二个代码 sn-p 的描述暗示它正在工作?您要描述的确切问题是什么? 【参考方案1】:

只需添加 "serverSide": true,您就是在告诉 Datatable 让我处理分页

为此,Datatable 将在请求 API 中附加一些请求参数,作为响应,您需要设置一些参数以使服务器端分页顺利工作。

你在正确的轨道上。只需要正确实施。

注意:因为 Datatable 会附加一些您需要在控制器级别处理它们的参数。将这些参数添加为@RequestParam。检查您的 API 调用以获取准确的参数。

1 Datatable 将在您的请求中添加以下参数POST/GET

    order:  asc
    start:  20
    length: 10

这将帮助您在 DB Query 中传递 LIMIT 参数。

SELECT * FROM User LIMIT 20,10;

2 您的 API 还应返回以下字段

"draw": 3,             // unique ID
"recordsTotal": 57,    // total number of records
"recordsFiltered": 57  // total number of filtered records

您可以查看示例演示 here 请检查并检查请求和响应

更多详情请参考this answer。

【讨论】:

我已经根据链接更改了我的代码,但仍然无法调用控制器中的函数。我需要在标题中包含一些脚本吗?我正在使用以下两个 请添加您的控制器以及添加服务器端 true 时从 UI 触发的 URL 这个网址:localhost/sunproject_master/reportc/… 控制器:函数 new_disease_morbidity_report() $this->load->model('reportm_test'); $get_refered_pa​​tients=$this->reportm_test->GetPatientsDetail(); $数据 = 数组(); $sub_array = 数组(); foreach($get_refered_pa​​tients as $row) $sub_array[] = $row->name; $sub_array[] = $row->unit_id; $data[] = $sub_array; $output = array( "draw"=>intval($_POST["draw"]), "recordsTotal"=>$this->reportm_test->GetAllPatientsDetail_data(), "recordsFiltered"= >$this->reportm_test->GetPatientsDetailFiltered_data(), "data"=>$data); echo json_encode($output);

以上是关于$(document).ready(function () 无法调用控制器中的函数的主要内容,如果未能解决你的问题,请参考以下文章

$(function) ready onload 等区别

在重新加载/刷新页面时,未调用dojo / ready函数

jQuery文档内部或外部的功能准备就绪

隐藏Flashdata消息

吸盘鱼褪色

jQuery 事件