We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

TypeScript example - custom cell dropdown

Hi, 

I read the thread https://www.syncfusion.com/forums/136659/custom-cell-edittemplate. It helps when we create a datagrid that has different edit types in the same column. It also shows how to change the datasource of the dropdown cell edit from a local item list using JavaScript:
var items = [{ text: "ListItem 1", value: "item1" },  
                    { text: "ListItem 2", value: "item2" }, 
                    { text: "ListItem 3", value: "item3" }, 
                    { text: "ListItem 4", value: "item4" }, 
                    { text: "ListItem 5", value: "item5" }];  

Do you have a TypeScript example? I am struggling on a TS implementation.

Thank you

5 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team October 11, 2019 10:29 AM UTC

Hi Jack , 
 
As requested , a sample for cell editing dropdown is attached below. Please follow the below steps for more clarifications. In the below sample the dropdown is rendered in the shipcountry column. 
 
 
let itemElem: HTMLElement; 
let itemObj: DropDownList; 
 
let items: { [key: string]: Object }[] = [ 
  { text: "France", value: "item1" },   
  { text: "Germany", value: "item2" },  
  { text: "Brazil", value: "item3" },  
  { text: "Belgium", value: "item4" },  
  { text: "Switzerland", value: "item5" }];   
 
    let grid: Grid = new Grid( 
        { 
         
            dataSource: orderDataSource, 
            editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Normal' }, 
            allowPaging: true, 
            pageSettings: { pageSize: 6, pageCount: 5 }, 
            toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'], 
            columns: [ 
                 . . . 
                        . . . 
                { 
                    field: 'ShipCountry', headerText: 'Ship Country',width: 160, edit:{ 
                
                    create: () => { 
                    itemElem = document.createElement('input'); 
                    return itemElem; 
                    }, 
                     
                    read: () => { 
                      return itemObj.text; 
                    }, 
 
                    destroy:() =>{ 
                      itemObj.text; 
                    }, 
 
                    write: (e) => { 
                    var Data = e.rowData.ShipCountry; 
                    itemObj = new DropDownList({ 
                    dataSource: items, 
                    fields : { value:'value' , text :'text'},        
                    text: Data , 
                    floatLabelType: 'Never' 
                    }); 
                     
                  itemObj.appendTo(itemElem); 
                    } 
                 
                }   
              ], 
        }); 
    grid.appendTo('#Grid'); 
 
 
 
Regards, 
Seeni Sakthi Kumar S 



JA Jack October 11, 2019 04:47 PM UTC

Hi Seeni,

Thank you so much for the example. It works.

In my case, I want to use this local dropdown for the ShipCountry column depending on the value from the other column. For example when CustomerID = VINET, we use this local dropdown, otherwise we use the defaultedit as the editType for the ShipCountry column. How to do that?

Thanks
Jack




SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team October 14, 2019 01:05 PM UTC

 
Hi Jack , 
 
We could see you would like to render the dropdown and textbox and make a toggle between them based  on the value of the other columns. We can achieve your requirement using the edit template feature of the Grid. Refer to the following code example. 
 
index.ts 

{
 field: 'ShipCountry', headerText: 'Ship Country',width: 160, edit:{
 
                
                    create: () => { 
                    itemElem = document.createElement('input'); 
                    return itemElem; 
                    },

                    write: (e) => { 
                    if(e.rowData.CustomerID == "VINET"){ 
                    var Data = e.rowData.ShipCountry; 
                    itemObj = new DropDownList({ 
                    dataSource: items, 
                    fields : { value:'value' , text :'text'},        
                    text: Data , 
                    floatLabelType: 'Never' 
                    }); 
                  itemObj.appendTo(itemElem); 
                    } 
                    else{ 
                      var Pole = e.rowData.ShipCountry; 
                      itemElem.value = Pole; 
                    } 
                    } 
                  } 
                }   
              ], 
        }); 
    grid.appendTo('#Grid'); 
  
 
Regards, 
Seeni Sakthi Kumar S 



JA Jack October 14, 2019 11:49 PM UTC

Thank you. That is what I need.


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team October 15, 2019 02:21 PM UTC

Hi Jack,  
  
Thanks for the update.  
  
We are happy to hear your requirement has been achived and you are good to go. 
  
Regards,  
Seeni Sakthi Kumar S 


Loader.
Up arrow icon