Change default properties or default behaviour od DropDownList

Hi there.

I have a lot of DropDownList s in my app like this example:

List<object> listaHorarios = db.Horarios.OrderBy(l => l.Codigo)
.Select(e => (object)new { value = e.Codigo, text = e.Descricao }).ToList();


Html.EJS().DropDownListFor(model => model.CodigoHorarioSegunda) .DataSource(listaHorarios).Fields(new DropDownListFieldSettings { Text = "text", Value = "value" })
.ShowClearButton().PopupWidth("300")
.Value(Model.CodigoHorarioSegunda).Render()


But i have to set the DropDownListFieldSettings in all of the DropDownList. Can't i change the default behaviour?


In one app i have, in EJ1 version, i had this code to change the default behaviour of the DropDownList:

ej.DropDownList.prototype.defaults.fields.value = "value";
ej.DropDownList.prototype.defaults.fields.text = "text";

But in the EJ2 i can't reach this properties. How can i change this?

And i would like to change the default behaviour of the PopupWidth to 'auto' too...


And i would like to avoid to put the "Created" event in all the elements too. I prefer the way i do in the EJ1 version.

Thanks


1 Reply

BC Berly Christopher Syncfusion Team November 12, 2021 04:26 PM UTC

Hi Emiliano, 
  
Greetings from Syncfusion support. 
  
We would like to inform you that we could not access the properties and change the default value since all the properties will be executed based on the component instance. So, we need to define the properties and event for the all component with help of common variable and function as mentioned below. 
  
@using Syncfusion.EJ2.DropDowns 
@{  
    var width = "350px"; 
    var field = new DropDownListFieldSettings { Text = "text", Value = "value" }; 
} 
@Html.EJS().DropDownList("default").DataSource((IEnumerable<object>)ViewBag.data).Created("OnCreate").Fields(field).ShowClearButton().PopupWidth(width).Render() 
@Html.EJS().DropDownList("default1").DataSource((IEnumerable<object>)ViewBag.data).Created("OnCreate").Fields(field).ShowClearButton().PopupWidth(width).Render() 
 
<script> 
    function OnCreate() { 
        console.log("created event triggered"); 
    } 
</script> 
 
  
Regards, 
Berly B.C 


Loader.
Up arrow icon