2012-03-15 26 views
1

我使用的jqGrid樹電網具有以下配置如何在jqgrid中動態擴展列的級別?

colModel : 
     [ 
      { 
       name:'id',width : 30, editable : false, align:"right",sortable : false, hidden : true, key : true, hidedlg:true 
      }, 
      { 
       name:'no',width : 50, editable : false, align:"left", sortable : true,sorttype:"text" 
      } 
      }, 
      { 
       name:'name', width : 300, editable : true, sortable : true, sorttype:"text" 
      } 
     ], 

treeGridModel:'adjacency', 
ExpandColumn:'name' 
treeGrid: true, 
cellEdit: true, 
sortable: true, 

從上述配置中,名字是我的擴大領域,我基於從服務器中某些層次的JSON data.The用戶可能希望通過水平以改變運行時的層次結構。所以我應該改變名稱字段的級別和縮進。我可以使用「setCell」方法設置級別,但縮進未更新,如何更改縮進?請幫助

回答

0

設置level的新值並不是那麼容易。簡單使用setCell將不起作用。讓我們您知道rowid您要將level更改爲newLevel的值。我認爲代碼應該是關於以下內容:

var $iconDiv, iCol, 
    $tr = $('#' + $.jgrid.jqID(rowid)), //get the tree row 
    rowData = $myGrid.jqGrid('getLocalRow', rowid), 
    $grid = $("#list"), // grid 
    getColumnIndexByName = function (myGrid, columnName) { 
     var cm = myGrid.jqGrid('getGridParam', 'colModel'), i, 
      l = cm.length; 

     for (i = 0; i < l; i++) { 
      if (cm[i].name === columnName) { 
       return i; // return the index 
      } 
     } 
     return -1; 
    }; // get local row data 

// update level in the local jqGrid data 
rowData.level = newLevel; 

// update the position of the icon 
$iconDiv = $tr.find("div.tree-wrap"); 
$iconDiv.width((newLevel + 1) * 18); // 18px per level 
$iconDiv.children("div.treeclick:first").css("left", (newLevel * 18) + 'px'); 

// update the value in the hidden level column 
iCol = getColumnIndexByName.call($grid, "level"); 
$tr.children('td:eq(' + iCol + ')').text(newLevel); 
+0

我可以使用setTreeNode(iRow,level)嗎? – pavi 2012-03-15 11:14:13

+0

@pavi:我不確定,但我認爲你不能。爲了確保必須檢查'setTreeNode'的完整代碼並進行調試。 'setTreeNode(i,len)'內部將修改行索引爲'i'的行中的行,直到行索引爲'len'的行。如果我理解你是正確的,那麼你只需要用索引'iRow'來調整**只有一行的水平。所以'setTreeNode(iRow,level)'不會讓你需要。 – Oleg 2012-03-15 11:57:38

+0

不,我必須根據層次結構調整整個樹。我使用了setTreeNode,但有一件事我不明白的是該方法一次會產生多少個縮進?因爲對我來說它是縮進兩次。 – pavi 2012-03-15 12:31:25