mui 側(cè)欄菜單實現(xiàn)多級樹數(shù)據(jù)列表,返回上一級樹

var currtTreeArr = []; //當前樹列表數(shù)組
var treeData = []; //根節(jié)點數(shù)據(jù)
var treeHistoryArr = []; //歷史打開的樹,用于回退,進入到其他節(jié)點
$(".mui-table-view").on("click", ".mui-navigate-right", function () { //列表冒泡監(jiān)聽點擊
  var idx = $(this).attr("index");
  if (currtTreeArr[idx].children.length) {
    //把點擊項的子樹顯示出來,并添加當前點擊的子樹到歷史
    currtTreeArr = currtTreeArr[idx].children;
    treeHistoryArr.push(currtTreeArr);
    renderTree(currtTreeArr);
  } else {
    renderLeafNode(currtTreeArr[idx].id); //葉子節(jié)點
  }
});

//渲染樹結(jié)構(gòu)列表
function renderTree(data) {
  if (treeHistoryArr.length > 1) {
    $("#goback").show(); //顯示返回上一級按鈕
  } else {
    $("#goback").hide();
  }
  var dom = "";
  data.forEach(function (item, idx) {
    dom +=
      ' <li class="mui-table-view-cell"><a class="mui-navigate-right" index=' +
      idx +
      ">" +
      item.name +
      "</a></li>";
  });
  $(".mui-table-view").empty().html(dom);
}

//顯示樹
function showTreeList() {
  treeHistoryArr = [];
  currtTreeArr = [{ //獲取到的樹數(shù)據(jù)
    name: '根節(jié)點',
    id: 1,
    children: [{
      name: '一級1',
      id: 11,
      children: [...]
    }, {
      ...
    }]
  }, ...];
  renderTree(currtTreeArr);
  treeHistoryArr.push(currtTreeArr);
  mui(".mui-off-canvas-wrap").offCanvas("show");
}

function renderLeafNode(id) {
  ...葉子節(jié)點的渲染
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容