I have an upload button with a submit button. I also have a dropdown connected to the same submit button. The drop down labels and values are connected to another dataset. I have an onlclick that loads the dropdown as I do not want the user to have to do it and I plan to hide it once this works. Right now it looks like the correct item is chosen but when I submit the center_id is blank. Any thoughts?
$w.onReady( function () {
$w( “#MemberCenter” ).onReady(() => {
$w( “#repeater1” ).onItemReady(($item, item, itemData, index) => {
$item( “#addCenterFiles” ).onClick((event) => {
$w( "#centerID" ).selectedIndex = itemData
});
});
});
});
$w.onReady( function () {
$w( “#myUploadButton” ).fileType = “Document” ;
$w( “#repeater1” ).onItemReady(($item, item, itemData, index) => {
$item( ‘#myButton’ ).onClick(() => {
if ($w( “#myUploadButton” ).value.length > 0 ) {
console.log( "Uploading " + $w( “#myUploadButton” ).value[ 0 ].name);
$w( “#myUploadButton” ).startUpload()
.then((uploadedFile) => {
console.log( “Upload successful. File is available here:” );
console.log(uploadedFile.url);
})
. catch ((uploadError) => {
console.log( "File upload error: " + uploadError.errorCode);
console.log(uploadError.errorDescription);
});
} else {
console.log( “Please choose a file to upload.” )
}
});
})
})
Hey Bill,
I think there may be an issue with how you’re accessing the item. Without knowing the structure of your DB, I’d point you to two things quick
- Your onItemReady callback function has the wrong parameters. The three parameters should be the $item selector, the item object (the name doesn’t matter) and index.
$w("#repeater1").onItemReady(($item, item, itemData, index)
should be
$w("#repeater1").onItemReady(($item, itemData, index)
- Check out the value that you’re setting. You could be setting $center_id as the value, when it’s really the label.
Let me know if this helps.
Thanks for chiming in. Greatly appriciated.
When I made that change it actually made things worse.
My issue is, I have a dropdown with its choices linked to the centers table. Wix makes the lables and values the same when linked to a table. I need the ID to get inserted so I have the dropdown linked to the ID rather than the title but I don’t want the user to have to choose an ID so I made an onclick event that chooses the correct item. All of that works as it should. They click # addCenterFiles and you can clearly see that the drop down has the correct item chosen but when I submit the ID does not get inserted. Everything else gets inserted just fine. Only that one field is left out. If I make the choice from the drop down manually rather than using the onclick it all works perfectly. Below is how the drop choice is getting made. It appears to all work but everything gets inserted except the dropdown value.
$w.onReady( function () {
$w( “#MemberCenter” ).onReady(() => {
$w( “#repeater1” ).onItemReady(($item, item, itemData, index) => {
$item( “#addCenterFiles” ).onClick((event) => {
$w( "#centerID" ).selectedIndex = itemData
});
});
});
});
Really the problem is that wix connects both the label and the value to the same field when the choices are linked to a table which makes no sense at all. It should work the same way it does when you create a list. You choose the label and the value separately.
@bill65358 Agreed, that is the real issue. What you have to do is manually set the values & labels yourself to the dropdown, which you can do with a little bit of code, rather than connecting it via the connector.
In your datasetReady handler (which you may need to create if you don’t have one), try this.
export async function datasetReady(){
const result = await $w("myDatasetId").getItems(0,$w("myDatasetId").getTotalCount()); //This gets the entire list of centers from your connected dataset, in object format.
const dropdownOptions = result.items.map( item => ({label:item.title,value:item._id}) ); //This little function creates an array of options for your dropdown with the label different from the value.
$w("#centerID").options = dropdownOptions; //Set your options.
}
This will get you the correct value of the id to be inserted into the database. Again, let me know if this works for you.
@chris-derrell thanks so much my friend. That did the trick!
@bill65358 Awesome! Glad to hear it. 