Hello,
I currently understand the width of the dropdown control is based on the parent container, so it is a fixed width. I wanted to ask if it may be possible to have the width of the dropdown control change dynamically based on the selected item from the dropdown instead. In the example below for instance, I'd like the width to contract to the point where the down arrow is closer to the selected item "Short option". So this:
Should look like this:
And if there is a longer option, I would like the dropdown control to expand to fit the item, instead of cutting it off:
So that it looks like this:
Thank you for any advice.
|
<SfDropDownList TItem="GameFields" TValue="string" PopupHeight="230px" Placeholder="Select a game" DataSource="@Games" Width="auto">
<DropDownListFieldSettings Text="Text" Value="ID" />
</SfDropDownList> |
Hi Ponmani,
It's possible I am not explaining correctly, apologies. I'm aware of the width property but it doesn't really fulfill my requirements. In the sample that you provide, when the "Golf" selection is provided, it currently looks like this:
Instead, would it be possible to have the width be dynamically adjusted to look like this when it is selected?
Similarly for a longer item like the one below, The sample currently looks like this:
Would it be possible to have the width dynamically adjust on selection to look like this?
Thank you,
John
|
window.OnOpen = (id, Text) => {
var multiObject = document.getElementById(id).parentElement;
var width = getTextWidth(Text, "bold 12pt arial");
// 20px added for dropdownIcon width
multiObject.style.width = (width + 20) + "px";
}
function getTextWidth(text, font) {
// re-use canvas object for better performance
var canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
var context = canvas.getContext("2d");
context.font = font;
var metrics = context.measureText(text);
return metrics.width;
} |
|
public void Select(Syncfusion.Blazor.DropDowns.SelectEventArgs<GameFields> args)
{
JsRuntime.InvokeVoidAsync("OnOpen", "ddl", args.ItemData.Text);
} |
Hi Ponmani,
Thank you for your reply. The solution you provided is close, but one caveat we have is that the dropdown list menu is sometimes updated dynamically with different values, so the option is reset dynamically as well. I mocked a razor file to recreate something similar to what is happening on our end attached and am using the javascript file that you provided above.
To reproduce the issue that I am having some trouble solving after running the app:
- Click the dropdown menu to display the list of options.
- Click the first item, "American football etc....". The dropdown width adjusts accordingly to the width of the selected item, as I would like it to.
- Click the "Change dropdown values" button
- The dropdown width doesn't dynamically adjust to the width that I would expect it to. It should be smaller in width, with less spacing
Similarly, from here, you can select an item, say "Golf". Then when you click "Change Dropdown values", The width doesn't adjust to accommodate the entirety of the placeholder "Select a game", where I would like it to.
Please let me know if this is still possible?
Thank you,
John
Attachment: Index_253aa684.zip
Thank you Ponmani, just wanted to follow up on this item.
|
public void ChangeValue()
{
switchGames = !switchGames;
StateHasChanged();
JsRuntime.InvokeVoidAsync("OnOpen", "ddl");
} |