Is it possible to delete an image from a gallery by code? I’ve got a database with an embedded media gallery. I tried using wixData.remove but it doesn’t seem to be deleting the image from the gallery. Please assist. Here’s the code so far.
import wixData from ‘wix-data’ ;
$w.onReady( function () {
let image = event.item.title let imagecover= event.item.src
const index = event.itemIndex;
console.log ( "Gallery index=" + index)
$w( ‘#input1’ ).value=image
$w( ‘#cover’ ).src=imagecover
// then below i am placing that title into the #input1 box
})
})
// the user selects an image in the gallery below which is already connected to 'dynamicDataset and that image title is stored
export function text30_click(event) {
Let’s say you want to remove the first item in a media gallery filed.
Then:
//let's say the media gallery filed key is "images"
//get the data record let's call it "item"
item.images.shift(); // this line removes the first element from the array.
wixData.update("CollectionName", item);
Thank you for your response J.D, Much appreciate it.
I’m trying to delete a specific record after retrieving it from the query. I have the information of the image from the query including its src value, index number in the database etc.
I tried to include the variable/ index number such as “item.images.shift(0)” and "item.images.shift(imagecover)"but this doesnt work.
Hi @jonatandor35 , here’s the code. I have two problems now.
Whilst testing I had placed the splice in the onReady function but for some reason it only temporarily deleted the record. When I refresh the page the record seems to reappear.
I moved the action from the onReady and attempted to delete the record in the button2 onclick event, but I’m not sure how to recall the item which is stored as “index”. I read that once “var” is stored outside of a function it can be called anywhere. However, the index number generated is a response to the user selecting the image via the onItemClicked, so how can I get the index number stored so that it can be used in the export function?
import WixData from 'Wix-data';
$w.onReady (function () {
$w("#gallery1").onItemClicked ((event)=>{
var index= event.itemIndex;
$w("#text31").text= index
console.log("Gallery Item Title=" + event.item.title)
console.log ("gallery#:", index)
let image= event.item.title
let imagecover= event.item.src
let eventname= $w('#text28').text
$w('#text30').text= image
$w('#cover).src= imagecover
})
})
export function button2_click(event){
let currentItem=($w("#dynamicDataset").getCurrentItem())
console.log(currentItem)
currentItem.media.splice(index,1)
console.log("item removed")
wixData.update("Gallery", currentItem)
$w("#gallery1").items= [{currentItem}]
console.log(currentItem)
}