首页 > 解决方案 > 如何在控制器codeigniter中添加菜单以升序和降序排序?

问题描述

内容我的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class AutoLoadDiv extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('ngoding/AutoLoad');
    }

    public function getData() {

        $this->load->library('table');
        $err = file_get_contents("application\logs\log.php");
        $filestr = preg_replace(array('/(^|\R)ERROR\s*-\s*/', '/(^|\R)(.*?)\s*-->\s*/'), array('$1', '$1$2 '), $err);
        $arraySpacing = nl2br($filestr);
        $arr = explode("\n", $arraySpacing);

        $output = '<style> th, td {text-align: left; padding: 10px;} tr:nth-child(even){background-color#FFF;} tr:nth-child(odd){background-color:#CCC;} </style>';
        $output .= '<table border="1px solid #dddddd;" >'; 
        $output .= '<tr><th>Date</th><tr>';
        for ($i = count($arr)-1; $i >= 0; $i--) {
            $output.="<tr><td>$arr[$i]</td></tr>"; 
        }
        $output.="</table>"; 
        echo $output; 


        }
    }

尝试实现 Datatables 但无法正常工作后,此处的代码视图

    <html>
        <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js">


        <script>
            $(document).ready(function(){
            setInterval(function(){
            $("#table").load('AutoLoadDiv/getData')
            }, 5000);
            });
        </script>


        <style>

        </style>

        <title>Auto Load Page in Div using Jquery</title>
        </head>
        <body>
            <h1>Auto Load Page in Div</h1>
            <div id="content">Please wait....</div>
            <div class="container">


                <table id="table" class="display" cellspacing="0" width="100%">

                    <tr>
                        <th>Date</th>
                    </tr>

                </table>
            </div>

            <script type="text/javascript">
                var table;
                table = $(document).ready(function() {

                    //datatables
                    table = $('#table').DataTable({

                        "processing": true,
                        "serverSide": true,
                        "order": [],

                        "ajax": {
                            "url": "<?php echo site_url('AutoLoadDiv/getData')?>",
                            "type": "POST"
                        },


                        "columnDefs": [
                            {
                                "targets": [ 0 ],
                                "orderable": false,
                            },
                        ],

                    });

                });

            </script>
        </body>
        </html>

在我实现数据表之后,结果视图如下:

https://ibb.co/keTQYe

我在实现我的代码以使菜单像下拉菜单那样像这个例子一样升序和降序时遇到问题:https : //ibb.co/chO2Bz,我想在控制器中实现html asc和dsc,而不是在视图中,如果我点击那个菜单是升序排序,如果再次单击该菜单是降序排序?

标签: phphtmlcsscodeigniter

解决方案


推荐阅读