2015-07-10 152 views
2

我試圖以顯示下拉列表中父子元素選擇list.as以下圖像enter image description here如何在下拉菜單中添加子項目的空間?

我的代碼中的foreach

@helper GetOption(List<Project_Cost_Management_System.Models.ChartOfAccount> model1, Guid? parentid) 
{ 
    foreach (var item3 in model1.Where(s => s.ParentAccountId.Equals(parentid))) 
    { 


     var zs3 = model1.Where(s => s.ParentAccountId == item3.Id); 
     int count3 = zs3.Count(); 
     if (count3 >= 0) 
     { 
      if(@item3.ParentAccountId == null) 
      { 
      <option value="@item3.Id">@item3.Name</option> 
      } 
      else 
      { 
       var cout = model1.Count(); 
       <option value="@item3.Id"> @item3.Name @model1.Where(s => s.dataid == @item3.dataparentid).Select(d => d.Name).First(). </option> 
      } 
     } 
     if (count3 > 0) 
     { 
      @GetOption(model1, item3.Id) 
     } 

    } 
} 

,但它表現爲enter image description here

如何我可以顯示爲第一張照片嗎?

+0

您可以發佈當前輸出演示嗎? –

+0

秒圖是 –

+0

我的意思是現場演示。我們如何分析代碼? :)你有一個動態代碼。 –

回答

0

我得到了答案。

添加模型類的層次另一個領域。

使用層次添加空間。我添加了我的代碼以供參考。

@helper GetOption(List<Project_Cost_Management_System.Models.ChartOfAccount> model1, Guid? parentid) 
{ 

    foreach (var item3 in model1.Where(s => s.ParentAccountId.Equals(parentid))) 
    { 

     var zs3 = model1.Where(s => s.ParentAccountId == item3.Id); 
     int count3 = zs3.Count(); 
     if (count3 >= 0) 
     { 
      if (@item3.ParentAccountId == null) 
      { 
       <option value="@item3.Id">@item3.Name</option> 
      } 
      else 
      { 
       int str = @item3.Hierarchy * 3; 
       string str1 = " ".ToString().PadRight(str); 
       str1 = str1.Replace(" ", "\u00a0"); 
       <option value="@item3.Id">@str1 @item3.Name</option> 

      } 
     } 
     if (count3 > 0) 
     { 
      @GetOption(model1, item3.Id) 
     } 

    } 
} 
1

您可以排序的實現你在找什麼用<optgroup>,所以你呈現的HTML將結束是這樣的:

<option value="...">Professional Fees</option> 
<optgroup label="Property Related Expenses"> 
    <option value="...">Maintenance Contribution</option> 
    ... 
</optgroup> 
... 

你可能有這樣做的唯一的問題是,您的實際分組本身並不是有效的選項,即你不能選擇「屬性相關的費用」,因爲它只是一個分組標籤。您也無法以這種方式真正控制右對齊的描述性文字。一般來說,HTML select元素相當嚴格,並且不允許進行大量的自定義。

當你需要更先進的功能,則必須將某種庫的創建模仿一個選擇列表中用更多的可定製的HTML元素的功能的「控制」。 interwebs上有一百萬個和一個不同的類庫,但我特別喜歡Select2.js。特別是針對您的情況,請參閱「模板」部分。

+1

如果我使用opgrop意味着我如何使選項組被選中的值,因爲父母也是一些孩子。 –

+0

你不行。這纔是重點。我只是指出,optgroup是最好的,你可以用直接的HTML做,這是不夠的。你將不得不爲此使用某種JS解決方案。 –

相關問題