【问题标题】:can't get and pass variable uri segment to function for datatables server side无法获取并将变量 uri 段传递给数据表服务器端的函数
【发布时间】:2018-08-03 05:33:06
【问题描述】:

我正在使用 CodeIgniter 和 Datatable Server Side 来显示来自数据库的数据。我想从我的 url 视图中获取段 url 并将该段传递给我的控制器并显示到我表中的最后一列。但是没用。

这是我的查看网址

http://localhost/sosboard/admin/module/55

这是我的控制器

function data_module()
    {
        // Datatables Variables
        $submenu_id     = $this->uri->segment(3);
        $draw = intval($this->input->get("draw"));
        $start = intval($this->input->get("start"));
        $length = intval($this->input->get("length"));

        $module= $this->m_admin->main_menu();
        $data = array();

        //$page = $data['write'];
        foreach ($module->result() as $rows) {
            $data[] = array(
                $rows->title,
                $rows->seq,
                $rows->target,
                $rows->icon_code,
                $rows->active,
                $submenu_id


            );
        }
        $output = array(
           "draw" => $draw,
             "recordsTotal" => $module->num_rows(),
             "recordsFiltered" => $module->num_rows(),
             "data" => $data
        );
      echo json_encode($output);
    }

这是我的 html 视图和 javascript

<div class="panel-body">
        <table class='table' id="myTable">
            <thead>
                <tr>
                    <th>Main Menu</th>
                    <th>Seq</th>
                    <th>Target</th>
                    <th>Icon Code</th>
                    <th>Active</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div> 



<script>
    $(document).ready(function() {
        $('#myTable').DataTable({
            "ajax": {
                "url": "<?php echo site_url('admin/data_module/') ?>",
                "type": "GET"
            }
        });
    });
</script>

所有列都很好,但最后一列没有显示。

请帮忙。 谢谢

【问题讨论】:

  • 显示数据表html代码
  • 查看我的更新代码@PankajMakwana
  • 'admin/data_module/'第三个参数为空
  • @AbdullaNilam 我的意思是我想获取当前页面的 url。我的当前页面是localhost/sosboard/admin/module/55,我想获取段 3(模块)。
  • @MardinoIsmail 尝试第 4 段

标签: php codeigniter datatables serverside-javascript


【解决方案1】:

它不起作用,因为当您实际上通过 ajax 在页面 /admin/data_module/ 上时,您正试图从 uri /admin/module/55 获取片段。因此 uri 3 和 4 并不像您期望的那样存在。只有 uri 段:(1) sosboard (2) admin (3) data_module 存在于该页面中。

我建议将参数作为 get 传递

$(document).ready(function() {
        $('#myTable').DataTable({
            "ajax": {
                "url": "<?php echo site_url('admin/data_module/') ?>",
                "type": "GET",
                'data': { param1: '<?php echo $this->uri->segment(3); // module ?>' }
            }
        });
    });

数据模块:

function data_module()
    {
        // Datatables Variables
        $submenu_id     = $this->input->get('param1');

如果您需要从 uri 获取 55,请使用段 4。如果您需要同时传递两者,那么只需向 ajax 添加另一个参数并按照我展示的方式获取它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多