2017-06-15 131 views
0

我目前正在爲客戶請求修改樹視圖控件(Telerik MVC Extensions)。他們的要求很簡單:如果樹中的項目具有附件,請在節點旁邊添加回形針以識別它。將圖像添加到樹視圖中的僅一個節點

我到目前爲止一直能夠做到這一點,但發現了一個這樣的小呃。我可以將圖像添加到具有附件的特定節點,但是,所有不應該沒有圖像的節點(因此,我的意思是它們應該在樹內顯得正常)。相反,我發現樹在回形針圖像的大小上留下了空白。

有沒有辦法動態關閉這個空白(如果沒有必要,也可以不添加圖片網址)?以下是我執行此過程的代碼(由於僅底層顯示附件,因此在樹的展開方法上完成)。

導航控制器

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult GetNextTreeViewLevel(TreeViewItem node) 
{ 
    ... 
    //If bottom layer, then execute the following 
    var data = _TreeRepo.GetProcessesByParcel(int.Parse(values[1]), cntTreeList); 
    nodes = from item in data 
       select new TreeViewItem 
       { 
       Text = item.strProcess, 
       Value = "PR" + "," + item.cntProcess.ToString(), 
       LoadOnDemand = false, 
       Enabled = true, 
       Selected = SelectedSearchResult.ToString().Length > 0 
             && SelectedSearchResult.ToString().Split('~').Length > 3 
             && decimal.Parse(SelectedSearchResult.ToString() 
              .Split('~') 
              .Last() 
              .Substring(2)) == item.cntProcess 
       ImageUrl = item.ysnHasAttachment.HasValue && item.ysnHasAttachment.Value == 1 
           ? @"/Content/NewImages/attachment.png" 
           : string.Empty 
       }; 
    return new JsonResult { Data = nodes }; 
} 

的是什麼樣子無/有碼圖像URL屏幕截圖:

enter image description here enter image description here

回答

0

我終於想出了一個解決的辦法問題。問題是我如何將數據添加到節點。原始邏輯在獲取數據以獲取IEnumerable對象之後執行Linq查詢。

因此,每個節點都試圖添加一個圖像(即使沒有)。因此,看起來很奇怪的空間。以下是我如何重寫這一邏輯以正確獲取我的數據。

var processNodes = new List<TreeViewItem>(); 
var data = _TreeRepo.GetProcessesByParcel(int.Parse(values[1]), cntTreeList); 

foreach (var item in data) 
{ 
    #region Process has at least one Attachment 
    if (item.ysnHasAttachment.HasValue && item.ysnHasAttachment.Value == 1) 
     processNodes.Add(new TreeViewItem 
     { 
      Text = item.strProcess, 
      Value = "PR" + "," + item.cntProcess.ToString(), 
      LoadOnDemand = false, 
      Enabled = true, 
      Selected = SelectedSearchResult.ToString().Length > 0 
         && SelectedSearchResult.ToString().Split('~').Length > 3 
         && decimal.Parse(SelectedSearchResult.ToString() 
         .Split('~') 
         .Last() 
         .Substring(2)) == item.cntProcess, 
      ImageUrl = "/Content/NewImages/smallAttachment.png" 
     }); 
    #endregion 
    #region Process has no Attachments 
    else 
     processNodes.Add(new TreeViewItem 
     { 
      Text = item.strProcess, 
      Value = "PR" + "," + item.cntProcess.ToString(), 
      LoadOnDemand = false, 
      Enabled = true, 
      Selected = SelectedSearchResult.ToString().Length > 0 
         && SelectedSearchResult.ToString().Split('~').Length > 3 
         && decimal.Parse(SelectedSearchResult.ToString() 
         .Split('~') 
         .Last() 
         .Substring(2)) == item.cntProcess 
     } 
    #endregion 
} 
nodes = processNodes; 

此時,您現在可以返回節點。那些應該有一個附件圖標將和那些不應該不會。有趣的是,4個月後,你可以拿出袖口以外的東西。