简体   繁体   English

Javascript /将输入数据保存到表格单元中

[英]Javascript / Save input data into a table cell

The structure is: 结构为:

  1. You click on a cell, 您单击一个单元格,
  2. The pop-up window appears, 出现弹出窗口,
  3. You type something on the input fields. 您在输入字段上键入内容。

What I want is to store the typed data into the cell which was clicked. 我想要的是将键入的数据存储到单击的单元格中。

This could be perfectly done with Angular.js , but I need to do it using Javascript only (no frameworks, libraries etc.). 这可以使用Angular.js完美地完成,但是我只需要使用Javascript(无需框架,库等)即可完成。

Here's my table with the pop-up window where the input fields are. 这是我的表,其中带有输入字段的弹出窗口。

在此处输入图片说明

I have been trying to store the data simply by using .innerHTML method, but it just deletes the dates and inserts my text. 我一直试图仅通过使用.innerHTML方法来存储数据,但它只是删除日期并插入我的文本。

  var cell = document.getElementsByTagName('td');
  for (var i=0; i<cell.length; i++) {
     cell[i].onclick = function() {
        var data = this.getAttribute('data-cell');
        editEvent = document.getElementById('addevent');//The pop-up window
        editEvent.style.cssText ='display: block;';
        this.style.position = 'relative';

        this.innerHTML = " "//??? I want the inputs data to be inserted here

I also have those input data stored in localStorage. 我也将那些输入数据存储在localStorage中。 Perhaps, there's an easier way to transfer the data from localStorage into the cell? 也许,有一种更简便的方法可以将数据从localStorage传输到单元中? Because it is exactly what I need there. 因为这正是我在那里需要的。

Please, help! 请帮忙! Thank you in advance! 先感谢您!

You haven't (yet) posted the generated HTML, so I'm guessing you have only one HTML element per day. 您尚未发布生成的HTML,所以我想您每天只有一个HTML元素。 That's why when you use element.innerHTML , you change their whole content. 这就是为什么当您使用element.innerHTML ,会更改它们的全部内容。

Create empty elements ( div or span would do) next to the date elements. 在日期元素旁边创建空元素(可以使用divspan )。

Like so: 像这样:

var date, dateBefore;
for(...) {
    // your existing date element
    dateBefore = date;
    date = document.createElement("div");
    // some code to insert the day into it, like date.innerHTML = "26";

    // a placeholder for your input
    datePlaceholder = document.createElement("div");

    // add the placeholder element to the DOM
    document.body.insertBefore(datePlaceholder, dateBefore);

    // insert your empty placeholder right before it, see also http://stackoverflow.com/questions/4793604/how-to-do-insert-after-in-javascript-without-using-a-library
    document.body.insertBefore(date, datePlaceholder);
}

You may want to to put both the date and datePlaceholder in the same div. 您可能需要将datedatePlaceholder放在同一div中。

Edit: using this example, you would have to change your selector (td -> div) and make sure the placeholders are big enough so they "catch" the clicks. 编辑:使用此示例,您将必须更改选择器(td-> div),并确保占位符足够大,以便它们“捕获”点击。 For instance, add a .placeholder class and make sure to give them a size in CSS: 例如,添加一个.placeholder类,并确保在CSS中为其设置大小:

.placeholder {
    height: 100%;
    width: 100%;
    /* debug border to make sure it takes the whole cell */
    border: 1px red solid;
}

You can use HTMLElement.dataset to read/write custom data attributes. 您可以使用HTMLElement.dataset读取/写入自定义数据属性。 An example app is HERE . 一个示例应用程序是HERE You can also work with localStorage directly, than the unique ID would be date. 您也可以直接使用localStorage,因为唯一的ID是日期。

In order to show text/data into calendar cell, you need a placeholder. 为了将文本/数据显示到日历单元中,您需要一个占位符。 In my example each table cell is in this format: 在我的示例中,每个表格单元格都采用以下格式:

<td>
    <div data-date="6-1-2015" class="box">
        <span class="day">6</span>
        <span class="text"></span> <!-- this is a placeholder -->
    </div>
</td>

This way it is easy to find clickable cells: 这样,很容易找到可点击的单元格:

// Select clickable cells
var cells = document.querySelectorAll('.box');

than to bind click events (to open modal window): 而不是绑定点击事件(以打开模式窗口):

// Bind clickable cells
for (var i = 0; i < cells.length; i++) {
    cells[i].addEventListener('click', function(e) {
        var thisCell = this;
        var thisDate = thisCell.dataset.date;
        var thisEvent = thisCell.dataset.event;
        var thisParticipants = thisCell.dataset.participants;
        var thisDescription = thisCell.dataset.description;
        date.innerHTML = thisDate;
        event.value = thisEvent || '';
        participants.value = thisParticipants || '';
        description.value = thisDescription || '';
        modal.style.display = 'block';
    });
};

Saving data is piece of cake: 保存数据是小菜一碟:

// Bind save button
submit.addEventListener('click', function(e) {
    var thisDate = date.innerHTML;
    var thisEvent = event.value;
    var thisParticipants = participants.value;
    var thisDescription = description.value;
    var thisCell = document.querySelector('[data-date="' + thisDate + '"]');
    // Assign data to table cell
    thisCell.dataset.event = thisEvent;
    thisCell.dataset.participants = thisParticipants;
    thisCell.dataset.description = thisDescription;
    // Show on screen
    var textField = thisCell.querySelector('.text'); //-- This is our container
    textField.innerHTML = '\
        <span class="eve">' + thisEvent + '</span>\
        <span class="par">' + thisParticipants + '</span>\
        <span class="des">' + thisDescription + '</span>\
    ';
    modal.style.display = 'none';
});

and after filling data, table cell looks like this: 填充数据后,表格单元格如下所示:

<td>
    <div data-date="6-1-2015" class="box" data-event="Event Title" data-participants="John, Robert" data-description="Football">
        <span class="day">6</span>
        <span class="text">
            <span class="eve">Event Title</span>
            <span class="par">John, Robert</span>
            <span class="des">Football</span>
        </span>
    </div>
</td>

For more info, please take a look at Fiddle, code is huge to paste it here. 有关更多信息,请查看Fiddle,将代码粘贴到此处的代码很大。

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

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