javascript - 从二维数组创建可编辑的 HTML 表格

标签 javascript html arrays

所以我正在尝试制作一个抽认卡网站,用户可以在其中添加、编辑和删除抽认卡。有两张卡片 - 正面和背面。用户已经可以添加单词,但不能编辑或删除它们。出于这个问题的目的,我将使用一个示例数组:

var flashcards = [["Uomo", "Man"],["Donna", "Woman"],["Ragazzo", "Boy"]]

但我想要一种更人性化的方式来编辑抽认卡,最好使用这样的表格:

<table>
  <tr>
    <th>Front</th>
    <th>Back</th> 
  </tr>
  <tr>
    <td><input type="text" name="flashcard" value="Uomo"> </td>
    <td><input type="text" name="flashcard" value="Man"></td>
  </tr>
    <tr>
    <td><input type="text" name="flashcard" value="Donna"></td>
    <td><input type="text" name="flashcard" value="Woman"></td>
  </tr>
      <tr>
    <td><input type="text" name="flashcard" value="Ragazzo"></td>
    <td><input type="text" name="flashcard" value="Boy"></td>
  </tr>
</table>

<button type="button">Add more</button>
<br>
<button type="button">Save changes</button>

因此他们可以更新他们的抽认卡编辑输入字段,或单击“添加更多”并创建一个新行。单击“保存更改”会将数组更新为表格的内容。

我不介意它本身不是一个 HTML 表格,而是一些易于用户编辑的东西。

我就是想不出解决这个问题的最佳方法。有什么建议吗?

最佳答案

我已经推荐了 VueJS——它确实是解决这个问题的一个非常好的工具。不管怎样,我已经使用 vanilla JavaScript 编写了一个基本的解决方案。对于编辑部分,它使用 contenteditable HTML 属性,该属性允许最终用户双击一个元素并更改它的文本内容。 html 显示是基本的,因此您可以根据需要更改它

<div id=style="width: 100%;">
  <ul id="table" style="list-style-type: none; display: inline-block;">

  </ul>
</div>
<script>
var flashcards = [["Uomo", "Man"],["Donna", "Woman"],["Ragazzo", "Boy"]];
var displayedCard = []; //Using a parallel array to keep track of which side is shown
for(var i = 0; i < flashcards.length; i++){
    displayedCard.push(0);
}
function renderFlashcardTable(){ //This will do the initial rendering of the table
    let ulTable = document.getElementById("table");
    for(var i = 0; i < flashcards.length; i++){
        let card = flashcards[i];
        let indexOfSideShown = displayedCard[i];
        let li = document.createElement("li");
        let cardValueSpan = document.createElement("span");
        cardValueSpan.innerHTML = card[indexOfSideShown]; //Get the value of the side of the card that is shown
        cardValueSpan.setAttribute("contenteditable", "true"); 
        cardValueSpan.oninput = function(e){ //This method gets called when the user de-selects the element they have been editing
            let li = this.parentElement;
            let sideIndex = parseInt(li.getAttribute("side-index"));
            card[sideIndex] = this.textContent;
        }
        li.appendChild(cardValueSpan);
        li.appendChild(getFlipSidesButton(li));
        li.setAttribute("side-index", indexOfSideShown);
        li.setAttribute("card-index", i);
        ulTable.appendChild(li);
    }
}
function getFlipSidesButton(listItem){//This is generated for each card and when clicked it "flips the switch"
    let btn = document.createElement("button");
    btn.innerHTML = "Flip card";
    btn.onclick = function(e){
        let card = flashcards[listItem.getAttribute("card-index")];
        let index = parseInt(listItem.getAttribute("side-index"));
        let nextSide = (index == 1) ? 0 : 1;
        listItem.setAttribute("side-index", nextSide);
        listItem.children[0].innerHTML = card[nextSide];
    }
    return btn;
}

renderFlashcardTable();
</script>

关于javascript - 从二维数组创建可编辑的 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56381780/

相关文章:

javascript - 解决来自 JSON 对象的循环引用

javascript - 如果有 event.preventDefault 函数,ajaxForm 不工作

arrays - SwiftUI - 列出嵌套数组中的元素

javascript - 为什么所有值都不能使用 Vue JS 在 Html 中动态传递?

javascript - Angular JS $http.success() vs $q.resolve()?

html - 我的 webpack 没有生成我的 css 文件

html - 当隐藏另一个不在 safari 上工作时,使用 flex 增加一个 div

使用数组的 php preg_replace - 具有重音字符的第一个或最后一个字母不起作用

C# StructLayout/FieldOffset 和数组中的索引

javascript - 在多个值的对象数组中搜索