I am coding a website in which admin has access to a dashboard page where they can see the list of users. I would like to add the ability for the admin to change other users’ roles in the same line.
Here is the code:
<table>
<caption> Liste des utilisateurs </caption>
<tr class="title">
<th>Date de création</th>
<th>Prénom</th>
<th>Nom d'utilisateur</th>
<th>Mail</th>
<th>Rôle</th>
<th>Id</th>
</tr>
<tr>
<td>0000-00-00 00:00:00</td>
<td></td>
<td>username</td>
<td>*****@mail.fr</td>
<td>admin</td>
<td>22</td>
<img class="information_modify" src="chemin" alt="img_modifier">
</tr>
<tr>
<td>2022-05-08 09:29:53</td>
<td></td>
<td>username</td>
<td>****@mail.fr</td>
<td>user</td>
<td>23</td>
<img class="information_modify" src="chemin" alt="img_modifier">
</tr>
</table>
But I have two problems:
I don't know how to change
<td>
to<input>
when someone clicks on an image; maybe with JavaScript?Even if I make it, how can I limit the change to only that line, and not all the
<td>
. I use a forEach loop in php to generate this code. So I can't really generate the code with each line personalised.
I need your help. Thanks in advance for your answers.
There are a couple of approaches, here, the first is to explicitly answer your question, and create a system in which – when the
.information_modify
element is clicked – the text-content of the cells are inserted into an<input>
element (and a second click on that.information_modify
element 'saves' the information to the<table>
, saving to the database is beyond the scope of this question):JS Fiddle demo.
The above approach, though, seems a little unnecessary; since a user only really needs the illusion of change to enable editing. With that in mind, the following approach retains
<input>
elements within the table-cells and simply changes their style to indicate editing possibilities:JS Fiddle demo.
With the second approach there is the obvious problem that the selection behaviour is noticeably different than if the cell-contents weren't simply 'disguised'
<td>
elements, because of the default (and apparently non-adjustable)user-select: contain
, which causes a user-selection to be contained within the element in which it started and not to participate in a selection that began elsewhere.Not to mention that I'm choosing to toggle the presence of the
readonly
attribute on the<input>
elements.The preferred approach, I think, will come down to personal preference and project requirements.