我想让html表的一些单元格可编辑,只需双击一个单元格,输入一些文本即可将更改发送到服务器。我不想使用像dojo数据网格这样的工具包。因为它提供了一些其他功能。您能否提供一些代码片段或有关如何实现它的建议?
答案 0 :(得分:102)
您可以对相关的单元格,行或表使用contenteditable属性。
针对IE8兼容性进行了更新
<table>
<tr><td><div contenteditable>I'm editable</div></td><td><div contenteditable>I'm also editable</div></td></tr>
<tr><td>I'm not editable</td></tr>
</table>
请注意,如果您使表格可编辑,至少在Mozilla中,您可以删除行等。
您还需要检查目标受众的浏览器是否支持此属性。
至于收听更改(以便您可以发送到服务器),请参阅contenteditable change events
答案 1 :(得分:43)
HTML5支持contenteditable,
<table border="3">
<thead>
<tr>Heading 1</tr>
<tr>Heading 2</tr>
</thead>
<tbody>
<tr>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
<tr>
<td contenteditable='true'></td>
<td contenteditable='true'></td>
</tr>
</tbody>
</table>
引用mdn entry on contenteditable
该属性必须采用以下值之一:
true或空字符串,表示该元素必须可编辑;
false,表示该元素不得编辑。
如果未设置此属性,则从其继承其默认值 父元素。
此属性是枚举属性,而不是布尔属性。这意味着 明确使用其中一个值为true,false或为空 string是必需的,并且不允许使用简写...
// wrong not allowed
<label contenteditable>Example Label</label>
// correct usage
<label contenteditable="true">Example Label</label>.
答案 2 :(得分:16)
我有三种方法,
您可以根据自己的要求同时使用Submitting with '<unnamed>'
Error: not compatible with STRSXP
或<input>
。
<强> 1。使用<textarea>
中的输入。
在所有<td>
中使用<input>
元素,
<td>
此外,您可能希望将输入调整为其<tr><td><input type="text"></td>....</tr>
的大小。离。,
td
您可以在未编辑时更改输入框边框的颜色。
<强> 2。使用input { width:100%; height:100%; }
属性。 (HTML5)强>
但是,如果要使用contenteditable='true'
,可能还需要将适当的值保存到数据库中。你可以用ajax实现这个目的。
您可以将关键代码contenteditable='true'
,keyup
,keydown
等附加到keypress
。此外,当用户连续输入时,对这些事件使用一些delay()是很好的,ajax事件不会在每个关键用户按下时触发。例如,
<td>
第3。单击时将$('table td').keyup(function() {
clearTimeout($.data(this, 'timer'));
var wait = setTimeout(saveData, 500); // delay after user types
$(this).data('timer', wait);
});
function saveData() {
// ... ajax ...
}
附加到<input>
。
单击<td>
时在td
中添加输入元素,根据<td>
的值替换其值。输入模糊时,请使用输入值更改`td&#39; s值。所有这一切都与javascript。
答案 3 :(得分:4)
试试这段代码。
$(function () {
$("td").dblclick(function () {
var OriginalContent = $(this).text();
$(this).addClass("cellEditing");
$(this).html("<input type="text" value="" + OriginalContent + "" />");
$(this).children().first().focus();
$(this).children().first().keypress(function (e) {
if (e.which == 13) {
var newContent = $(this).val();
$(this).parent().text(newContent);
$(this).parent().removeClass("cellEditing");
}
});
$(this).children().first().blur(function(){
$(this).parent().text(OriginalContent);
$(this).parent().removeClass("cellEditing");
});
});
});
您还可以访问此链接了解更多详情:
答案 4 :(得分:4)
您可以使用x-editable https://vitalets.github.io/x-editable/ 它来自bootstrap的很棒的库
答案 5 :(得分:4)
我正在将其用于可编辑字段
<table class="table table-bordered table-responsive-md table-striped text-center">
<thead>
<tr>
<th class="text-center">Citation</th>
<th class="text-center">Security</th>
<th class="text-center">Implementation</th>
<th class="text-center">Description</th>
<th class="text-center">Solution</th>
<th class="text-center">Remove</th>
</tr>
</thead>
<tbody>
<tr>
<td class="pt-3-half" contenteditable="false">Aurelia Vega</td>
<td class="pt-3-half" contenteditable="false">30</td>
<td class="pt-3-half" contenteditable="false">Deepends</td>
<td class="pt-3-half" contenteditable="true"><input type="text" name="add1" value="spain" class="border-none"></td>
<td class="pt-3-half" contenteditable="true"><input type="text" name="add1" value="marid" class="border-none"></td>
<td>
<span class="table-remove"><button type="button"
class="btn btn-danger btn-rounded btn-sm my-0">Remove</button></span>
</td>
</tr>
</tbody>
</table>
答案 6 :(得分:3)
如果你使用Jquery,这个插件会帮助你 很简单,但很好
答案 7 :(得分:3)
这是一个可运行的单一示例。
<html>
<head>
<script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script> <!-- jQuery source -->
</head>
<body>
<table align="center">
<tr> <td>id</td> <td>name</td> </tr>
<tr> <td>001</td> <td>dog</td> </tr>
<tr> <td>002</td> <td>cat</td> </tr>
<tr> <td>003</td> <td>pig</td> </tr>
</table>
<script>
$(function(){
$("td").click(function(event){
if($(this).children("input").length > 0)
return false;
var tdObj = $(this);
var preText = tdObj.html();
var inputObj = $("<input type='text' />");
tdObj.html("");
inputObj.width(tdObj.width())
.height(tdObj.height())
.css({border:"0px",fontSize:"17px"})
.val(preText)
.appendTo(tdObj)
.trigger("focus")
.trigger("select");
inputObj.keyup(function(event){
if(13 == event.which) { // press ENTER-key
var text = $(this).val();
tdObj.html(text);
}
else if(27 == event.which) { // press ESC-key
tdObj.html(preText);
}
});
inputObj.click(function(){
return false;
});
});
});
</script>
</body>
</html>
答案 8 :(得分:2)
只需在单击广告时动态地在 # 136 z-index: 1031;
中插入 <input>
元素。只有简单的HTML和Javascript。无需<td>
,contentEditable
,jquery
答案 9 :(得分:0)
这实际上是如此直接, 这是我的HTML,jQuery示例..它就像一个魅力,我使用在线json数据样本构建所有代码。 欢呼声
&LT;&LT; HTML&gt;&gt;
<table id="myTable"></table>
&LT;&LT; jQuery&gt;&gt;
<script>
var url = 'http://jsonplaceholder.typicode.com/posts';
var currentEditedIndex = -1;
$(document).ready(function () {
$.getJSON(url,
function (json) {
var tr;
tr = $('<tr/>');
tr.append("<td>ID</td>");
tr.append("<td>userId</td>");
tr.append("<td>title</td>");
tr.append("<td>body</td>");
tr.append("<td>edit</td>");
$('#myTable').append(tr);
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + json[i].id + "</td>");
tr.append("<td>" + json[i].userId + "</td>");
tr.append("<td>" + json[i].title + "</td>");
tr.append("<td>" + json[i].body + "</td>");
tr.append("<td><input type='button' value='edit' id='edit' onclick='myfunc(" + i + ")' /></td>");
$('#myTable').append(tr);
}
});
});
function myfunc(rowindex) {
rowindex++;
console.log(currentEditedIndex)
if (currentEditedIndex != -1) { //not first time to click
cancelClick(rowindex)
}
else {
cancelClick(currentEditedIndex)
}
currentEditedIndex = rowindex; //update the global variable to current edit location
//get cells values
var cell1 = ($("#myTable tr:eq(" + (rowindex) + ") td:eq(0)").text());
var cell2 = ($("#myTable tr:eq(" + (rowindex) + ") td:eq(1)").text());
var cell3 = ($("#myTable tr:eq(" + (rowindex) + ") td:eq(2)").text());
var cell4 = ($("#myTable tr:eq(" + (rowindex) + ") td:eq(3)").text());
//remove text from previous click
//add a cancel button
$("#myTable tr:eq(" + (rowindex) + ") td:eq(4)").append(" <input type='button' onclick='cancelClick("+rowindex+")' id='cancelBtn' value='Cancel' />");
$("#myTable tr:eq(" + (rowindex) + ") td:eq(4)").css("width", "200");
//make it a text box
$("#myTable tr:eq(" + (rowindex) + ") td:eq(0)").html(" <input type='text' id='mycustomid' value='" + cell1 + "' style='width:30px' />");
$("#myTable tr:eq(" + (rowindex) + ") td:eq(1)").html(" <input type='text' id='mycustomuserId' value='" + cell2 + "' style='width:30px' />");
$("#myTable tr:eq(" + (rowindex) + ") td:eq(2)").html(" <input type='text' id='mycustomtitle' value='" + cell3 + "' style='width:130px' />");
$("#myTable tr:eq(" + (rowindex) + ") td:eq(3)").html(" <input type='text' id='mycustomedit' value='" + cell4 + "' style='width:400px' />");
}
//on cancel, remove the controls and remove the cancel btn
function cancelClick(indx)
{
//console.log('edit is at row>> rowindex:' + currentEditedIndex);
indx = currentEditedIndex;
var cell1 = ($("#myTable #mycustomid").val());
var cell2 = ($("#myTable #mycustomuserId").val());
var cell3 = ($("#myTable #mycustomtitle").val());
var cell4 = ($("#myTable #mycustomedit").val());
$("#myTable tr:eq(" + (indx) + ") td:eq(0)").html(cell1);
$("#myTable tr:eq(" + (indx) + ") td:eq(1)").html(cell2);
$("#myTable tr:eq(" + (indx) + ") td:eq(2)").html(cell3);
$("#myTable tr:eq(" + (indx) + ") td:eq(3)").html(cell4);
$("#myTable tr:eq(" + (indx) + ") td:eq(4)").find('#cancelBtn').remove();
}
</script>
答案 10 :(得分:0)
这是必不可少的,尽管您不需要使代码变得混乱。相反,您可以遍历所有<td>
并添加带有属性的<input>
,最后输入值。
function edit(el) {
el.childNodes[0].removeAttribute("disabled");
el.childNodes[0].focus();
window.getSelection().removeAllRanges();
}
function disable(el) {
el.setAttribute("disabled","");
}
<table border>
<tr>
<td ondblclick="edit(this)"><input value="cell1" disabled onblur="disable(this)"></td>
<td ondblclick="edit(this)"><input value="cell2" disabled onblur="disable(this)"></td>
<td ondblclick="edit(this)"><input value="cell3" disabled onblur="disable(this)"></td>
<td ondblclick="edit(this)"><input value="so forth..." disabled onblur="disable(this)">
</td>
</tr>
</table>
答案 11 :(得分:0)
点击<input>
到<td>
。
当其模糊时,将<input>
更改为<span>
。
答案 12 :(得分:-1)
如果你的表格有很多功能(排序、导出、更新、编辑等),
我会推荐引导表。
<块引用>关于编辑实践:
侦听事件:click-cell.bs.table 然后在单击时将属性 contenteditable 添加到 td
您可能不想允许在每一列中进行编辑,因此我自己使用 JS 添加了 data-editable
属性来确定这一点。
Title
列允许您编辑
<!DOCTYPE html>
<html>
<head>
<!-- jquery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous"></script>
<!-- bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
<!-- bootstrap-table-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.18.3/bootstrap-table.min.css" integrity="sha512-5RNDl2gYvm6wpoVAU4J2+cMGZQeE2o4/AksK/bi355p/C31aRibC93EYxXczXq3ja2PJj60uifzcocu2Ca2FBg==" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.18.3/bootstrap-table.min.js" integrity="sha512-Wm00XTqNHcGqQgiDlZVpK4QIhO2MmMJfzNJfh8wwbBC9BR0FtdJwPqDhEYy8jCfKEhWWZe/LDB6FwY7YE9QhMg==" crossorigin="anonymous"></script>
<!--bootstrap-table-lanuage-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.18.3/bootstrap-table-locale-all.min.js" integrity="sha512-1PCRWIvrSQaZjCRWaa0GHWKr1jQA8u79VnIvkAme6BKeoNWe5N89peawTXdVp+kukb8rzNsEY89ocMJqVivdSA==" crossorigin="anonymous"></script>
<!--bootstrap-table-export-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.18.3/extensions/export/bootstrap-table-export.min.js" integrity="sha512-cAMZL39BuY4jWHUkLWRS+TlHzd/riowdz6RNNVI6CdKRQw1p1rDn8n34lu6pricfL0i8YXeWQIDF5Xa/HBVLRg==" crossorigin="anonymous"></script>
<!-- screenshots -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4.2.8/dist/es6-promise.auto.min.js" integrity="sha256-Xxrdry6fWSKu1j2ALyuK/gHFQ+2Bp76ZrR6f3QXI47Y=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.0.0-rc.7/dist/html2canvas.min.js" integrity="sha256-Ax1aqtvxWBY0xWND+tPZVva/VQZy9t1Ce17ZJO+NTRc=" crossorigin="anonymous"></script>
<!-- tableexport.jquery.plugin If you want to export, then you must add it. -->
<script src="https://cdn.jsdelivr.net/npm/tableexport.jquery.plugin@1.10.22/tableExport.min.js" integrity="sha256-Dsris8trQzzQXIM6PgMzSugaNyUacxaR9o2VrJalh6Y=" crossorigin="anonymous"></script>
<!-- font-awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js" integrity="sha512-RXf+QSDCUQs5uwRKaDoXt55jygZZm2V++WUZduaU/Ui/9EGp3f/2KZVahFZBKGH0s774sd3HmrhUy+SgOFQLVQ==" crossorigin="anonymous"></script>
<style>
html {
font-family: sans-serif;
line-height: 1.15;
-webkit-text-size-adjust: 100%;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
h1, h2, h3, h4, h5, h6 {
margin-top: 0;
margin-bottom: 0.5rem;
color: #004a88;
text-align: center;
}
.table-blue {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
.table-blue td, .table-blue th {
border: 1px solid #ddd;
padding: 8px;
}
.table-blue tr:hover {background-color: #ddd;}
.table-blue th {
background-color: #004a88;
font-size: larger;
font-weight: bold;
padding-top: 5px;
padding-bottom: 5px;
text-align: left;
color: white;
}
/* https://stackoverflow.com/a/63412885 */
thead, tbody tr {
display: table;
width: 100%;
table-layout: fixed;
}
tbody {
display: block;
overflow-y: auto;
table-layout: fixed;
max-height: 512px;
}
td {
word-break: break-all;
}
</style>
</head>
<body>
<!-- Table-options:
- https://bootstrap-table.com/docs/api/table-options/
- https://bootstrap-table.com/docs/extensions/export/
-->
<table id="myTable" class="table table-striped table-blue"
data-toggle="table"
data-search="true"
data-search-highlight="true"
data-show-refresh="true"
data-show-toggle="true"
data-show-columns="true"
data-show-export="true"
data-minimum-count-columns="2"
data-show-pagination-switch="true"
data-pagination="true"
data-id-field="id"
data-page-list="[10, 25, 50, 100, ALL]"
data-show-footer="false"
data-side-pagination="client"
data-export-types='["csv", "json", "excel", "doc", "sql", "png"]'
data-editable = '[false, true, false, false]'
data-export-options='{
"fileName": "products"
}'
data-url="https://jsonplaceholder.typicode.com/photos">
<thead>
<tr>
<th data-sortable="true" data-field="id">Id</th>
<th data-sortable="true" data-field="title">Title</th>
<th data-sortable="true" data-field="url">URL</th>
<th data-sortable="true" data-formatter="imageFormatter" data-field="thumbnailUrl">Thumbnail URL</th>
</tr>
</thead>
</table>
</body>
<script>
const TABLE_ID = "myTable";
const TABLE = document.getElementById(TABLE_ID)
window.onload = () => {
const table = $(`#${TABLE_ID}`)
function imageFormatter(value, row) {
return `<img src="${value}" style="width:60px;height:60px" loading="lazy"/>`;
}
function saveData(tdData) {
// ... ajax ...
console.log("save")
}
const infoEditable = JSON.parse(TABLE.getAttribute("data-editable"))
if (infoEditable === null) {
return
}
table.on('click-cell.bs.table', function (event, field, value, row, td) {
td = td[0]
if (td.getAttribute("contentEditable")) {
return
}
const index = Array.prototype.indexOf.call(td.parentNode.children, td)
if (infoEditable[index]) {
td.contentEditable = "true"
}
td.addEventListener("keyup", (event) => {
clearTimeout($.data(this, 'timer'));
const wait = setTimeout(saveData, 1000); // delay after user types
$(this).data('timer', wait);
})
})
}
</script>
</html>