AutoCompleter

Framework Core
Tag Helper <autocompleter>

A flitered drop down list from a text box.

Base

This is an abstract class used by all implemented filter field types

Properties

AutoCompleteBase.ResultType type

The type of list this autocompleter returns. Posts ResultType. This is a defined enum created in the client application by overloading AutoCompleteBase.ResultType

string ajaxPage
Default: "/ajax/autocompleter"

The API endpoint that is called to retrieve all autocompleter operations.

ValidateValueHandler<Attachment> validateValueHandler
Default: null

Javascript function called to download a file.

int width
Default: -1

The width of the rendered control. If -1 then is left empty for CSS to control.

bool containerFullWidth
Default: false

Adds an inline style of "width: 100%" to the rendered control.

bool readOnly
Default: false

Make the control read only.

bool disabled
Default: false

*** Makes the control disabled.

bool focus
Default: false

Set the focus to this control.

List<AutoCompleterExtraParameter> extraParameters
Default: null

A list of extra Name Value string parameters to be posted along with the control. Allows extra IDs to be sent through.

bool autoValidate
Default: true

Causes the Javascript validate event to fire on the control after loading to validate any entry in the control.

bool postInvalidValues
Default: false

Allow the control to post back an entry even if it didn't match a valid item in the list. Useful for allowing additions to the list.

bool unique
Default: false

*** Any value entered into this control cannot already exist in the list of values. Posts unique=1 & valueid=value

bool showEdit
Default: false

Show an edit icon next to the AutoCompleter to navigate to another page to edit the value and other details.

string editAction - Deprecated
Default: null

If showEdit is true then the action to navigate to for the editing.

string editSavedUrl
Default: null

If showEdit is true then the action to navigate to for the editing.

string EditSaveButtonName
Default: Save

This simulates the Save button being pressed to allow backend attributes to find the correct method to call.

bool showList
Default: true

Puts an icon next to the AutoCompleter to show a drop down list of all possible entries.

bool showAdd
Default: false

Show an add icon next to the AutoCompleter. This button needs javascript binding to do anything.

bool wide
Default: false

Adds a "Wide" class to the AutoCompleter textbox for use in certain situations.

string editButtonText
Default: "Edit"

*** The text to display on any edit button.

List<AutoCompleterFilter> filters
Default: null

Posts through the value of a linked field for filtering the list displayed in this control.

List<AutoCompleterJoin> joins
Default: null

With joined controls, both have to be valid for both to display as valid. Passes a Join=1 & Field=value

bool textOnly
Default: false

The control will not save values but instead always mirror the text. This allow a look up list but still act as a wildcard text field.

bool filter-active
Default: null

Adds "Active" to the Extra Parameters. When validating an initial value that has been loaded, this will get overridden to False to show any old matches that are no longer available to select. This prevents data deletion of historical records. If you want to enforce a match then use the hide-inactive-value-text attribute.

bool hide-inactive-value-text
Default: false

If the pre-existing value is inactive then do not show the related text regardless of any filter-active value. Used to override a filter-active to hide deleted entries and so force a correction on saving.

AutoCompleterExtraParameter
string Key

The posted parameter name to use.

string Value

The posted value.

AutoCompleterFilter

This takes the value of ControlId and posts it as an Extra Paramater with a key of ParameterName along with its value.

string ParameterName

The posted parameter name to use.

string ControlId

The id of the control to use to filter against.

bool isAutoCompleter

Is the control being used to filter also an AutoCompleter control.

This control posts with every keypress to return a list of possible options.

Post Properties

AutoCompleteBase.ResultType type

A string representation of the enum for the type of list returned. This is a defined enum created in the client application by overloading AutoCompleteBase.ResultType

string Search

The search text to find.

string ExtraParameters

Any extra parameters passed through to the backend API.

Guid User

Guid of user making the call.

Post Returned Properties

AutoCompleteResults data

The returned data set

AutoCompleteResults
List<Result> Results

The list of matching results

bool IncludeInfo

Does the returned results include the Info field. This causes the list to display the extra Info line under each result.

Result
string Text

The text value of the result.

string Value

The string value of the result.

string Info

The string value for the info display line.

Javascript Events

Valid - Textbox

Fires when a valid entry is found in the autocompleter field. Returns the valid value.
$('#MyFieldId').on('Valid', function (e, value) { } );

Valid - Valuebox

Fires when a valid entry is found in the autocompleter field. Returns the valid value and the previous value.
$('#MyFieldId').on('Valid', function (e, value, oldValue) { } );

Invalid - Valuebox

Fires when a valid entry is not found in the autocompleter field. Returns the invalid value.
$('#MyFieldId').on('Invalid', function (e, value) { } );

Javascript Methods

ShareItLibrary.AutoCompleter.Clear = function(id)

Clears the AutoCompleter field and prevents the result list from displaying. This can be used when another field is not valid and you wish to prevent entries into this field.
ShareItLibrary.AutoCompleter.Clear('MyFieldId');

ShareItLibrary.AutoCompleter.InitSingle = function(id)

Initialises the Autocompleter field. Used for dynamic loading to bind events after page has loaded.
ShareItLibrary.AutoCompleter.InitSingle('MyFieldId');

ShareItLibrary.AutoCompleter.AutoCompleteValueText = function(id)

After setting a value via javascript into an autocompleter field value, refresh the text to show the result.
ShareItLibrary.AutoCompleter.AutoCompleteValueText('MyFieldId');

$.fn.setvalue(value, text)

Sets the value on the field $('#MyAutoCompleter').setvalue('123', 'Hello');

$.fn.SiAcClear()

Clears value and text on the field $('#MyAutoCompleter').SiAcClear();

AutoCompleterHelper

Helper functions.

Method

public static IActionResult EditAction(Func<IActionResult> defaultAction, HttpContext context, ITempDataDictionary tempData)

Used on the Save method of a controller if Edit button is used. If Save is ok then call return AutoCompleterHelper.EditAction(() => EditBack(), HttpContext, TempData);

AutoComplete.cs

AutoComplete.cs
public class AutoComplete : AutoCompleteBase
{
public new enum ResultType { MyType }

public override AutoCompleteResults FindResults(AutoCompleterQuery acq)
{
return (ParseResultType()) switch
{
ResultType.MyType => MyType(acq),
_ => new AutoCompleteResults(new List())
};
}


public override AutoCompleteBase.ValidateValueResult ValidateValue(AutoCompleterValidate acv)
{
return (ParseResultType()) switch
{
ResultType.MyType => ValidateMyType(acv),
_ => new ValidateValueResult()
};
}


private AutoCompleteResults MyType(AutoCompleterQuery acq)
{
List items = new();
var qs = (IQueryable)db.MyModel;

if (!acq.FullList)
qs = qs.Where(e => e.MyType.Contains(Search));

if (Active != null)
qs = qs.Where(e => e.Active == Active);

var customId = Field("CustomId"); // Only add this line if needed.
if (customId != null) // Only add this line if needed.
qs = qs.Where(e => e.CustomId == customId); // Only add this line if needed.

qs = qs.Include(e => e.IncludedEntity).OrderBy(e => e.MyType).Take(acq.FullList ? fullListCount : listCount);

foreach (var q in qs);
{
Result r = new();
{
Text = myType.Text,
Value = myType.ValueId.ToString(),
Info = myType.Description // Only add this line if needed.
};
items.Add(r);
}

return new AutoCompleteResults(items, true);
}


private ValidateValueResult ValidateAccelerator(AutoCompleterValidate acv)
{
ValidateValueResult vvr = new();
try
{
Guid id = new Guid(acv.Value);
var q = db.MyModel.Find(id);
if (q != null)
{
vvr.Text = myType.Text;
vvr.Value = value;
vvr.IsValid = true;
}
}
catch
{
}
return vvr;
}
}

Simple AutoComplete.

Razor
<autocompleter asp-for="MyFieldId" type="AutoComplete.ResultType.MyType" extra-parameters="@new EpsActive()"></autocompleter>

Add Extra and Filter Parameters.

Razor
<autocompleter asp-for="MyFieldId" type="AutoComplete.ResultType.MyType" class="form-control" extra-parameters="@new EpsActive()" filters="@(new List<AutoCompleterFilter> { new AutoCompleterFilter("CustomId", "MyControlId") })"></autocompleter>;