Awesome ASP.net Web-Forms Controls
Autocomplete
Meal:
Autocomplete needs an url, controller or data function (js func) to get data from, by default convention it
will look for a controller with the same name as it + "Autocomplete"
action GetItems
- gets the items for the autocomplete, it
will receive a v
parameter which represents the value of the textbox
AutocompleteDemo.aspx
Meal: <% Meal.Autocomplete() .Url(Page.Url().Action("GetItems","MealAutocomplete")) .Placeholder("try o"); %> <awe:Ocon runat="server" ID="Meal" />
Awesome/Autocomplete/MealAutocompleteController.cs
public class MealAutocompleteController : Controller { public ActionResult GetItems(string v)// v is the entered text { v = (v ?? "").ToLower().Trim(); var items = Db.Meals.Where(o => o.Name.ToLower().Contains(v)); return Json(items.Take(10).Select(o => new KeyContent(o.Id, o.Name))); }
Custom Item rendering
Meal:
Achieving custom item rendering by setting ItemFunc
,
we're reusing the awem.imgItem used by the imgDropdown but any js function that receives an object (KeyContent inheritant MealDisplay) and returns a string will do.
AutocompleteDemo.aspx
Meal: <% MealImgIt.Autocomplete() .ItemFunc("awem.imgItem") .Url(Page.Url().Action("GetMealsImgAutoc", "Data")) .Placeholder("try o"); %> <awe:Ocon runat="server" ID="MealImgIt" />
Awesome/DataController.cs
public ActionResult GetMealsImgAutoc(string v) { v = (v ?? string.Empty).ToLower(); var url = Url.Content(DemoUtils.MealsUrl); var items = Db.Meals .Where(o => o.Name.ToLower().Contains(v)) .Select(o => new MealDisplay(o.Id, o.Name, url + o.Name + ".jpg")); return Json(items); }
Storing selected value key in a separate field using .PropId
Key:
you can (should) use hidden input instead
when the user selects a value from the autocomplete lists the PropId will be assigned the key of the selected value, but if the user will type something into the autocomlete after that the propId value will clear
AutocompleteDemo.aspx
<div class="efield"> <div class="elabel"> Meal: </div> <% Meal3.Autocomplete().PropId(MealKey.ClientID).Controller<MealAutocompleteController>(); %> <awe:Ocon runat="server" ID="Meal3" /> </div> <div class="efield"> <div class="elabel">Key:</div> <% MealKey.TextBox(); %> <awe:Ocon runat="server" ID="MealKey" /> <div class="awe-il">you can (should) use hidden input instead</div> </div>
Using DataFunc and ItemFunc
git repo:
using DataFunc
to get data from github api, ItemFunc
is used to set a custom js function that will render the item
AutocompleteDemo.aspx
git repo: <% Repo.Autocomplete() .DataFunc("getGit") .ItemFunc("site.gitItem"); %> <awe:Ocon runat="server" ID="Repo" /> <script> function getGit(v) { return $.get('https://api.github.com/search/repositories', { q: v }) .then(function (data) { return $.map(data.items, function (item) { return { k: item.id, c: item.full_name, avatar: item.owner.avatar_url, desc: item.description }; }); }); } </script>
Bound to parents
autcomplete list limited to categories selected in the parent
Using predefined parameters
Comments