Quellcode durchsuchen

Suchfunktion ergänzt

Arne Diekmann vor 5 Jahren
Ursprung
Commit
0b40c90473

+ 18 - 0
GreenTree.Strohrmann.ERP.Web/Controllers/CustomerController.cs

@@ -10,6 +10,7 @@ using GreenTree.Strohrmann.ERP.Services.Geolocator;
 using GreenTree.Strohrmann.ERP.Web.Extension;
 using GreenTree.Strohrmann.ERP.Web.Models.Business;
 using GreenTree.Strohrmann.ERP.Web.Models.Rights.User;
+using GreenTree.Strohrmann.ERP.Web.Models.Shared;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 
@@ -160,6 +161,23 @@ namespace GreenTree.Strohrmann.ERP.Web.Controllers
             return RedirectToAction(nameof(Index));
         }
 
+        // POST: CustomerController/Search/*term*
+        [HttpPost]
+        public ActionResult Search(SearchModel search)
+        {
+            if (search == null)
+                return new JsonResult(null);
+
+            var result = _eRPDbContext.Customers
+                .Where(c => c.Firstname.Contains(search.SearchTerm) ||
+                            c.Lastname.Contains(search.SearchTerm) ||
+                            c.CompanyName.Contains(search.SearchTerm))
+                .ToArray()
+                .Select(c => new CustomerModel(c));
+
+            return new JsonResult(result);
+        }
+
         #endregion
     }
 }

+ 9 - 0
GreenTree.Strohrmann.ERP.Web/Extension/HtmlContentExtension.cs

@@ -230,6 +230,15 @@ namespace GreenTree.Strohrmann.ERP.Web.Extension
 
         #region Helper
 
+        /// <summary>
+        /// Generates a random short ID string
+        /// </summary>
+        /// <param name="htmlHelper">HtmlHelper context.</param>
+        public static string RandomId(this IHtmlHelper htmlHelper)
+        {
+            return Guid.NewGuid().ToShortString();
+        }
+
         /// <summary>
         /// Converts an object in a JS literal object format
         /// </summary>

+ 6 - 0
GreenTree.Strohrmann.ERP.Web/Models/Business/CraftModel.cs

@@ -35,6 +35,11 @@ namespace GreenTree.Strohrmann.ERP.Web.Models.Business
         [Display(Name = "Kunde")]
         public CustomerModel Customer { get; set; }
 
+        /// <summary>
+        /// Craft customer id
+        /// </summary>
+        public int CustomerId { get; set; }
+
         /// <summary>
         /// Craft employees
         /// </summary>
@@ -68,6 +73,7 @@ namespace GreenTree.Strohrmann.ERP.Web.Models.Business
             Name = craft.Name;
             CreationDate = craft.CreationDate;
             Customer = new CustomerModel(craft.Customer);
+            CustomerId = craft.Customer.Id;
             CraftEmployees = craft.CraftEmployees
                 .Select(ce => new CraftEmployeeModel(ce))
                 .ToList();

+ 20 - 0
GreenTree.Strohrmann.ERP.Web/Models/Shared/SearchModel.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace GreenTree.Strohrmann.ERP.Web.Models.Shared
+{
+    public class SearchModel
+    {
+        /// <summary>
+        /// Path of the model property
+        /// </summary>
+        public string ModelPropertyPath { get; set; }
+
+        /// <summary>
+        /// Term to be searched for
+        /// </summary>
+        public string SearchTerm { get; set; }
+    }
+}

+ 1 - 3
GreenTree.Strohrmann.ERP.Web/Views/Craft/Create.cshtml

@@ -47,9 +47,7 @@
                 </div>
                 <div class="form-group">
                     <label asp-for="Customer" class="control-label"></label>
-                    @*@Html.DropDownListFor(e => e.Customer.Id, 
-                        (IEnumerable<SelectListItem>)ViewData["AvailableCustomers"], 
-                        new { @class = "form-control" })*@
+                    @await Html.PartialAsync("~/Views/Customer/_SearchCustomerPartial.cshtml")
                     <span asp-validation-for="Customer" class="text-danger"></span>
                 </div>
             </div>

+ 72 - 0
GreenTree.Strohrmann.ERP.Web/Views/Customer/_SearchCustomerPartial.cshtml

@@ -0,0 +1,72 @@
+@model GreenTree.Strohrmann.ERP.Web.Models.Shared.SearchModel
+
+@{ 
+	var partialId = Html.RandomId();
+}
+
+<script type="text/javascript">
+
+    @{ 
+        <text>
+        function search_@partialId () {
+            $.ajax({
+                method: "POST",
+                url: "@Url.Action("Search", "Customer")",
+                data: {
+                    SearchTerm: $("#text_@partialId").val()
+                },
+                success: function (data) {
+                    debugger;
+                    $("#searchCustomerModal_@partialId").modal("show");
+                    if (data == null) return;
+                    $(data).each(function (index, element) {
+                        $("#searchCustomerModal_@partialId").find(".list-group").append(
+                            '<button type="button" class="list-group-item list-group-item-action">' + element + '</button>');
+                    });
+                },
+                error: function (msg) {
+
+                }
+            });
+        }
+            
+        </text>
+    }
+
+</script>
+
+<div class="input-group mb-3">
+    <div class="input-group-prepend">
+        <button class="btn btn-outline-secondary fas fa-search" type="button" onclick="search_@partialId ()"></button>
+    </div>
+    <input id="text_@partialId" type="text" class="form-control" placeholder="Suchbegriff" aria-label="Suchbegriff" aria-describedby="basic-addon1">
+    @if (Model == null)
+	{ 
+        <input id="hiddenValue_@partialId" type="hidden" />
+	}
+	else
+	{
+        <input id="hiddenValue_@partialId" type="hidden" name="@Model.ModelPropertyPath" />
+	}
+</div>
+
+<div id="searchCustomerModal_@partialId" class="modal fade">
+    <div class="modal-dialog">
+        <div class="modal-content">
+            <div class="modal-header">
+                <h4 class="modal-title">Suchergebnisse</h4>
+            </div>
+            <div class="modal-body">
+                <div class="form-group">
+                    <label class="control-label"></label>
+                    <div class="list-group">
+                    </div>
+                </div>
+            </div>
+            <div class="modal-footer">
+                <div class="btn btn-secondary" data-dismiss="modal">Schließen</div>
+                <div class="btn btn-success">Auswählen</div>
+            </div>
+        </div>
+    </div>
+</div>