引导树视图插件转换为codigniter?

时间:2017-05-11 21:52:02

标签: php codeigniter plugins treeview

我想将Bootstrap Treeview插件转换为CodeIgniter,它在核心PHP上正常运行但是当我将其转换为CodeIgniter时,它只显示数组结果。

结果:My code output

来源:Bootstrap Treeview source我想将其转换为CodeIgniter

fetch.php - 控制器

<?php

class Fetch extends MY_Controller {

    public function index() {

        $this->load->view('dashboard');
        // $query (= " SELECT * FROM country_state_city ";
        $query = $this->db->query("SELECT * FROM country_state_city ");
        // (" SELECT * FROM country_state_city ")->result_array() ;

        //$output = array();
        foreach ($query->result_array() as $row) {
            $sub_data["id"] = $row["id"];
            $sub_data["name"] = $row["name"];
            $sub_data["text"] = $row["name"];
            $sub_data["parent_id"] = $row["parent_id"];
            $data[] = $sub_data;

        }


        foreach ($data as $key => &$value) {
            $output[$value["id"]] = &$value;
        }

        foreach ($data as $key => &$value) {
            if ($value["parent_id"] && isset($output[$value["parent_id"]])) {
                $output[$value["parent_id"]]["nodes"][] = &$value;
            }
        }

        foreach ($data as $key => &$value) {
            if ($value["parent_id"] && isset($output[$value["parent_id"]])) {
                unset($data[$key]);
            }
        }

        echo json_encode($data);

    }
}

?>

dashboard.php - view

<!DOCTYPE html>
<html>
<head>
    <title>ajax jaquey tree grid</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
    <script type="text/javascript" charset="utf8"
            src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.js"></script>

    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.css"/>

    <style>
    </style>
</head>
<body>
<br/><br/>
<div class="container" style="width:500px;">
    <h2 align="center">Make Treeview using Bootstrap Treeview Ajax JQuery with PHP</h2>
    <br/><br/>
    <div id="treeview"></div>
</div>
</body>
</html>

<script>
    $(document).ready(function () {
        $.ajax({
            url: "<?php echo base_url();?>fetch",
            method: "POST",
            dataType: "json",
            success: function (data) {
                $('#treeview').treeview({data: data});
            }
        });

    });
</script>

1 个答案:

答案 0 :(得分:0)

如果要使用ajax请求加载数据,则无需在index()函数中加载它。

您必须在index()中加载唯一的视图,并为树视图创建另一个函数,如下所示:

您在页面加载时加载所有数据,然后进行不正确的ajax调用。

<强>控制器:

<?php

class Test_c extends CI_Controller {

    public function index() {

        $this->load->view('test');


    }
    function tree() {
        // $query (= " SELECT * FROM country_state_city ";
        $query = $this->db->query("SELECT * FROM country_state_city ");
        // (" SELECT * FROM country_state_city ")->result_array() ;

        //$output = array();
        foreach ($query->result_array() as $row) {
            $sub_data["id"] = $row["id"];
            $sub_data["name"] = $row["name"];
            $sub_data["text"] = $row["name"];
            $sub_data["parent_id"] = $row["parent_id"];
            $data[] = $sub_data;

        }


        foreach ($data as $key => &$value) {
            $output[$value["id"]] = &$value;
        }

        foreach ($data as $key => &$value) {
            if ($value["parent_id"] && isset($output[$value["parent_id"]])) {
                $output[$value["parent_id"]]["nodes"][] = &$value;
            }
        }

        foreach ($data as $key => &$value) {
            if ($value["parent_id"] && isset($output[$value["parent_id"]])) {
                unset($data[$key]);
            }
        }

        echo json_encode($data);
    }
}

?>

试试这样。有用。 其他事情是正确的。

希望您使用我的解决方案解决您的错误。

...问候