简体   繁体   English

在点击表格中的“编辑”按钮后,如何使表格中的一个单元格“内容可编辑”

[英]How can I make one cell in a table “content editable” after hitting and edit button in the table

I am working on a permissions page on my site and currently have it sets up with three columns Username Permissions and Action . 我正在网站上的权限页面上工作,当前将其设置为三列Username PermissionsAction When I press the Edit button in the Action column I want to be able to edit the Permissions column only in that specific row. 当我在“ Action列中按“ Edit按钮时,我只希望能够在该特定行中编辑“ Permissions列。 Right now I have it setting both the Username and Permissions cell to be contenteditable. 现在,我将“ Username和“ Permissions单元格都设置为可编辑的。

Here is is my definition of my table (data being pulled from DB): 这是我对表的定义(从数据库中提取数据):

echo "<td class='username'>" . $row['username'] . "</td>";
echo "<td class='permissions'>" . $row['isAdmin'] . "</td>";
echo "<td><input type='button' class='btn btn-primary edit' value='Edit'></td>"

And Here is the script I am currently using to change each column in the row : 这是我当前用于更改行中每一列的脚本:

var $this = $(this);
var tds = $this.closest('tr').find('td').filter(function () {
    return $(this).find('.edit').length === 0;
});
//console.log(tds);
if ($this.val() === 'Edit') {
   $this.val('Save');
   if($this.id !== '.edit') {
       tds.prop('contenteditable', true);
   }
}

I need it to only select the <td class='permissions'> in the current row that the button was clicked on. 我只需要在单击按钮的当前行中选择<td class='permissions'>

What is the proper way to select only that cell in the corresponding row? 仅选择相应行中该单元格的正确方法是什么?

I've tried multiple ways to try and only select it from tds based on the class but have had no success. 我尝试了多种方法尝试仅根据类从tds中选择它,但没有成功。

Instead of using the rather complicated construction 而不是使用相当复杂的结构

var tds = $this.closest('tr').find('td').filter(function () {
  return $(this).find('.edit').length === 0;
});

you could simply do 你可以简单地做

var tds = $(this).closest('tr').find('td.permissions');

to identify the target element to make editable. 确定要编辑的目标元素。 I assume that this code is part of the 'click'-event function for the "edit" button.. 我假定此代码是“编辑”按钮的“单击”事件功能的一部分。

I have made a fully editable HTML Table using DataTables and some custom code. 我已经使用DataTables和一些自定义代码制作了一个完全可编辑的HTML Table。 I can't share the whole code though, as it is a closed-source project for work, but I think it will be helpfull. 我不能共享整个代码,因为它是一个封闭的工作项目,但是我认为这会有所帮助。

Take a look at my repo 看看我的回购

Some useful notes: 一些有用的注意事项:

1) I used Mutation Observer Api to track changes in cell values. 1)我使用了Mutation Observer Api来跟踪单元格值的变化。 I stored every changed cell in an array and on clicking 'Save' the array gets sent with AJAX to the back-end. 我将每个更改的单元格存储在一个数组中,然后单击“保存”,该数组将随AJAX发送到后端。

2) There is a HTML attribute called contenteditable . 2)有一个称为contenteditable的HTML属性。 It lets you edit, well, the content of an element. 它使您可以编辑元素的内容。 Use that to edit the cells' values before appending them to the array. 使用它来编辑单元格的值,然后再将其附加到数组。 Attach an event listener to the 'Edit' button and when it is clicked, add this attribute to all the 'td' elements of your table. 将事件侦听器附加到“编辑”按钮,然后单击它,然后将此属性添加到表的所有“ td”元素中。

3) You just send the array with values to the back-end and run some simple queries to your database. 3)您只需将带有值的数组发送到后端,然后对数据库运行一些简单的查询。

Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM