Browse Source

Liste de selection des années: remplissage en Ajax

olivier.massot 7 years ago
parent
commit
ab80deb82d

+ 8 - 0
CD67.FicheCollege.Entity/Extend/Annee.cs

@@ -10,6 +10,14 @@ namespace CD67.FicheCollege.Entity
     [MetadataType(typeof(Annee_Metadata))]
     public partial class Annee
     {
+        public Annee flat()
+        {
+            return new Annee()
+            {
+                Id = Id,
+                Libelle = Libelle
+            };
+        }
     }
 
     /// <summary>

+ 10 - 0
CD67.FicheCollege.Factory/AnneeFactory.cs

@@ -45,5 +45,15 @@ namespace CD67.FicheCollege.Factory
             return dbContext.Annees.Where(a => a.Libelle == annee_lib).First().Id;
         }
 
+        public List<Annee> getFlatList()
+        {
+            List<Annee> annees = new List<Annee>();
+            foreach (Annee annee in dbContext.Annees)
+            {
+                annees.Add(annee.flat());
+            }
+            return annees;
+        }
+
     }
 }

+ 9 - 0
CD67.FicheCollege.MVC/Controllers/AnneesController.cs

@@ -82,5 +82,14 @@ namespace CD67.FicheCollege.MVC.Controllers
             return View(model);
         }
 
+        [HttpGet]
+        public JsonResult GetList()
+        {
+            AnneeFactory fact = new AnneeFactory(db);
+            JsonResult res = Json(fact.getFlatList());
+            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
+            return res;
+        }
+
     }
 }

+ 25 - 0
CD67.FicheCollege.MVC/Scripts/cd67-Menu.js

@@ -35,6 +35,28 @@
     $("#select-annee").on("click", function (event) {
         $("#select-annee").hide();
         $("#yearpicker").show();
+
+        $.ajax({
+            type: "GET",
+            url: "/Annees/GetList",
+            data: '',
+            contentType: "application/json; charset=utf-8",
+            dataType: "json",
+            success: function (data) {
+                var comboBox = $("#yearpicker");
+                comboBox.html("");
+                $.each(data, function (index, itemData) {
+                    var option = $("<option />");
+                    option.val(itemData.Id);
+                    option.html(itemData.Libelle);
+                    if (itemData.Libelle == comboBox.data("default")) {
+                        option.attr('selected', 'selected');
+                    }
+                    comboBox.append(option);
+                });
+            }
+        });
+
         $("#yearpicker").focus();
         event.preventDefault();
     });
@@ -49,4 +71,7 @@
         $("#select-annee").show();
     });
 
+
+
+
 });

+ 4 - 15
CD67.FicheCollege.MVC/Views/Shared/_Layout.cshtml

@@ -54,19 +54,8 @@
                 <span id="titre" class="nav navbar">
                     <h1 id="app-title">Fiches collège 
                         <a id="select-annee" href="/Annees">@Model.Annee_Lib <sup class="fa fa-edit sup"></sup></a>
-                        
-                        <select name="yearpicker" id="yearpicker" value="@Model.Obj.Id" style="display:none;">
-                            @foreach (Annee annee in @Model.ListAnnees)
-                            {
-                                if (annee.Id == @Model.Obj.Id)
-                                {
-                                    <option selected value="@annee.Id">@annee.Libelle</option>
-                                }
-                                else
-                                {
-                                    <option value="@annee.Id">@annee.Libelle</option>
-                                }
-                            }
+                        <select name="yearpicker" id="yearpicker" data-default="@Model.Annee_Lib" style="display:none;">
+                            @* Rempli en Ajax *@
                         </select>
                     </h1>
                 </span>
@@ -77,7 +66,7 @@
                     <li class="search">
                         <form action="@Url.Action("Index", "Recherche", new { })" class="search-form">
                             <label for="recherche" class="search-field">
-                                <input type="text" class="form-control input-search" style="display:none;" name="recherche" id="recherche" placeholder="Rechercher..." data-auto="" value="@Request["recherche"]">
+                                <input disabled type="text" class="form-control input-search" style="display:none;" name="recherche" id="recherche" placeholder="Rechercher..." data-auto="" value="@Request["recherche"]">
                             </label>
                             <span class="icon-search fa fa-search fa-2x"></span>
                         </form>
@@ -103,7 +92,7 @@
                                             <p class="text-left"><strong>@utilisateurConnecte.login</strong></p>
                                             <p class="text-left small">@utilisateurConnecte.email</p>
                                             <p class="text-left">
-                                                <a href="#" class="btn btn-primary btn-block btn-sm">Paramètres</a>
+                                                <a disabled  href="#" class="btn btn-primary btn-block btn-sm">Paramètres</a>
                                             </p>
                                         </div>
                                     </div>