简体   繁体   English

如何制作部分不可编辑的表格单元格?

[英]How to make a partially un-editable table cell?

This might be kind of a weird question, but is it possible to build table cells that are partially editable, but also partially un-editable?这可能是一个奇怪的问题,但是是否可以构建部分可编辑但部分不可编辑的表格单元格? By this I mean, I want to use JavaScript to make a majority of the cell editable by setting .contentEditable = true in JavaScript (already done).我的意思是,我想使用 JavaScript 通过在 JavaScript 中设置.contentEditable = true来使大部分单元格可编辑(已经完成)。 However, I also want a portion (preferably on the right side) to be text that cannot be written over or edited.但是,我还希望有一部分(最好在右侧)是无法覆盖或编辑的文本。

What I'm trying to do here is make most of the table cell editable to put in raw data, ie numbers.我在这里要做的是使大部分表格单元格可编辑以放入原始数据,即数字。 But to leave an stagnate section with a unit, so that the unit doesn't actually get taken as input (it's more just like decoration).但是要留下一个单位的停滞部分,以便该单位实际上不会被视为输入(它更像是装饰)。

I've drawn a mock-up of what I'm imagining, if this helps:如果这有帮助,我已经绘制了我想象的模型:

小样

So far, I've tried adding a <input> element after the initial input following <td> , then style the input to set point-events: none .到目前为止,我已经尝试在<td>之后的初始输入之后添加一个<input>元素,然后将输入设置为设置point-events: none But this does not look good and ends up creating an input form inside of a content editable cell, which isn't really what I want, and ends up looking like this:但这看起来不太好,最终在内容可编辑单元格中创建了一个输入表单,这并不是我真正想要的,最终看起来像这样:

截屏

I think it would have to use some CSS overlay and not actually be apart of the table HTML.我认为它必须使用一些 CSS 覆盖,而不是实际上是表 HTML 的一部分。 Either way, how would I go about this?无论哪种方式,我将如何 go 关于这个?

You could just use two span elements, make one of them contenteditable .您可以只使用两个span元素,将其中一个contenteditable Example:例子:

 table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid black; }.input { display: flex; justify-content: space-between } span { padding: 0.5em; }.value { flex-grow: 1; }
 <table> <thead> <tr> <th>Heading 1</th> <th>Heading 2</th> </tr> </thead> <tbody> <tr> <td>Entry 1</td> <td class="input"><span class="value" contenteditable="">10</span><span class="unit">kg</span></td> </tr> <tr> <td>Entry 1</td> <td class="input"><span class="value" contenteditable="">20</span><span class="unit">kg</span></td> </tr> </tbody> </table>

Alternatively, you could use the after pseudo-selector to add the unit.或者,您可以使用after伪选择器添加单位。 Example:例子:

 table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid black; }.value::after { content: " kg"; }
 <table> <thead> <tr> <th>Heading 1</th> <th>Heading 2</th> </tr> </thead> <tbody> <tr> <td>Entry 1</td> <td class="input"><span class="value" contenteditable="">10</span></td> </tr> <tr> <td>Entry 1</td> <td class="input"><span class="value" contenteditable="">20</span></td> </tr> </tbody> </table>

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

相关问题 如何使部分不可编辑的表格单元格与内联 JavaScript 函数兼容? - How to make a partially un-editable table cell compatible with inline JavaScript functions? 通过jquery设置文本框内容前缀和后缀,使其不可编辑? - Set textbox content prefix and postfix via jquery and make it un-editable? 使ExtJS GridPanel的某些单元格不可编辑 - making certain cells of an ExtJS GridPanel un-editable 将不可编辑的文本添加到contentEditable div的每一行的末尾 - Adding Un-editable text to the end of each line of a contentEditable div 如何将HTML表格单元格转换为可编辑的文本框 - How to make a HTML table cell into a editable text box 双击可使表格单元格可编辑 - Angularjs - Make table cell editable on double click - Angularjs 在点击表格中的“编辑”按钮后,如何使表格中的一个单元格“内容可编辑” - How can I make one cell in a table “content editable” after hitting and edit button in the table 如何使可编辑HTML表格中的表格单元格仅接受数字(无字母)? - How to make a table cell in the editable HTML table to accept numbers only (no letters)? 如何在Backgrid中将特定的可编辑单元格设置为不可编辑 - How to make a specific editable cell as non editable in backgrid 如何在jqGrid中动态编辑单元格 - How to make cell editable dynamically in jqGrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM