RadzenAutoComplete issues with returned data

Hello,

after a change in our backend the strings list in the AutoComplete doesn't come from a single variable, but 2 class variables combined, both strings. After looking at the provided examples, I managed to display the list as wanted. But once I select one result, I get only ONE of the 2 strings returned..

<RadzenAutoComplete Data=@listElements TextProperty="datasign" FilterOperator="StringFilterOperator.StartsWith" LoadData=@OnLoadData Change=@(args => OnChange(args, "AutoComplete")) Style="width: 100%" >
    <Template Context="element">@element.datasign@element.datanumber</Template>
</RadzenAutoComplete>

my code looks like the above. I believe the problem is the TextProperty value, which is one of the 2 string-variables contained in each element. What do I have to change to get the full text of the selected entry?

1 Like

Solved - :grin:
I set the TextProperty to "element" - which nicely returns the whole object used for the selected entry.
So I have access to datasign and datanumber .. and further member variables

Hello eagle275,

can you share the code for the RadzenAutoComplete?

you wrote that it returns the whole object - in the change event?

I also need the id of the selected entry

robert

Sure .. hope it helps a bit ...

   <RadzenAutoComplete Data=@datalist TextProperty="element" FilterOperator="StringFilterOperator.StartsWith" LoadData=@OnLoadData Change=@(args => OnChange(args, "AutoCompleteLetters")) Style="width: 100%; font-size: 24px;" onkeypress="return (event.charCode!=8 && event.charCode ==0 || (event.charCode>=65 && event.charCode<=90) || (event.charCode>=97 && event.charCode<=122))" @oninput="@(ui => { lettercode = (string) ui.Value;})" >
      <Template Context="element">@element.letters @element.numbercode</Template>
   </RadzenAutoComplete>
...

 async Task OnLoadData(LoadDataArgs args)
    {
        await Task.Yield();
        datalist = localDataService.ReadUnitSearchList();
        if (args.Filter != null && args.Filter != "") {
            int number;

            if (int.TryParse(args.Filter, out number)) {
                datalist = datalist.Where(c => c.letters.Equals(letters) && c.numbercode.ToLower().Contains(args.Filter.ToLower())).ToList();
            }
            else
            {
                datalist = datalist.Where(c => (c.letters + c.numbercode).ToLower().Contains(args.Filter.ToLower())).ToList();
            }
        }
        await InvokeAsync(StateHasChanged);
    }

    private async Task OnChange(object value, string name)
    {
        switch (name)
        {
            case "AutoCompleteLetters":
                if (first)
                {
                    first = false;
                }
                else
                {
                    if (value is dUnitSearchList) // thats the class of the "element"
                    {
                        dUnitSearchList val = (dUnitSearchList)value;
                        localid = val.localID; // ID of selected entry after clicking on a choice

                        ...  // process the data 
                        data = localDataService.ReadData(localid);
                        await OnInitializedAsync(); // refresh dialog 
                        first = true; // reset the internal step 
                    }
                    else
                    {
                        first = true;
                    }
                }
                break;
...
}

incase you wonder about the 2 step handling with first = true -> first = false , then again first = true :
I found while twiddling and testing it that during the first phase the is not much in the data delivered - but on the second phase the object value contains the element that is clicked on to select one entry. hence I skip the first phase and act on the second phase - then reset for the next first phase again ...

localDataService is kinda "middleware" service which translates the client-objects to server objects

1 Like

I followed the logic for your solution and it works great for getting the underlying element that the user selected via the OnChange event. However since the TextProperty is using the 'element' instead of the display property of the class then it renders the object namespace instead of a value on screen.

How do you get it to display the value instead of the namespace?

<RadzenStack Orientation="Orientation.Horizontal">
	<RadzenAutoComplete style="width: 100%" LoadData="@OnAutoCompleteLoadData" Data="@classCodes" TextProperty="context" Change=@(args => OnClassCodeChanged(args))>
		<Template Context="context">
			<RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.SpaceBetween">
				<RadzenStack>
					@context.Description
					<br />
					@context.ClassCodeNumber
				</RadzenStack>
				<RadzenStack>				
					<RadzenButton Variant="Variant.Text" Click=@(args => OpenClassCodeDialog(@context.ClassCodeNumber)) Style="border: none; color: black;" Text="Details..." />
				</RadzenStack>
			</RadzenStack>
		</Template>
	</RadzenAutoComplete>
	<RadzenButton Click="ClassCodeDeleted" Visible=@ShowDeleteButton ButtonStyle="ButtonStyle.Danger" Icon="delete"></RadzenButton>
</RadzenStack>

@code {
	public string? classCode;

	[Parameter]
	public IEnumerable<ClassCodeModel>? classCodes { get; set; }
	[Parameter]
	public EventCallback<string> OnDelete { get; set; }
	[Parameter]
	public EventCallback<ClassCodeModel> OnClassCodeChange { get; set; }
	[Parameter]
	public string? ControlId { get; set; }
	[Parameter]
	public bool ShowDeleteButton { get; set; }


	private async Task ClassCodeDeleted()
	{
		await OnDelete.InvokeAsync(ControlId);
	}

	private async Task OnClassCodeChanged(dynamic args)
	{
		if (args.GetType().Name.Contains("ClassCodeModel"))
			await OnClassCodeChange.InvokeAsync(args);
	}

	...
}

I want it to display this:

Instead it currently displays like this:

Hello @Jayman ,

you can change the way the job description is displayed by using <Template - but I assume the <RadzenStack-element messes with the processing

<RadzenAutoComplete Data=@wordliste TextProperty="element" FilterOperator="StringFilterOperator.StartsWith" LoadData=@OnLoadData Change=@(args => OnChange(args, "AutoCompleteZei")) Style="width: 100%; font-size: 24px;" onkeypress="return (event.charCode!=8 && event.charCode ==0 || (event.charCode>=65 && event.charCode<=90) || (event.charCode>=97 && event.charCode<=122))" @oninput="@(ui => { word1 = (string) ui.Value;})" >
   <Template Context="element">@element.word @element.number</Template>
</RadzenAutoComplete>

I use this for AutoComplete- and the displayed "element" consists of a letter code coming from the word-field and a number code from the number-field

The template is working fine as far as displaying the results correctly as the user is typing. The issue is when the user makes a selection from the list of options that shows up while they are typing. Once they make a selection then the control renders the namespace instead of the display value inside the input field. Refer to the screenshots in my previous post showing what I need vs what is currently displaying. Thanks for your time.

NVM. I solved the issue by databinding to a variable and then setting the value property on the autocomplete control.

Value=@ClassCodeDescription

1 Like

Thx for clarifying you question .. That would have been my next suggestion - you need a way to accept the coice and set it as value ^^

1 Like

Thanks for sharing this solution. brilliant!