javascript - 如何在使用 Dragtable UI 时动态地将列添加到表格中?

标签 javascript jquery jquery-ui

我需要一个 GUI,允许我通过拖放来移动列。但我也希望能够动态添加/删除表格中的列。

我遇到了Dragtable插件似乎能够满足我的需要。我唯一不喜欢这个 GUI 的是它的文档不是最好的。

无论如何,我添加了自定义 jQuery 代码,它允许我向表中添加列。我遇到的问题是我手动添加的列不可拖动。

如何使手动添加的列也可拖动?

这是我用来简单地将列添加到表中的代码。

$('#test1').click(function()
{
    $( "#testTable1 > thead > tr" ).each(function()
    {
        $(this).append('<th>New Column</th>');
    });

    $( "#testTable1 > tbody > tr" ).each(function()
    {
        $(this).append('<td>New cell in the column</td>');
    });

    // I tried adding this after I add the column, but it did not work. "it had no affect"
    //$('.defaultTable').dragtable();
});

在这里您可以看到我的代码的运行情况

  $(document).ready(function() {

    $('.defaultTable').dragtable();

	$('#test1').click(function()
	{
		$( "#testTable1 > thead > tr" ).each(function()
		{
			$(this).append('<th>New Column</th>');
		});
		
		$( "#testTable1 > tbody > tr" ).each(function()
		{
			$(this).append('<td>New cell in the column</td>');
		});
		
		$('.defaultTable').dragtable();
	});
	
	
  });
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://akottr.github.io/css/akottr.css" rel="stylesheet"/>
<link href="http://akottr.github.io/css/reset.css" rel="stylesheet"/>

<link rel="stylesheet" type="text/css" href="//rawgithub.com/akottr/dragtable/master/dragtable.css" />

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<script src="//rawgithub.com/akottr/dragtable/master/jquery.dragtable.js"></script>

<!-- only for jquery.chili-2.2.js -->
<script src="//code.jquery.com/jquery-migrate-1.1.1.js"></script>
<script type="text/javascript" src="//akottr.github.io/js/jquery.chili-2.2.js"></script>


<div class="sample">
  
  <button type="button" id="test1">Add column</button>

  <div class="demo">
    <h4>demo</h4>
    <div class="demo-content">
      <table class="defaultTable sar-table" id="testTable1">
        <thead>
          <tr>
            <th>TIME</th>
            <th>%user</th>
            <th>%nice</th>
            <th>%system</th>
            <th>%iowait</th>
            <th>%idle</th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td>12:10:01 AM</td><td>28.86</td><td>0.04</td><td>1.65</td><td>0.08</td><td>69.36</td>
          </tr>
          <tr>
            <td>12:20:01 AM</td><td>26.54</td><td>0.00</td><td>1.64</td><td>0.08</td><td>71.74</td>

          </tr>
          <tr>
            <td>12:30:01 AM</td><td>29.73</td><td>0.00</td><td>1.66</td><td>0.09</td><td>68.52</td>
          </tr>
        </tbody>
      </table>

    </div>
  </div>
  
</div>

最佳答案

再次调用dragTable之前只需刷新表格上的数据

$('.defaultTable').removeData().dragtable();

DEMO

关于javascript - 如何在使用 Dragtable UI 时动态地将列添加到表格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960096/

相关文章:

JavaScript/Python 匿名函数与命名函数作用域

javascript - 单击、拖动和调整大小事件混合在一起

javascript - 使用parents(),与Jquery中的$(this)函数最接近

javascript - jQuery 突出显示滚动上的导航链接不起作用

javascript - 获取 URL 到端口号

jquery - 如何设置 jQuery Combobox 的样式使其看起来像普通的选择下拉列表

jquery 空白/灰色显示对话框的页面

javascript - 您可以在没有 html 文件输入的情况下创建 Javascript 文件列表或文件对象吗

javascript - 使用javascript单击页面上的所有按钮

javascript - meteor react 器相当于 react 路由器是什么?