How to set data for dropdownedit column?

Hi

How to set data manager, value and text fields, for dropdownedit column? 
There is nothing in documentation, and examples from MVC or others doesn't work.

16 Replies

PP Pooja Priya Krishna Moorthy Syncfusion Team May 8, 2020 11:40 AM UTC

Hi Tomasz, 
We have prepared a sample using remote data source. We can assign data source to the dropdown column by using drop-down-data property. Please find the below code example. 
[cs] 
public object Get() 
{ 
    var data = GetUrlDataSource(); 
    return Json(new { result = data, count = data.Count }); 
} 
[cshtml] 
@{ 
  List<Resource> ResourceCollection = new List<Resource>(){ 
   new Resource() { id= 1, text= "Project Manager", value= "Project Manager" }, 
     //... 
   }; 
} 
<ej-tree-grid id="TreeGridContainer" 
//...> 
   <e-datamanager id="myData" url="/api/TreeGrid" adaptor="WebApiAdaptor"></e-datamanager> 
     <e-tree-grid-columns> 
        //... 
            <e-tree-grid-column field="resources" header-text="Resources" edit-type="Dropdown" drop-down-data="ResourceCollection"> 
                <ej-drop-down-list> 
                    <e-drop-down-list-fields text="text" value="value" /> 
                </ej-drop-down-list> 
            </e-tree-grid-column> 
        </e-tree-grid-columns> 
</ej-tree-grid> 


Please refer the below sample and documentation link. 

Regards, 
Pooja K. 



PS Phil Seeman May 13, 2020 06:17 PM UTC

Hi,

The above info is for EJ1. Can you provide an equivalent answer for EJ2? Thanks.


PK Padmavathy Kamalanathan Syncfusion Team May 14, 2020 10:28 AM UTC

Hi Tomasz, 
  
Thanks for your update. 
  
QUERY: How to set data manager, value and text fields for dropdownedit column in Tree Grid 
  
We have achieved your requirement by using the edit params property of Tree Grid column.  You can customize model of the editType component(i.e Dropdown) through the params in edit property of e-treegrid-column tag helper. In the edit params, we can assign the data source, field (with text and value) for the drop down component. 
  
Please check the below code snippet, 
  
            //edit params 
@{ 
    var DropDownList = new Syncfusion.EJ2.DropDowns.DropDownList() {  
                         DataSource = new Syncfusion.EJ2.DataManager() {  
                             Url = "/api/Employee", Adaptor = "WebApiAdaptor" }, 
                             Query = "new ej.data.Query()" 
                             Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings() { 
                                Value = "Priority", Text = "Priority" } }; 
} 
  
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" idMapping="TaskId" 
  parentIdMapping='ParentId' height="270" treeColumnIndex="1" > 
  
    <e-treegrid-editSettings allowAdding="true" allowDeleting="true"  
        allowEditing="true"  mode="Row"></e-treegrid-editSettings> 
    <e-treegrid-columns> 
        <e-treegrid-column field="TaskId" headerText="Task ID" isPrimaryKey="true" ></e-treegrid-column> 
         <e-treegrid-column field="Priority" headerText="Priority" width="120" 
           editType="dropdownedit" edit="new {@params = DropDownList }"></e-treegrid-column> 
    </e-treegrid-columns> 
</ejs-treegrid> 
  
  
     public object Get() 
        { 
            var data2 = new PList().PListList(); //dropdown's data 
  
            return  new { Items = data2, Count = data2.Count() }; 
            
        } 
        public static List<PList> list = new List<PList>(); 
        public class PList 
        { 
            public string Priority { get; set; } 
            public List<PList> PListList() 
            { 
  
                list.Add(new PList { Priority = "UltraCritical" }); 
                list.Add(new PList { Priority = "Critiical" }); 
                list.Add(new PList { Priority = "Normal" }); 
                list.Add(new PList { Priority = "High" }); 
                list.Add(new PList { Priority = "Low" }); 
                return list; 
            } 
        } 
  
We have prepared sample for your reference. You can download the sample from the below location, 
  
Please check the below help documentation, 
  
Kindly get back to us for further assistance. 
  
Regards, 
Padmavathy Kamalanathan 




TO Tomasz May 14, 2020 10:32 AM UTC

Can I add parameter to query in DropDownList Datasource? In my scenario, I have a different list for each row, based on row ID.


PS Phil Seeman May 15, 2020 07:16 AM UTC

Thank you very much for the EJ2 sample code.

Another question: could the multi-select dropdown control be used as a treegrid column in a similar fashion?


PK Padmavathy Kamalanathan Syncfusion Team May 15, 2020 01:29 PM UTC

Hi Tomasz,
 
  
Thanks for your update. 
  
QUERY: Add parameter to query dropdow list's data based on row value 
  
We have achieved your query by adding the additional parameter to the dropdown in Tree Grid's "actionBegin" event with request type "beginEdit" . 
  
Please check the below code snippet, 
  
  
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" 
     idMapping="TaskId" parentIdMapping='ParentId'   
     actionBegin="begin"  actionComplete="complete"> 
    <e-treegrid-columns> 
        <e-treegrid-column field="TaskId" headerText="Task ID" isPrimaryKey="true" textAlign="Right" width="90"></e-treegrid-column> 
      
        <e-treegrid-column field="Priority" headerText="Priority" width="90" editType="dropdownedit"  
  edit="new {@params = DropDownList }"></e-treegrid-column></ejs-treegrid> 
  
<script> 
    var flag = true; 
    function begin(args) { 
        if (args.requestType == "beginEdit") { 
            var params = this.grid.columns[2].edit.params.query.params; 
           // checking whether id is already present or not then adding it as params 
  
            if (params.length == 0) { 
                     this.columns[2].edit.params.query.addParams("id", args.rowData.TaskId); 
                    // adding row's primary key value as params to dropdown 
            } 
            else if (params.length) { 
                for (var i = 0; i < params.length; i++) { 
                   if (params[i]["key"] == "id") { 
                        flag = false; 
                    } 
                } 
                if (flag) { 
                    this.columns[2].edit.params.query.addParams("id", args.rowData.TaskId); 
                    // adding row's primary key value as params to dropdown 
                } 
            } 
            } 
            
        } 
    
    function complete(args) { 
        if (args.requestType == "save" || args.requestType == "cancel") { 
             var params = this.columns[2].edit.params.query.params; 
            flag = true; 
               for (var i = 0; i < params.length; i++) { 
                    if (params[i]["key"] == "id") { 
                       params.splice(i,1); 
                    } 
                } 
        } 
    } 
</script> 
  
   public object Get() 
        { 
            var queryString = Request.Query; 
  
            // here we can get the parameter 
            int num = Convert.ToInt32(queryString["id"]); 
               // based on id value you can return data  
              ---------  
            return new { Items = data2, Count = data2.Count() }; 
  
        } 

In actionComplete event with "requestType" "save" and "cancel" we need to remove the added params in order to prevent the conflict while adding the parameter again. 
Please check the below screenshot, 
 
  
Using this primary key in server end, you can return data as per your requirement. 
  
Please check the below help documentation, 
  
Kindly get back to us for further assistance. 
  
Regards, 
Padmavathy Kamalanathan 

  



PS Phil Seeman replied to Phil Seeman May 15, 2020 01:43 PM UTC

Thank you very much for the EJ2 sample code.

Another question: could the multi-select dropdown control be used as a treegrid column in a similar fashion?


Never mind this question - it seems I've found an answer:




PS Phil Seeman replied to Phil Seeman May 15, 2020 03:20 PM UTC

Thank you very much for the EJ2 sample code.

Another question: could the multi-select dropdown control be used as a treegrid column in a similar fashion?


Never mind this question - it seems I've found an answer:



My apologies - I see now the sample I referenced is for native JS - would it be possible to see how to incorporate a multiselect column in a tree grid for ASP.NET Core?


PK Padmavathy Kamalanathan Syncfusion Team May 18, 2020 11:25 AM UTC

Hi Phil, 
  
Thanks for contacting Syncfusion Forums. 
  
QUERY: would it be possible to see how to incorporate a multi select column in a Tree Grid for ASP.NET Core 
  
As mentioned in the forum 146207, we can render the multi select column in Tree Grid using edit Template. Please check the below code snippet for using the Multi Select Drop Down column in Tree Grid for ASP.NET Core 

 
  
    <ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data"  
            idMapping="TaskId" parentIdMapping='ParentId'  treeColumnIndex="1"  > 
           ---------- 
    <e-treegrid-columns> 
        <e-treegrid-column field="TaskId" headerText="Task ID" isPrimaryKey="true"  
           textAlign="Right" width="90"></e-treegrid-column> 
        <e-treegrid-column field="Priority" headerText="Task Name" width="60"  
       edit="@(new { create = "create", read = "read", write = "write", destroy ="destroy" })"> 
        </e-treegrid-column> 
    </e-treegrid-columns> 
   </ejs-treegrid> 
  
<script> 
  
    //data for multi select using data manager 
   var data = new ej.data.DataManager({ 
    url: '/api/Employee', 
    adaptor: new ej.data.WebApiAdaptor() 
   }); 
    var multiSelectObj, elem; 
    function create(args) { 
        elem = document.createElement('input'); 
        return elem; 
    } 
    function read(args) { 
        return multiSelectObj.value; 
    } 
    function destroy(args) { 
        return multiSelectObj.destroy(); 
    } 
    function write(args) { 
        multiSelectObj = new ej.dropdowns.MultiSelect({ 
            dataSource: data, 
            fields: { value: 'Priority' }, 
              value: Array.isArray(args.rowData["Priority"])? args.rowData["Priority"] :  
                     [args.rowData["Priority"]], 
        }); 
        multiSelectObj.appendTo(elem); 
  
    } 
</script> 
  

Please check the below screenshot, 
  
 
  
Please check the below help documentations, 
  
Kindly get back to us for further assistance. 
  
Regards, 
Padmavathy Kamalanathan 




PS Phil Seeman May 20, 2020 06:11 AM UTC

One thing I'm wanting to do is define certain columns as dropdownlist dynamically. I don't know if this is the best approach but below is what I'm doing. My question is, how do a specify the create/read/write/destroy methods in my JS?

            var customFields = @Html.Raw(Json.Serialize(@ViewBag.CustomFields));
            for (var i = 0; i < customFields.length; i++) {
                detail.columns[i].headerText = customFields[i].Name;
                detail.columns[i].visible = true;
                detail.columns[i].showInColumnChooser = true;
                switch (customFields[i].FieldType) {
                    case "number":
                        .......
                        break;
                    case "enum":
                        detail.columns[i].edit="@(new { create = "create", read = "read", write = "write", destroy ="destroy" })"
                        break;
                    default:
                }
            }

The highlighted syntax will not work - the question is, how do I set the create/read/write/destroy function destinations at that point?



PK Padmavathy Kamalanathan Syncfusion Team May 21, 2020 01:44 PM UTC

Hi Phil, 
  
Thanks for your update. 
  
QUERY: need to define certain columns as dropdownlist with edit template dynamically in JS 
  
We have achieved your requirement by setting the edit template of particular column in the load event of Tree Grid as shown below, 
  
  
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" idMapping="TaskId"  
        parentIdMapping='ParentId' 
       height="270" treeColumnIndex="1"  load="load"> 
       ---------------- 
    </ejs-treegrid> 
  
    function load(args) { 
       
      for (var i = 0; i < customFields.length; i++) { 
         
            if(customFields[i].FieldType == "string"){ 
                // setting edit template for column based on type 
                this.columns[i].edit = { 
                   create: function () { 
                        elem = document.createElement('input'); 
                        return elem; 
                    }, 
                    read: function () { 
                           return multiSelectObj.value; 
                    }, 
                    destroy: function () { 
                         return multiSelectObj.destroy(); 
                    }, 
                    write: function (args) { 
                       multiSelectObj = new ej.dropdowns.MultiSelect({ 
                       dataSource: data, 
                       fields: { value: 'Priority' }, 
                       value: Array.isArray(args.rowData["Priority"])?  
                                 args.rowData["Priority"] :  
                               [args.rowData["Priority"]], 
                       }); 
                     multiSelectObj.appendTo(elem); 
                    } 
                } 
                
            } 
          
        } 
    
    } 
  
Please check the below help documentation, 
  
Kindly get back to us for further assistance. 
  
Regards, 
Padmavathy Kamalanathan 




DA David June 2, 2021 12:41 PM UTC

Hi,

I am trying to do this samle with .net core 5. I tried to do the same thing in my app, but kept getting request failed.

After banging my head against a wall for a while, I updated the sample below to .net core 5, and the same error occurred.

I have attached the updated project in a zip. Wondering how this is done now?

Thanks,
David

Attachment: TestSample_3fe80e5d.7z


PK Padmavathy Kamalanathan Syncfusion Team June 4, 2021 03:14 PM UTC

Hi David, 
 
Thanks for contacting Syncfusion Forums. 
 
We can reproduce the reported issue at our end with .net core 5. We are validating the cause of the issue and we will update further details on or before 8th June 2021. 
 
Until then we appreciate your patience. 
 
Regards, 
Padmavathy Kamalanathan 



PK Padmavathy Kamalanathan Syncfusion Team June 8, 2021 07:36 AM UTC

Hi David, 
 
We appreciate your patience. 
 
We have checked the reported issue in WEB API controller with .NET Core 5.0 and found that the data (Items and Count) has been returned from server end in the camel case. We need to return it in pascal case for proper data binding. So, we suggest you add the below configuration in Startup.cs file to get rid of the reported issue.  
 
services.AddControllers()  
            .AddJsonOptions(options =>  
            {  
                options.JsonSerializerOptions.PropertyNamingPolicy = null;  });  
        }  
 
Kindly get back to us for further assistance. 
 
Regards, 
Padmavathy Kamalanathan 



DA David June 9, 2021 08:36 AM UTC

Hi Padmavathy,

This works perfectly. 
Thanks,
David


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team June 10, 2021 08:54 AM UTC

Hi David, 

Thanks for your update. Please get back to us if you need any further assistance. We are happy to assist you. 

Regards, 
Farveen sulthana T 


Loader.
Up arrow icon