E
E
Evgeny Khripunov2018-01-29 19:14:32
Yii
Evgeny Khripunov, 2018-01-29 19:14:32

How to delete attachments to a post when editing a post?

It is possible to create/edit/delete entries on the site. Site on Yii2.
You can attach files when creating an entry. Implemented using a convenient widget
https://github.com/kartik-v/yii2-widget-fileinput
When deleting an entry, we simply unlink and the files are deleted.
When editing, how to implement the deletion of some files of this entry? For example, as on the wall of VKontakte, the user sees the attached files and can delete some of them when editing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2018-01-29
@fannyfan414

Options 2: (I don’t write code - I describe the principle)
1) Ajax - when you click on an element, a request is made to the image deletion action, pass the image object ID in the parameters (you store pictures in a separate table and link them by parent_id with the record, I hope ?). Create an image object by id, call the delete method. At the front, you get the server's response in the Ajax callback, upon successful removal, you cut out the picture and controls for it from the DOM.
Plus solutions - quickly and without rebooting.
Minus - sometimes the picture is deleted by accident, in this case (unlike option 2) a rollback is not possible.
2) In the hidden field, write the IDs of the images to be deleted by clicking on the delete icon (and, accordingly, cut out the image from the DOM), check the field by submit on the server, pull out the IDs of the images from it, then loop as in option 1.
Plus solutions - until the real form is submitted files will not be deleted. That is, everything can be rolled back simply by refreshing the page.
Minus - ... to delete, you need to make a submit, it's not always convenient if you just want to delete a couple of pictures from the post. Looks less user friendly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question