Dynamically change css based on if a values are selected

Hello,


I have a multiselect and want to control the css based on if there are values selected.

If the user selects a value then display VALUE PRESENT CSS CLASS.

If multiselect values are set on initial load then display VALUE PRESENT CSS CLASS.

if user unselects a value and there are still other values selected then display VALUE PRESENT CSS CLASS

If there are no values selected either on initial load or after clear then display DEFAULT CSS CLASS.

If the last value is unselected then display DEFAULT CSS CLASS.

Basically the logic is to show default css class when there are no values in the multiselect and show the value present css class when there are values selected.


I am getting inconsistent results, multiselect css goes back to syncfusion default in between clicks. I have attached a sample. If there's a way to achieve the same result without having to check all the multiselect events that would be great. If not i would like to know how to fix the provided sample code

Thank you


Attachment: multiselectBackgroundColor_d995811b.zip

12 Replies 1 reply marked as answer

BS Buvana Sathasivam Syncfusion Team September 20, 2022 03:33 AM UTC

Hi Pavel,


Greetings from Syncfusion support.


We have checked your shared sample. You can dynamically update the CSS class based on bindable values, like the below code, without using any events. Please find the following code and sample for your reference.

<SfMultiSelect TValue="List<string>" TItem="Country" CssClass="@((MultiVal != null && MultiVal.Count > 0) ? VALUE_IS_PRESENT_CSS_CLASS : FILTER_DEFAULT_CSS_CLASS)" @bind-Value="@MultiVal" DataSource="@Countries">

…..

</SfMultiSelect>

 

@code {

    private const string FILTER_DEFAULT_CSS_CLASS = "circuit-notes";

    private const string VALUE_IS_PRESENT_CSS_CLASS = "circuit-notes-selection";

    public List<string> MultiVal { get; set; }

 

    protected override void OnInitialized()

    {

        MultiVal = new List<string>(){"CA", "DK"};

    }

}


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SyncfusionBlazorApp1-1335173209


Please check the above sample and let us know if it meets your requirement. 


Regards,

Buvana S



PA Pavel September 20, 2022 10:21 PM UTC

The solution does not meet the requirement because there are times when the css is neither  VALUE_IS_PRESENT_CSS_CLASS or  FILTER_DEFAULT_CSS_CLASS. Instead the css goes back to syncfusion default. I have attached a video that shows the issue.


Thank you


Attachment: Screen_Recording_20220920_at_8_26_09_AM_AdobeExpress.mp4_60509edb.zip


SP Sureshkumar P Syncfusion Team September 21, 2022 10:04 AM UTC

Hi Pavel,

We have modified the code example to resolve your faced issue when using the dynamic CSSClass property update value. We suggest you use the single class name instead of the class name separate with a hyphen.

Find the code example here:

<SfMultiSelect TValue="List<string>" TItem="Country" CssClass="@((MultiVal != null && MultiVal.Count > 0) ? VALUE_IS_PRESENT_CSS_CLASS : FILTER_DEFAULT_CSS_CLASS)" Placeholder="Countries" EnableChangeOnBlur="false" @bind-Value="@MultiVal" DataSource="@Countries" Mode="@VisualMode.CheckBox" ShowClearButton="true" AllowFiltering="false" PopupHeight="700px" PopupWidth="400px" Width="150px">

    <MultiSelectFieldSettings Text="Name" Value="Code"></MultiSelectFieldSettings>

    <MultiSelectEvents TValue="List<string>" TItem="Country" ValueChange="@ValueChanged" Created="@OnCreated"></MultiSelectEvents>

</SfMultiSelect>

 

 

@code {

    private const string FILTER_DEFAULT_CSS_CLASS = "defaultClass";

    private const string VALUE_IS_PRESENT_CSS_CLASS = "selectedClass";

    public List<string> MultiVal { get; set; } = new List<string>() { };

    public string CountriesCss { get; set; } = "";

 

    protected override void OnInitialized()

    {

        MultiVal = new List<string>() { "AU", "CA" };

    }

 

    public class Country

    {

        public string Name { get; set; }

 

        public string Code { get; set; }

    }

 

    List<Country> Countries = new List<Country>

    {

        new Country() { Name = "Australia", Code = "AU" },

        new Country() { Name = "Bermuda", Code = "BM" },

        new Country() { Name = "Canada", Code = "CA" },

        new Country() { Name = "Cameroon", Code = "CM" },

        new Country() { Name = "Denmark", Code = "DK" },

        new Country() { Name = "France", Code = "FR" },

        new Country() { Name = "Finland", Code = "FI" }

    };

    private void ValueChanged(MultiSelectChangeEventArgs<List<string>> arg)

    {

        if (MultiVal != null && MultiVal.Count > 0)

        {

            CountriesCss = VALUE_IS_PRESENT_CSS_CLASS;

        }

        else

        {

            CountriesCss = FILTER_DEFAULT_CSS_CLASS;

        }

 

    }

 

    private async Task OnCreated()

    {

        if (MultiVal != null && MultiVal.Count > 0)

        {

            CountriesCss = VALUE_IS_PRESENT_CSS_CLASS;

            await InvokeAsync(StateHasChanged);

        }

        else

        {

            CountriesCss = FILTER_DEFAULT_CSS_CLASS;

            await InvokeAsync(StateHasChanged);

        }

    }

 

}


Find the modified sample in the attachment:

Note: If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.

Regards,

Sureshkumar P


Attachment: SyncfusionBlazorApp1_15bb9ba9.zip


PA Pavel September 22, 2022 03:51 AM UTC

Great removing the - from the css worked!! Thank you

I have one more question.

I would like to add hover to FILTER_DEFAULT_CSS_CLASS. so when you hover over the multiselect with no values background color changes to a darker grey. How do I access the hover of the multiselect?

thank you



SP Sureshkumar P Syncfusion Team September 22, 2022 06:14 AM UTC

Hi Pavel,

You can achieve your requirement by using the native onmouseover and onmouseout event.

Find the code example:

<SfMultiSelect TValue="List<string>" TItem="Country" @onmouseover="@ComponentHover" @onmouseout="@ComponentHouerOut" CssClass="@((MultiVal != null && MultiVal.Count > 0) ? VALUE_IS_PRESENT_CSS_CLASS : FILTER_DEFAULT_CSS_CLASS)" Placeholder="Countries" EnableChangeOnBlur="false" @bind-Value="@MultiVal" DataSource="@Countries" Mode="@VisualMode.CheckBox" ShowClearButton="true" AllowFiltering="false" PopupHeight="700px" PopupWidth="400px" Width="150px">

    <MultiSelectFieldSettings Text="Name" Value="Code"></MultiSelectFieldSettings>

    <MultiSelectEvents TValue="List<string>" TItem="Country" ValueChange="@ValueChanged" Created="@OnCreated"></MultiSelectEvents>

</SfMultiSelect>

@code {

// mousehover native event handler

    void ComponentHover(){

    }

    // mouseleave native event handler

    void ComponentHouerOut(){

    }

}


Regards,

Sureshkumar P



PA Pavel September 22, 2022 03:25 PM UTC

can you please provide an example of how to set the background color css inside the methods?

what selector do i use and how do i set it programmatically in side method

// mousehover native event handler

    void ComponentHover(){

how to set hover css?

    }

    // mouseleave native event handler

how to set hover css?

    }



SP Sureshkumar P Syncfusion Team September 23, 2022 08:15 AM UTC

Hi Pavel,

Query: I would like to add hover to FILTER_DEFAULT_CSS_CLASS. so, when you hover over the multiselect with no values background color changes to a darker grey. How do I access the hover of the multiselect?

We suggest you use the CSS selector to update the required styles.

Find the code example here:

.e-multiselect.e-control-wrapper.e-input-group.defaultClass:hover {

        background-color: darkgray;

    }

Regards,

Sureshkumar P



PA Pavel September 26, 2022 04:02 PM UTC

this actually did not work, When i hover nothing happens

i provided a sample with added selector 

.e-multiselect.e-control-wrapper.e-input-group.defaultClass:hover {

        background-color: red;

    }


Thank you



Attachment: SyncfusionBlazorApp1_4_2548213a.zip



SP Sureshkumar P Syncfusion Team September 27, 2022 08:57 AM UTC

Hi Pavel,

We have validated the shared information with the sample, and you have customized the component style in the application with important styles. Which is the reason the updated hover style is not applied properly in the sample. We suggest you remove the below styles from your application to resolve the issue.

Find the code example here:

/*popout background color*/

   /* .defaultClass.e-multiselect.e-input-group .e-multi-select-wrapper {

        background-color: #f0f0f0 !important;

    }*/

Find the modified sample in the attachment:

Regards,

Sureshkumar P


Attachment: SyncfusionBlazorApp1_4_bf5335b2.zip


PA Pavel September 28, 2022 12:34 AM UTC

That fixed the hovering color but how do I get the #f0f0f0 background color back when not hovered?
When we comment out

/* .defaultClass.e-multiselect.e-input-group .e-multi-select-wrapper {

background-color: #f0f0f0 !important;

}*/

the background color is white before and after hover.

is there a way to have the background color #f0f0f0 .before and after hover instead of the default white color?




SP Sureshkumar P Syncfusion Team September 28, 2022 07:43 AM UTC

Hi Pavel,

We suggest you apply the CSS style for defaultclass without important to achieve your requirement.

Find the code example here:

/*Display this background when there is a value selected*/

    .e-multiselect.e-control-wrapper.e-input-group.selectedClass {

        background-color: #ff2950;

    }

    /* default style with hovering */

    .e-multiselect.e-control-wrapper.e-input-group.defaultClass:hover {

        background-color: gray;

    }

    /* default style without hovering */

    .e-multiselect.e-control-wrapper.e-input-group.defaultClass {

        background-color: #f0f0f0;

    }

Find the modified sample in the attachment:

Regards,

Sureshkumar P


Attachment: SyncfusionBlazorApp1_4_99280fc3.zip

Marked as answer

PA Pavel October 4, 2022 03:31 PM UTC

thank you!


Loader.
Up arrow icon