Pruteanu Alexandru
Pruteanu Alexandru

Reputation: 247

Change input type="file" to get value from array

Before entering the images into the database, I used js to display the user's images before they were added to the database which I managed to do. But I also want to add a feature that can delete certain images.

I have an input type = "file" from where I insert the data from the pc.

<div class="form-group">
<input class="form__upload" 
  name="images" 
  id="creative---Point19981022--newPostPhoto" 
  type="file" multiple="" accept="image/*">
<label for="creative---Point19981022--newPostPhoto">Choose photo</label>
</div>

In order to add the wiper functionality, we stored the input data in an array of objects, I created a new array where I store the data I want to delete and then use array.filter to separate the information.

$this.data('delimg'); -> <div class="delete_image_output" data-delimg="0">x</div>

let prepareArrayList = new Array();

  $(document).on('click','.delete_image_output',function(){
  //Get Image From Fill
  let filesArr = document.getElementById('creative---Point19981022--newPostPhoto').files;
  let arrayImg = [...filesArr];
  //Delegation which get data-delimg value
  let $this = $(this);
  let renderOutpudId = $this.data('delimg');
  //get value started from data-delimg value
  const delElelemnt = arrayImg[renderOutpudId];
  //push deleted value in a new array which will by use to make a filter
   prepareArrayList.push(delElelemnt)

   //Make a filter with value coming from array with deleted element
   arrayImg = arrayImg.filter(f => !prepareArrayList.includes(f));

    //at the end arrayImg will has the remaining values
    console.log(arrayImg)

});

In the end I manage to have an array with the selected images but there is a problem saving the information in the database.

But how can I change the code in the structure below, instead of the value from the input to take the value from the array above?

let createPost = document.querySelector('.createNew--post--creativePoint');
if(createPost){
createPost.addEventListener('submit',(crPos)=>{
    crPos.preventDefault();

    let filesImg = document.getElementById('creative---Point19981022--newPostPhoto').files;
    const postData = new FormData();
        postData.append('title',document.getElementById('creative---Point19981022-create--newPostTitle').value);
        postData.append('link',document.getElementById('creative---Point19981022-create--newPostLink').value);
        postData.append('description',document.getElementById('creative---Point19981022--newPostDescription').value);
        postData.append('datePost',document.getElementById('creative---Point19981022--dataNow').value);
        // Using For loop for miltiple images
        for (let p = 0; p < filesImg.length; p++) {
            postData.append('images', filesImg[p]);
        }
        postData.append('youtubeEmbedVideo',document.getElementById('creative---Point199810022-create-embedIdOnly').value);
        postData.append('author',document.getElementById('creative---Point19981022--authorDate').value);
    
    // Do not allow posts if all fields al empty
    if( document.getElementById('creative---Point19981022-create--newPostTitle').value != "" || 
        document.getElementById('creative---Point19981022--newPostPhoto').files.length != 0  || 
        document.getElementById('creative---Point19981022-create--newPostLink').value  != "" ||
        document.getElementById('creative---Point19981022--newPostDescription').value  != "" ||
        document.getElementById('creative---Point199810022-create-embedIdOnly').value  != ""){
            //createPostFnc(postData);
            console.log(filesImg)
    }else{
        showAlert('error',' No field was filled in, the post cannot be processed');
    }    
});

filesImg instead of the input value take the value from the array and adjust the structure

Upvotes: 0

Views: 413

Answers (1)

Louys Patrice Bessette
Louys Patrice Bessette

Reputation: 33943

So, if I understand correctly, you wish to send a filtered array of files instead of all the files that were selected by the input.

I would try the use of a ternary operator on filesImg to check if the "filtered array" is empty or not. Because maybe the user didn't delete any...

let filesImg = (arrayImg.length>0) ? arrayImg : document.getElementById('creative---Point19981022--newPostPhoto').files;

You have to declare arrayImg at the global scope, just like you did for prepareArrayList.

Upvotes: 1

Related Questions