javascript - 内联编辑数据表 jquery

标签 javascript jquery html css codeigniter

您好,我一直在尝试学习有关内联编辑的教程 http://www.vandelaydesign.com/inline-edit-with-ajax/ .我想将它应用到我的数据表中。当用户单击编辑按钮时,其行内的相应列将成为输入,但我不知道如何将其转换为这种方式。在本教程中,它在 jquery 中使用 .prev 来获取 span 的值,但我不知道如何在我的情况下执行此操作。这是我的 table 的样子:

enter image description here

单击编辑按钮时,类别名称和描述应成为输入。我该怎么做?

<tbody>
    <?php foreach ($categories as $category) { ?>
        <tr>
            <td> <span id="<?= $category->category_id ?>" class="datainfo"> <?= $category->category_name ?>  </span> </td>
            <td>
                <?= $category->category_desc ?>
            </td>
            <td>
                <?= $category->created_on ?>
            </td>
            <td>
                <?= $category->updated_on ?>
            </td>
            <td>
                <?= $category->status ?>
            </td>
            <td>
                <button type="button" name="edit_cat" class="btn btn-light btn-sm edit_cat">
                    <!-- data-id="<?= $category->category_id ?>" -->
                    <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                </button>
            </td>
        </tr>
        <?php } ?>
</tbody>

Javascript:

 $(document).on('click', '.edit_cat', function() {
      console.log('edit category');
      // console.log( $(this).data('id') );
      var dataset = $(this).prev(".datainfo");
      console.log(dataset);
      var theid   = dataset.attr("id");
      console.log(theid);
      var newid   = theid+"-form";
      var currval = dataset.text();

      dataset.empty();

       $('<input type="text" name="'+newid+'" id="'+newid+'" value="'+currval+'" class="hlite">').appendTo(dataset);

    });

在解决这个问题后,我还需要帮助进行 future 的校对,我将如何保存它,也许获取 span id 并从那里开始?

最佳答案

数据表内置了内联编辑功能。

它作为自己的扩展安装

Inline editing in Editor is activated through the use of the inline() API method. Simply call the method and pass in the cell you want to edit as the first parameter.

Take a closer look here - https://editor.datatables.net/examples/inline-editing/simple

关于javascript - 内联编辑数据表 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47686466/

相关文章:

html - 如何使网页中的元素不被打印?

html - 更流畅的背景图像动画(防止闪烁)

javascript - 下划线.js : findWhere with nested property value

javascript - 由于某种原因,clearTimeout 不起作用

javascript - 在 ReactJS 中添加指向 Font Awesome 图标的链接

javascript - 如何使用 JS 编辑 CSS 变量?

javascript - 更改页面上使用的每个实例的 jquery 变量

javascript - 如何在不跳转文档的情况下更新 window.location.hash?

javascript - 命名约定的 Web 标准

javascript - 使用 Kendo UI 数据源的授权 header 拦截器