How can I update Dropdown Items dynamically with JS

Dear @dat,
Please, try it like this:

fd.field('DropDown0').widget.setDataSource(datasource);var datasource = [
  { value: 0, text: "A" },
  { value: 1, text: "B" },
  { value: 2, text: "C" },
  { value: 3, text: "D" },
  { value: 4, text: "E" }
];
fd.field('DropDown0').widget.setDataSource(datasource);
fd.field('DropDown0').widget.setOptions({dataTextField : 'text', dataValueField: 'value'});
2 Likes

hi I have tried this, however, when I select the option in dropdown, it will show [object Object]

 fd.control('DataTable1').$on('edit', function(e) {
           console.log("DataTable1 edit => " + e.column.field);
            if (e.column.field === 'Location'){        
               populateCategories(e.widget, e.model.Location, "Location");     
            }
    }); 

image

           var location = [];
            pnp.sp.web.lists.getByTitle('Location Master Data').items.select('Title', 'Description').get().then(function(items){
                    
                items.forEach(item => {
                    location.push({value: item.Title, text: item.Description});
                    //location.push({text: item.Description});
                });
                widget.setDataSource({data: location});
                widget.setOptions({dataTextField : "text", dataValueField: "value"});   
                widget._hideBusy(); 
            });

Dear @Derek_wong,
I don't understand your code, we have an example here -

Here's the code you need to use:

fd.spRendered(function() {
    fd.control('DataTable1').$on('edit', function(e) {
        if (e.column.field === 'Location') {
            //pass widget + current Location value
            populateLocations(e.widget, e.model.Location);
        }
    })

});

function populateLocations(widget, value) {
    //will show as loading
    widget._showBusy();

    sp.web.lists.getByTitle('Location Master Data').items
        .select('ID', 'Title')
        .get()
        .then(function(items) {
            //set options
            widget.setDataSource({
                data: items.map(function(i) { return i.Title })
            });

            //set value if one was select
            widget.value(value);
            //hide loading state
            widget._hideBusy();
        });
}

@Nikita_Kurguzov ,

I need to display text and get the selected value , same as your previous example. However, after I have selected the item, it only show the [object Object] instead of the text or value

fd.field('DropDown0').widget.setDataSource(datasource);var datasource = [
{ value: 0, text: "A" },
{ value: 1, text: "B" },
{ value: 2, text: "C" },
{ value: 3, text: "D" },
{ value: 4, text: "E" }
];
fd.field('DropDown0').widget.setDataSource(datasource);
fd.field('DropDown0').widget.setOptions({dataTextField : 'text', dataValueField: 'value'});

Dear @Derek_wong,
But you're doing something else in your sample above. By the way, what's the version of the app package you're using? Check it in your app catalog:
image

If you have older version than 1.8.1, please, follow update instruction here - Update the app package for Plumsail Forms (SharePoint Online) — SharePoint forms

@Nikita_Kurguzov May I know where can find this version in sharepoint ?

image

I am using 1.8.8 designer

Dear @Derek_wong,
This is not the same, this is the version of the desktop app. You need to check the app package. It can be found in your app catalog, the URL will be different depending on your setup.

You go to Admin Center > SharePoint Admin Center >More features > Apps:

App catalog:
image

Distribute apps for SharePoint:

will that matter on the drop down showing object Object instead of text value?

Dear @Derek_wong,
It matters for everything, including this issue. It's likely that the functionality was updated with the app package, and we cannot help if we are not sure which version you're using.

I am not the admin on sharepoint , I will check tmr with the right person, May I know why it will keep show this error. when I click the dropdown in the datatable
image

btw..I just got the information
image

Dear @Derek_wong,
Yes, this is a SharePoint issue not related to forms. Feel free to ignore, it has no negative consequences, and SharePoint often gives a lot of warnings in console that don't mean much, so it's just the usual :slight_smile:

@Nikita_Kurguzov
I got the version with 1.0.8.0 at attached. not sure is it related to my current problem on Object /object on drop down.

image

Dear @Derek_wong,
Just checked it, and the value/text being different is actually not available in the Data Table dropdowns, only in Dropdown fields on the form itself. If you're interested in implementing it for the Data Table columns as well, we can offer paid support, contact us at support@plumsail.com

It's likely going to be added sooner or later, but it's not a priority as far as I know, and we can speed up the development process for a small fee.

I would also recommend to update to v1.0.8.1 for various fixes and features - Update the app package for Plumsail Forms (SharePoint Online) — SharePoint forms

@Nikita_Kurguzov May I know any workaround which can suggest me?

Dear @Derek_wong,
You can use just text, like this?

fd.rendered(function() {
    fd.control('DataTable1').$on('edit', function(e) {
        console.log(e)
        if (e.column.field === "Column1") {
            //pass widget + current column value
            console.log(e.model);
            populateColumn(e.widget, e.model.Column1);
        }
    })

});

function populateColumn(widget, value) {

    widget.setDataSource({
        data: ['Category A', 'Category B', 'Category C']
    });

    //set value if one was select
    widget.value(value);
}

Hi @Nikita_Kurguzov ! I'm having a problem with dropdown. I set the dropdown data upon load but the saved data is not being displayed on Edit form.
Do you have some suggestions? thank you

Hello @Jes_Santimosa,

Are you using forms for SharePoint? What is the type of the drop down field? Is it a common or SharePoint field?

You can share the screenshot of the field settings for me to check:
image

If this is a common field, you need to save its value to a SharePoint field:

    //fill SharePoint field with the selected value
    fd.field('DropDown1').$on('change', function() {
        fd.field('DepartmentCode').value = fd.field("DropDown1").value;
    });

Edit form: I'm using sharepoint dropdown field and I'm populating the data upon load but the saved data doesn't show.