TreeView
Checkboxes
This view shows how to show checkboxes in a TreeView control.
Features
Settings
Description
Set the ShowCheckboxes property to true and the TreeView will add checkboxes to each node.
When checkboxes are displayed, the TreeView manages their hierarchy so that when a checkbox is checked or cleared, the new value is automatically applied to all child nodes, and reflected on the state of the parent nodes.
When items are checked or unchecked, the OnClientCheckedItemsChanged event is raised, and the checkedItems property of the client TreeView object is updated with a list of the items that are currently checked.
The checkboxes are only shown when TreeView works without lazy nodes. That's to say, the second parameter lazyLoadActionUrl in the Bind method and the OnClientLazyLoadFunction property are not set.
The CheckOnClick property set to 'true' enables toggling check when click on entire item, not only on checkbox.
The CheckedMemberPath property determines which property of TreeView's data items, for example 'NewItem', being assigned by checkboxes value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using MvcExplorer.Models; using System.Web.Mvc; using System.Collections.Generic; namespace MvcExplorer.Controllers { public partial class TreeViewController : Controller { // GET: Checkboxes public ActionResult Checkboxes(FormCollection collection) { IValueProvider data = collection; _treeViewCheckboxesDataModel.LoadPostData(data); ViewBag.DemoOptions = _treeViewCheckboxesDataModel; return View(Property.GetData(Url)); } private readonly ControlOptions _treeViewCheckboxesDataModel = new ControlOptions { Options = new OptionDictionary { { "CheckOnClick" , new OptionItem{Values = new List< string > { "True" , "False" },CurrentValue = "False" }}, { "CheckedMemberPath" , new OptionItem{Values = new List< string > { "null" , "NewItem" },CurrentValue = "null" }} } }; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | @model Property[] @ { ControlOptions optionsModel = ViewBag.DemoOptions; ViewBag.DemoSettings = true ; } @section Scripts{ <script type="text/javascript"> function checkedItemsChanged(treeView) { //Showing NewItem status var selectedNode = treeView.selectedNode; if (selectedNode) { formatItemRecurrsive(treeView, selectedNode); } //Showing checked items status list var items = treeView.checkedItems, msg = '' ; if (items.length) { msg = '<p><b>@Resources.TreeView.Checkboxes_CheckedItems</b></p><ol>\r\n' ; for ( var i = 0; i < items.length ; i++) { if (items[i].NewItem) { msg += '<li style="color:red">' + items[i].Header + ' <i>(NewItem = true)</i></li>\r\n' ; } else { msg += '<li>' + items[i].Header + ' <i>(NewItem = false)</i></li>\r\n' ; } } msg += '</ol>' ; } document.getElementById( 'tvChkStatus' ).innerHTML = msg; } //Custom Format Item for showing NewItem status function formatItem(treeView, node) { var imgNewItem = node.element.lastChild; if (node.dataItem.NewItem) { if (imgNewItem.tagName != "IMG" ) { imgNewItem = document.createElement( "img" ); imgNewItem.setAttribute( "style" , "height:20px; margin-left:3px; content:url(@Url.Content(" ~/Content/images/ new .png "));" ); node.element.appendChild(imgNewItem); } } else if (imgNewItem.tagName == "IMG" ) { node.element.removeChild(imgNewItem); } } //Custom Format Item and all sub-items function formatItemRecurrsive(treeView, node) { formatItem(treeView, node); var nodes = node.nodes; if (nodes && nodes.length > 0) { for ( var i = 0; i < nodes.length ; i++) { formatItemRecurrsive(treeView, nodes[i]); } } } </script> } @ (Html.C1().TreeView() .Bind(Model) .DisplayMemberPath( "Header" ) .ChildItemsPath( "Items" ) .ShowCheckboxes( true ) .CheckOnClick(Convert.ToBoolean(optionsModel.Options[ "CheckOnClick" ].CurrentValue)) .CheckedMemberPath(optionsModel.Options[ "CheckedMemberPath" ].CurrentValue) .OnClientFormatItem( "formatItem" ) .OnClientLoadedItems( "checkedItemsChanged" ) .OnClientCheckedItemsChanged( "checkedItemsChanged" )) < div id = "tvChkStatus" ></ div > @section Settings{ @Html .Partial( "_OptionsMenu" , optionsModel) } @section Summary{ < p > @Html .Raw(Resources.TreeView.Checkboxes_Text1)</ p > } @section Description{ < p > @Html .Raw(Resources.TreeView.Checkboxes_Text2)</ p > < p > @Html .Raw(Resources.TreeView.Checkboxes_Text3)</ p > < p > @Html .Raw(Resources.TreeView.Checkboxes_Text4)</ p > < p > @Html .Raw(Resources.TreeView.Checkboxes_Text5)</ p > < p > @Html .Raw(Resources.TreeView.Checkboxes_Text6)</ p > < p > @Html .Raw(Resources.TreeView.Checkboxes_Text7)</ p > } |