sudheen

sudheen

  • 1.4k
  • 313
  • 83.4k

Jquery

Sep 10 2015 12:37 AM
I tried creating tree view with following code
  1. namespace TreeView.Models
  2. {
  3. public class BookViewModel
  4. {
  5. public long Id
  6. {
  7. get;
  8. set;
  9. }
  10. public string Title
  11. {
  12. get;
  13. set;
  14. }
  15. public bool IsWritten
  16. {
  17. get;
  18. set;
  19. }
  20. }
  21. }
The following is the code snippet for the AuthorViewModel class.
  1. using System.Collections.Generic;
  2. namespace TreeView.Models {
  3. public class AuthorViewModel
  4. {
  5. public AuthorViewModel()
  6. {
  7. BookViewModel = new List < BookViewModel > ();
  8. }
  9. public int Id
  10. {
  11. get;
  12. set;
  13. }
  14. public string Name
  15. {
  16. get;
  17. set;
  18. }
  19. public bool IsAuthor
  20. {
  21. get;
  22. set;
  23. }
  24. public IList < BookViewModel > BookViewModel
  25. {
  26. get;
  27. set;
  28. }
  29. }
  30. }
Now we create a controller “HomeController” that has two action methods for both GET and POST requests. The action method's name is “Index”. The Get request action method returns a tree view in the UI whereas the POST request method gets the posted data from the UI. The following is the code snippet for HomeController.
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Web.Mvc;
  4. using TreeView.Models;
  5. namespace TreeView.Controllers
  6. {
  7. public class HomeController : Controller
  8. {
  9. [HttpGet]
  10. public ActionResult Index()
  11. {
  12. List<AuthorViewModel> model = new List<AuthorViewModel>();
  13. AuthorViewModel firstAuthor = new AuthorViewModel
  14. {
  15. Id = 1,
  16. Name = "John",
  17. BookViewModel = new List<BookViewModel>{
  18. new BookViewModel{
  19. Id=1,
  20. Title = "JQuery",
  21. IsWritten = false
  22. }, new BookViewModel{
  23. Id=1,
  24. Title = "JavaScript",
  25. IsWritten = false
  26. }
  27. }
  28. };
  29. AuthorViewModel secondAuthor = new AuthorViewModel
  30. {
  31. Id = 2,
  32. Name = "Deo",
  33. BookViewModel = new List<BookViewModel>{
  34. new BookViewModel{
  35. Id=3,
  36. Title = "C#",
  37. IsWritten = false
  38. }, new BookViewModel{
  39. Id=4,
  40. Title = "Entity Framework",
  41. IsWritten = false
  42. }
  43. }
  44. };
  45. model.Add(firstAuthor);
  46. model.Add(secondAuthor);
  47. return View("Index", model);
  48. }
  49. [HttpPost]
  50. public ActionResult Index(List<AuthorViewModel> model)
  51. {
  52. List<AuthorViewModel> selectedAuthors = model.Where(a => a.IsAuthor).ToList();
  53. List<BookViewModel> selectedBooks = model.Where(a => a.IsAuthor)
  54. .SelectMany(a => a.BookViewModel.Where(b => b.IsWritten)).ToList();
  55. return View();
  56. }
  57. }
  58. }
The preceding code shows how books are associated with an author in the GET action method and how to get a selected tree node (authors and books) in the POST request.

Bootstrap CSS is already added to the application but we write a new custom CSS for the tree view design. The following is the code snippet for the tree CSS.

  1. .tree li {
  2. margin: 0px 0;
  3. list-style-type: none;
  4. position: relative;
  5. padding: 20px 5px 0px 5px;
  6. }
  7. .tree li::before{
  8. content: '';
  9. position: absolute;
  10. top: 0;
  11. width: 1px;
  12. height: 100%;
  13. right: auto;
  14. left: -20px;
  15. border-left: 1px solid #ccc;
  16. bottom: 50px;
  17. }
  18. .tree li::after{
  19. content: '';
  20. position: absolute;
  21. top: 30px;
  22. width: 25px;
  23. height: 20px;
  24. right: auto;
  25. left: -20px;
  26. border-top: 1px solid #ccc;
  27. }
  28. .tree li a{
  29. display: inline-block;
  30. border: 1px solid #ccc;
  31. padding: 5px 10px;
  32. text-decoration: none;
  33. color: #666;
  34. font-family: 'Open Sans',sans-serif;
  35. font-size: 14px;
  36. font-weight :600;
  37. border-radius: 5px;
  38. -webkit-border-radius: 5px;
  39. -moz-border-radius: 5px;
  40. }
  41. /*Remove connectors before root*/
  42. .tree > ul > li::before, .tree > ul > li::after{
  43. border: 0;
  44. }
  45. /*Remove connectors after last child*/
  46. .tree li:last-child::before{
  47. height: 30px;
  48. }
  49. /*Time for some hover effects*/
  50. /*We will apply the hover effect the the lineage of the element also*/
  51. .tree li a:hover, .tree li a:hover+ul li a {
  52. background: #dd4814; color: #ffffff; border: 1px solid #dd4814;
  53. }
  54. /*Connector styles on hover*/
  55. .tree li a:hover+ul li::after,
  56. .tree li a:hover+ul li::before,
  57. .tree li a:hover+ul::before,
  58. .tree li a:hover+ul ul::before{
  59. border-color: #dd4814;
  60. }
  61. .tree-checkbox{
  62. margin :4px !important;
  63. }
  64. .tree:before {
  65. border-left: 1px solid #ccc;
  66. bottom: 16px;
  67. content: "";
  68. display: block;
  69. left: 0;
  70. position: absolute;
  71. top: -21px;
  72. width: 1px;
  73. z-index: 1;
  74. }
  75. .tree ul:after {
  76. border-top: 1px solid #ccc;
  77. content: "";
  78. height: 20px;
  79. left: -29px;
  80. position: absolute;
  81. right: auto;
  82. top: 37px;
  83. width: 34px;
  84. }
  85. *:before, *:after {
  86. box-sizing: border-box;
  87. }
  88. *:before, *:after {
  89. box-sizing: border-box;
  90. }
  91. .tree {
  92. overflow: auto;
  93. padding-left: 0px;
  94. position: relative;
  95. }
Now we create an Index view that renders in the browser and shows the tree view for the author and book. The following is the code snippet for the Index view.
  1. @model List
  2. <TreeView.Models.AuthorViewModel>
  3. @section head{
  4. @Styles.Render("~/Content/css/tree.css")
  5. }
  6. <div class="panel panel-primary">
  7. <div class="panel-heading panel-head">Author Book Tree View</div>
  8. <div id="frm-author" class="panel-body">
  9. @using (Html.BeginForm())
  10. {
  11. <div class="tree">
  12. @for (int i = 0; i < Model.Count(); i++)
  13. {
  14. <ul>
  15. <li>
  16. <a href="#">
  17. @Html.CheckBoxFor(model => model[i].IsAuthor, new { @class = "tree-checkbox parent", @id = @Model[i].Id })
  18. <label for=@i>
  19. <strong>Author:</strong>
  20. @Html.DisplayFor(model => model[i].Name)
  21. </label>
  22. </a>
  23. <ul>
  24. @for (int j = 0; j < Model[i].BookViewModel.Count(); j++)
  25. {
  26. int k = 1 + j;
  27. @Html.HiddenFor(model => model[i].BookViewModel[j].Id)
  28. <li>
  29. <a href="#">
  30. @Html.CheckBoxFor(model => model[i].BookViewModel[j].IsWritten, new { @class = "tree-checkbox node-item", @iid = i + "" + j })
  31. <label for=@i@j>
  32. <strong>Book @(k):</strong> @Html.DisplayFor(model => model[i].BookViewModel[j].Title)
  33. </label>
  34. </a>
  35. </li>
  36. }
  37. </ul>
  38. </li>
  39. </ul>
  40. }
  41. </div>
  42. <div class="form-group">
  43. <div class="col-lg-9"></div>
  44. <div class="col-lg-3">
  45. <button class="btn btn-success" id="btnSubmit" type="submit">
  46. Submit
  47. </button>
  48. </div>
  49. </div>
  50. }
  51. </div>
  52. </div>
  53. @section scripts{
  54. @Scripts.Render("~/Scripts/tree.js")
  55. }
Thereafter we create an important part of this example. We create the JavaScript file tree.js with the following code.
  1. (function($)
  2. {
  3. function Tree() {
  4. var $this = this;
  5. function treeNodeClick()
  6. {
  7. $(document).on('click', '.tree li a input[type="checkbox"]', function() {
  8. $(this).closest('li').find('ul input[type="checkbox"]').prop('checked', $(this).is(':checked'));
  9. }).on('click', '.node-item', function() {
  10. var parentNode = $(this).parents('.tree ul');
  11. if ($(this).is(':checked')) {
  12. parentNode.find('li a .parent').prop('checked', true);
  13. } else {
  14. var elements = parentNode.find('ul input[type="checkbox"]:checked');
  15. if (elements.length == 0) {
  16. parentNode.find('li a .parent').prop('checked', false);
  17. }
  18. }
  19. });
  20. };
  21. $this.init = function() {
  22. treeNodeClick();
  23. }
  24. }
  25. $(function() {
  26. var self = new Tree();
  27. self.init();
  28. })
  29. }(jQuery))
Req:When child node selected then parent node to be selected .for this iam using above jquery.here they have not  given how to call jquery function in any event
 
In This code how to call above jquery function in any check event or any other events.