SoFunction
Updated on 2025-03-01

JS unlimited navigation menu implementation method

Navigation using layui rendering here

Entity Class

private int id; //id
  private String dept_name;  //Department name  private int pid;  //The superior department id  private int sort_num;  //Sort values  private int create_id; //Create  private String create_time; //Creation time  private int modify_id; //Modify  private String modify_time; //Last modified time  private int is_del; // Whether to delete 0 Not deleted 1 Deleted  private int status; //status
  private String remark; //remark
  private List<Dept> deptChild = new ArrayList<>(); //Department iteration

List<Dept> selectDeptForJsonMenu();

&lt;resultMap type="" &gt;
    &lt;result column="id" property="id" javaType="" /&gt;
    &lt;result column="dept_name" property="dept_name" javaType="" /&gt;
    &lt;result column="sort_num" property="sort_num" javaType="" /&gt;
    &lt;result column="create_time" property="create_time" javaType="" /&gt;
    &lt;result column="modify_id" property="modify_id" javaType="" /&gt;
    &lt;result column="modify_time" property="modify_time" javaType="" /&gt;
    &lt;result column="is_del" property="is_del" javaType="" /&gt;
    &lt;result column="status" property="status" javaType="" /&gt;
    &lt;result column="remark" property="remark" javaType="" /&gt;
    &lt;collection column="id" property="deptChild" javaType="" select="selectDeptForJsonMenuChild"/&gt;
  &lt;/resultMap&gt;
  &lt;!-- Query department menu tree --&gt;
  &lt;select  resultMap="deptFirst"&gt;
    SELECT * FROM sys_dept WHERE pid = 0 AND is_del = 0 ORDER BY sort_num
  &lt;/select&gt;
  &lt;select  resultMap="deptFirst" parameterType=""&gt;
    select * FROM sys_dept where pid = #{id} AND is_del = 0 ORDER BY sort_num
  &lt;/select&gt;

public List<Dept> selectDeptForJsonMenu(){
    return ();
  }

@RequestMapping(value = "selectDeptForJsonMenu",method={,})
  @ResponseBody
  public List<Dept> selectDeptForJsonMenu(){
    return ();
  }

html is introduced and the element module is loaded.

<div >
</div>
$(function(){
  $.ajax({
    method:'post',
    url:getPath+"dept/selectDeptForJsonMenu",
    dataType:'json',
    success:function(data){
      if( &gt; 0){
        var showlist = $('&lt;ul class="layui-nav layui-nav-tree"&gt;&lt;/ul&gt;');
        showall(data, showlist);
        $("#leftMenu").html(showlist);
      }else{ 
        $('#leftMenu').html('<span class="layui-btn layui-btn-primary" >Add</span>') ;      }
    }
  });
  //json is json data  //parent is the final html splicing content  function showall(json, parent) {
    for (var i in json) {
      //There are child nodes traversal      if (json[i]. &gt; 0) {
        //Create li        var li = $('&lt;li class="layui-nav-item layui-nav-itemed"&gt;&lt;/li&gt;');
        $(li).append('&lt;a href="javascript:;" rel="external nofollow" rel="external nofollow" &gt;'+json[i].dept_name+'&lt;/a&gt;');
        $(li).append('&lt;ul class="layui-nav-child"&gt;&lt;/ul&gt;').appendTo(parent);
        //Select UL Callback        showall(json[i].deptChild, $(li).children().eq(1));
      }
      //No children      else {
        $('&lt;li&gt;&lt;/li&gt;').append('&lt;a href="javascript:;" rel="external nofollow" rel="external nofollow" &gt;'+json[i].dept_name+'&lt;/a&gt;').appendTo(parent);
      }
    }
  }
});

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links