最近咱們的表格中,須要更改數據的顯示方式,就是jqgrid中以樹形的方式顯示。請先看圖:html
就是簡單的這種從屬方式。可是由於這個沒有找到官方的文檔(後來找到了,可是發現是翻譯過來的,也不是很清楚),因此作出來稍微的有點困難,好在方法總比困難多。這個問題仍是解決了,在這裏我記錄一下。方便之後的小夥伴們運用,若是有大神的話,也歡迎指出錯誤。前端
首先是在jqgrid中須要添加東西,請看下面的demo:json
1 treeGrid: true, 2 treeGridModel: 'adjacency', //固定寫法,還有其餘的一種方式,可是那種沒有研究過 3 ExpandColumn : 'agent_id', // 4 ExpandColClick: true, //能不能點擊 false就不能點擊了,true就是能點擊 5 treeReader : { 6 //前面的四個參數須要在json中展現 7 level_field: "level", //級別,主要是就是說展示出來是第幾層,最高級是0,而後是1,2,3…… 8 parent_id_field: "parent", //用來標識哪一個是父元素(須要時字符串格式:「parent」:「123」是能夠的,可是若是是「parent」:123就錯了) 9 leaf_field: "is_leaf", //是否是根節點,false表示這個不是最後的節點,true表示是最後的節點。(若是這個是最後的節點,可是設置的仍是false,會發生一種狀況,就是點擊這個又從新加載了一遍數據) 10 expanded_field: "expanded", //是否是須要展開,false不展開,true展開 11 loaded_field:true // 12 },
就是這12行代碼,其實上面的寫在html中,前端的任務就算是完成了。可是考慮到還須要後臺數據的配合,因此下面仍是須要說一下, json數據的格式。數組
下面貼出來數據的格式:spa
1 [ 2 { 3 "agent_id": "2019-04-17", 4 "game_id": "134283522", 5 "level": 0, 6 "is_leaf": false, 7 "players": 42, 8 "rounds": 42, 9 "bets": 13650000, 10 "effects": 26650000, 11 "total_revenue": 8850000, 12 "fees": 650000, 13 "total_win_agent": -8850000, 14 "id":"134283522", 15 "expanded":false 16 }, { 17 "game_id": "134283522", 18 "agent_id": 96292, 19 "players": 42, 20 "rounds": 42, 21 "bets": "13650000", 22 "effects": "26650000", 23 "fees": "650000", 24 "total_win_agent": "-8850000", 25 "total_revenue": "8850000", 26 "level": 1, 27 "is_leaf": true, 28 "parent": "134283522", 29 "expanded":false 30 }, { 31 "agent_id": "2019-04-17", 32 "game_id": "134284035", 33 "level": 0, 34 "is_leaf": false, 35 "players": 6, 36 "rounds": 6, 37 "bets": 1800000, 38 "effects": 950000, 39 "total_revenue": 150000, 40 "fees": 50000, 41 "total_win_agent": -150000, 42 "id":"134284035", 43 "expanded":false 44 }, { 45 "game_id": "134284035", 46 "agent_id": 96292, 47 "players": 6, 48 "rounds": 6, 49 "bets": "1800000", 50 "effects": "950000", 51 "fees": "50000", 52 "total_win_agent": "-150000", 53 "total_revenue": "150000", 54 "level": 1, 55 "is_leaf": true, 56 "parent": "134284035" 57 }, 58 { 59 "game_id": "134284035", 60 "agent_id": 96292, 61 "players": 6, 62 "rounds": 6, 63 "bets": "1800000", 64 "effects": "950000", 65 "fees": "50000", 66 "total_win_agent": "-150000", 67 "total_revenue": "150000", 68 "level": 1, 69 "is_leaf": false, 70 "parent": "134284035", 71 "id":"123" 72 }, 73 { 74 "parent":"123", 75 "game_id": "134284035", 76 "agent_id": 96292, 77 "players": 6, 78 "rounds": 6, 79 "bets": "1800000", 80 "effects": "950000", 81 "fees": "50000", 82 "total_win_agent": "-150000", 83 "total_revenue": "150000", 84 "level": 2, 85 "is_leaf": true 86 } 87 ]
裏面除了原來的數據,還須要把我們須要的東西加進來。並且由於是個數組,因此這裏面的數據是有順序的,若是順序不對的話,顯示就有問題了。這裏須要注意翻譯