js树不显示数据

lx0bsm1f  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(241)

我试图使用js树来显示用户有权访问哪些页面,数据正在从后端正确提取,因为我可以在网络部分看到相同的(按f12打开开发工具后),但它没有得到显示。
下面是我写的js:

$(document).ready(function () {
    var url = '../../controller/usermgmt/rolemgmt.php';

    var cbArray = [],
        cbCount = 0;
    var staff = "";   
    $('#staffType').change(function () {
        staff = $(this).val();
        //        $('[type="checkbox"]').each(function(){
        //            $(this).prop('checked', false);
        //        })
        //   alert(staff);
        htm = ' <ul class="jstree-container-ul jstree-children jstree-wholerow-ul jstree-no-dots" role="group">';
        if (staff != "") {
            cbCount = 0;
            cbArray = [];
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: {
                    reason: 'searchRole',
                    staff: staff
                },
                success: function (suc) {

                    if (suc['status'] == true) {
                        var parent_menu = suc['parent_menu'];
                        var sub_menu = suc['child_menu'];
                       // alert(sub_menu.length);
                        var inner_menu = '';
                        var count = 0;
                    //alert(parent_menu);
                         //$("#tree_2").html("hello");
                        createJSTree(suc['parent_menu']);
                       //console.log( suc['parent_menu']) ;

                    }
                }, 
                error: function (error) {
                    alert(error);
                }
            });
        }
    });

      function createJSTree(parent_menu) {
        $('#tree_2').jstree({
            'core': {
                'data': parent_menu
            }
        });
    }

});

下面是控制器文件:

<?php

include_once dirname(dirname(dirname(__FILE__))) . "/const.php";
include_once CONFIG_PATH . '/modal/usermgmt/rolemgmt.php';
require(CONFIG_PATH . "/config1.php");

$reason = isset($_POST['reason'])?$_POST['reason']:"";
$pgname = array();
$pgpath = array();
$parent_menu=array();
$sub_menu=array(); 
$child_page_count=0;

function fill_access_content1($conn) {
    $arr = [];
    $res = modal_fill_access_content($conn);

    foreach ($res as $row) {
        if ($row['parent_link_id'] == 0) {
            //Only checkbox.
?>
            <input name="chbox" id="<?php echo $row['pk_page_id'] ?>" type="checkbox" /><?php echo $row['alias_name'] ?>  <br><br>
<?php
        } else {
            //Collapsible.
?>
            <button id="<?php echo $row['pk_page_id'] ?>" name="collapsible" data-toggle="collapse" class="btn btn-link btn-lg col<?php echo $row['pk_page_id'] ?>" data-target="#div<?php echo $row['pk_page_id'] ?>">+</button><input class="parent" name="chbox" id="<?php echo $row['pk_page_id'] ?>" type="checkbox" /><?php echo $row['alias_name'] ?><br><br>
            <div name="<?php echo $row['pk_page_id'] ?>" class="cbcls<?php echo $row['pk_page_id'] ?>" id="div<?php echo $row['pk_page_id'] ?>" class="collapse"></div>
<?php
            $obj = (object)[
                'pkid' => $row['pk_page_id'],
                'child' => $row['parent_link_id'],
            ];
            $arr[] = $obj;
        }
    }
    $_SESSION['page_parent_array'] = $arr;
}

if ($reason == "search") {
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    $id = $_POST['id'];
    $arr = [];
    $arr = $_SESSION['page_parent_array'];

    foreach ($arr as $obj) {
        if ($obj->pkid == $id) {
            $child = $obj->child;
            break;
        }
    }
    $val = [];
    $val = searchChildren($conn, $child);
    echo json_encode($val);
}

if ($reason == "insert") {
    $cb = $_POST['chckBox'];
    $staff = $_POST['staff'];
    $val = insertRole($conn, $staff, $cb);
    echo json_encode($val);
}

if ($reason == "searchRole") {
    $staff = $_POST['staff'];
    $_SESSION['drop_down_staff_role_id'] =$_POST['staff'];
    //  $val = searchRole($conn, $staff);

    $page_name_by_roles=(array)getMenuByRole($conn, 10);
    // var_dump($page_name_by_roles);
    $all_menu=array();
    if (!empty($page_name_by_roles['data']) && $page_name_by_roles['status']===true ) {
        foreach($page_name_by_roles['data'] as $key=>$value) {
            $pgname[]= $value['page_name'];
            $pgpath[]= $value['path'];
            if ( $value['menu_parent_id']==0) {
                $parent_menu[]=   $value;
                $all_menu[]=array("id"=>$value['id'],"parent"=>"#", "text"=>$value['alias_name']);
            } else {
                $sub_menu[][$value['menu_parent_id']]= $value;
                $all_menu[]=array("id"=>$value['id'],"parent"=>$value['menu_parent_id'], "text"=>$value['alias_name']);
            }
        }
    }
    $htm="";
    $htm='<ul>
            <li>Root node 1
                <ul>
                    <li id="child_node_1">Child node 1</li>
                    <li>Child node 2</li>
                </ul>
            </li>
            <li>Root node 2</li>
        </ul>';
    // { "id": "ajson1", "parent": "#", "text": "Simple root node" },
    // var_dump($page_name_by_roles);
    // $page_name_by_roles;
    // var_dump($parent_menu);
    // var_dump($sub_menu); 
    if (is_array($parent_menu) && count($parent_menu)>0) {
        echo json_encode(array('status'=>true, 'parent_menu'=>$all_menu, 'child_menu'=>$sub_menu ));  
    } else {
        echo json_encode(array('status'=>false, 'parent_menu'=>$parent_menu, 'child_menu'=>$sub_menu ));  
    }
}

if ($reason == "fetch_pages") {
}
?>

控制器是

<?php
    function modal_fill_access_content($conn) {
        $query = "select pk_page_id, alias_name, menu_parent_id, parent_link_id from list_page_value where menu_parent_id = '0'";
        $stmt = $conn->prepare($query);
        $stmt->execute();
        $res = $stmt->fetchAll();
        return $res;
    }

    function searchChildren($conn, $child) {
        $res = [];
        $query = "select pk_page_id, alias_name from list_page_value where pk_page_id IN (".$child.")";
        $stmt = $conn->prepare($query);
        $stmt->execute();
        $res = $stmt->fetchAll();
        return $res;
    }

    function insertRole($conn, $staff, $cb) {
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(-1);
        try {
            $conn->beginTransaction();
            $query = "update tbl_access_mgmt set grant_access = '0' where fk_role_id = '$staff'";
            $stmt = $conn->prepare($query);

            if (!$stmt->execute()) {
                throw new Exception("0");
            }
            $fk_user_id = 0;

            $query = "INSERT INTO `tbl_access_mgmt`(`pk_access_id`, `fk_page_id`, `fk_role_id`, `grant_access`, `fk_user_id`, `timestamp`) VALUES";

            for ($i=0;$i<sizeof($cb); $i++) {
                if ($i == 0) {
                    $query .= "(NULL,'$cb[$i]', '$staff', '1', '$fk_user_id', CURRENT_TIMESTAMP)";
                } else {
                    $query .= ",(NULL,'$cb[$i]', '$staff', '1', '$fk_user_id', CURRENT_TIMESTAMP)";
                }
            }

            $query .= "ON DUPLICATE KEY UPDATE grant_access = VALUES(grant_access), fk_user_id = VALUES(fk_user_id), timestamp = VALUES(timestamp)";
            $stmt = $conn->prepare($query);

            if (!$stmt->execute()) {
                throw new Exception(implode(",", $stmt->showInfo()));
            }

            $conn->commit();
            $resp['status'] = true;
            $resp['msg'] = "Data successfully inserted.";
            return $resp;
        } catch (Exception $ex) {
            $conn->rollback();
            $resp['status'] = false;
            $resp['msg'] = "Can't complete the action. Error";
            return $resp;
        }
    }

    function searchRole($conn, $staff) {
        try {
            $query = "select fk_page_id from tbl_access_mgmt where fk_role_id = '$staff' and grant_access = '1'";
            $stmt = $conn->prepare($query);

            if (!$stmt->execute()) {
                throw new Exception("0");
            }
            $res = $stmt->fetchAll();

            $resp['status'] = true;
            $resp['msg'] = "Search Completed.";
            $resp['data'] = $res;

            return $resp;
        } catch (Exception $ex) {
            $resp['status'] = false;
            $resp['msg'] = "Can't complete the action. Error";
            return $resp;
        }
    }

    function getMenuByRole($conn, $role_id) {
        try {
            $query = "SELECT DISTINCT a.path, a.pk_page_id as id, a.page_name, a.alias_name, a.menu_parent_id, a.parent_link_id FROM list_page_value as a inner join tbl_access_mgmt as b inner join tbl_role_types as c on a.pk_page_id = b.fk_page_id WHERE b.grant_access =1 and c.pk_role_id =:role_id order by a.page_name";
            $stmt = $conn->prepare($query);

            if (!$stmt->execute(array(':role_id'=>$role_id))) {
                throw new Exception("0");
            }
            // $res = $stmt->fetchAll();
            $res =$stmt->fetchAll(PDO::FETCH_ASSOC);
            $resp['status'] = true;
            $resp['msg'] = "Menu listing Completed.";
            $resp['data'] = $res;
            return $resp;
        } catch (Exception $ex) {
            $resp['status'] = false;
            $resp['msg'] = "Can't complete the action. Error";
            return $resp;
        }
    }

以下js文件已并入其中:

<script src="<?=ASSET_PATH?>assets/global/plugins/jquery.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/js.cookie.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jquery.blockui.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/bootstrap-switch/js/bootstrap-switch.min.js" type="text/javascript"></script>
<!-- END CORE PLUGINS -->
<!-- BEGIN PAGE LEVEL PLUGINS -->
<script src="<?=ASSET_PATH?>assets/global/plugins/moment.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/morris/morris.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/morris/raphael-min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/counterup/jquery.waypoints.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/counterup/jquery.counterup.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/amcharts/amcharts.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/amcharts/serial.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/amcharts/pie.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/amcharts/radar.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/amcharts/themes/light.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/amcharts/themes/patterns.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/amcharts/themes/chalk.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/ammap/ammap.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/ammap/maps/js/worldLow.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/amcharts/amstockcharts/amstock.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/fullcalendar/fullcalendar.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/horizontal-timeline/horizontal-timeline.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/flot/jquery.flot.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/flot/jquery.flot.resize.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/flot/jquery.flot.categories.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jquery-easypiechart/jquery.easypiechart.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jquery.sparkline.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jqvmap/jqvmap/jquery.vmap.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jqvmap/jqvmap/maps/jquery.vmap.russia.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jqvmap/jqvmap/maps/jquery.vmap.world.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jqvmap/jqvmap/maps/jquery.vmap.europe.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jqvmap/jqvmap/maps/jquery.vmap.germany.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jqvmap/jqvmap/maps/jquery.vmap.usa.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jqvmap/jqvmap/data/jquery.vmap.sampledata.js" type="text/javascript"></script>
<!-- END PAGE LEVEL PLUGINS -->
<!-- BEGIN THEME GLOBAL SCRIPTS -->
<script src="<?=ASSET_PATH?>assets/global/scripts/app.min.js" type="text/javascript"></script>
<!-- END THEME GLOBAL SCRIPTS -->
<!-- BEGIN PAGE LEVEL SCRIPTS-->
<script src="<?=ASSET_PATH?>assets/global/plugins/jquery-idle-timeout/jquery.idletimeout.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jquery-idle-timeout/jquery.idletimer.js" type="text/javascript"></script>
<!-- <script src="<?=ASSET_PATH?>assets/pages/scripts/ui-idletimeout.min.js" type="text/javascript"></script>  -->
<!-- END PAGE LEVEL SCRIPTS -->
<!-- BEGIN THEME LAYOUT SCRIPTS -->
<script src="<?=ASSET_PATH?>assets/layouts/layout/scripts/layout.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/layouts/layout/scripts/demo.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/layouts/global/scripts/quick-sidebar.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/layouts/global/scripts/quick-nav.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/bootstrap-sweetalert/sweetalert.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/pages/scripts/ui-sweetalert.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/global/plugins/jstree/dist/jstree.min.js"></script>
<script src="<?=ASSET_PATH?>assets/pages/scripts/ui-tree.min.js" type="text/javascript"></script>
<script src="<?=ASSET_PATH?>assets/staff_js/usermgmt/rolemgmt.js"></script>

我不知道我错在哪里。认为它可能是js冲突,但看起来不像控制台日志没有显示任何错误。有什么提示吗???

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题