如何将列拖放到另一列中 [英] How to drag and drop a column into another

查看:149
本文介绍了如何将列拖放到另一列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:jquery.dataTables.js: https://datatables.net

I am using: jquery.dataTables.js from: https://datatables.net

我正在尝试将列从一个表拖放到另一个表。

I am trying to drag and drop a column from one table to another.

编辑:
所以基本上我想做的是:

so basically what I want to do is:


  • 能够将表2中的名称拖放到名为

  • 后面的名称相同的名称将从表2中消失。

案例2:如果我使用按钮添加新行添加新行

case 2: if I add a new row using the button Add new Row


  • 我需要将表2中的名称拖放到名称中。

  • I need be able to drag a drop the names from table 2 into that column name too.

所以基本上我想做的只是拖放在列中不在行。

so basically I want to do a drag and drop just in the column not in the row.

我不想创建一个新行将名称从1个表移动到另一个表。

I don't want create a new row just move the names from 1 table to another.

编辑2:

1-您可以将表2中的多个值拖放到表#1吗?
否,拖动并且只有1比1才可以下拉。
拖拽只有在用户点击编辑或添加新行后才可以。
所以我将能够将名称drom表2替换为列名称表1

1- Can you drag/drop multiples values from Table #2 to Table #1? no, the drag and drop will be possible just 1 by 1. The drag and drop will be just possible after the user clicks in edit or add a new row. so I will be able to replace names drom table 2 into the column names table 1

2-如果否,则拖动的值将替换价值在哪里?

如果是,它应该如何工作?添加其他值为空的新行?
没有行需要添加,我们只需要替换列名。

如何工作:
所以在点击编辑或添加新行后,我将能够从表2拖动到
表1中的列。

how will works: so after click in edit or add new row i will be able to drag a name from table 2 into column in table 1.

如果可能的话,再多一些资料:
如果选择表2中的行,则此行应更改颜色,显示已选中。在表1中,这个需要删除的collun名称需要更改颜色以显示
用户可以删除。

few more resquests if possible: if select the row in table 2, this row should be change the color, showing was selected. and in the table 1 collun name where this need be dropped need to change the color to show the user can be dropped.

在这里工作的示例:
http://plnkr.co/edit/6sbmBzbXDzm4p6CjaVK0?p=preview

   $(document).ready(function() {
 var dataUrl = 'http://www.json-generator.com/api/json/get/ccTtqmPbkO?indent=2';
var options = [
  { key : 'option 1', value : 1 },
  { key : 'option 2', value : 2 },
  { key : 'option 3', value : 3 }
];

$(document).ready(function() {
  var $table = $('#example');
  var dataTable = null;

  $table.on('mousedown', 'td .fa.fa-minus-square', function(e) {
    dataTable.row($(this).closest("tr")).remove().draw();
  });

  $table.on('mousedown.edit', 'i.fa.fa-pencil-square', function(e) {
    enableRowEdit($(this));
  });

  $table.on('mousedown', 'input', function(e) {
    e.stopPropagation();
  });

  $table.on('mousedown.save', 'i.fa.fa-envelope-o', function(e) {
    updateRow($(this), true); // Pass save button to function.
  });

  $table.on('mousedown', '.select-basic', function(e) {
    e.stopPropagation();
  });

  dataTable = $table.DataTable({
    ajax: dataUrl,
    rowReorder: {
      dataSrc: 'order',
      selector: 'tr'
    },
    columns: [{
      data: 'order'
    }, {
      data: 'name'
    }, {
      data: 'place'
    }, {
      data: 'delete'
    }]
  });

  $table.css('border-bottom', 'none')
        .after($('<div>').addClass('addRow')
          .append($('<button>').attr('id', 'addRow').text('Add New Row')));

  // Add row
  $('#addRow').click(function() {
    var $row = $("#new-row-template").find('tr').clone();
    dataTable.row.add($row).draw();
    // Toggle edit mode upon creation.
    enableRowEdit($table.find('tbody tr:last-child td i.fa.fa-pencil-square'));
  });

  $('#btn-save').on('click', function() {
    updateRows(true); // Update all edited rows
  });

  $('#btn-cancel').on('click', function() {
    updateRows(false); // Revert all edited rows
  });

  function enableRowEdit($editButton) {
    $editButton.removeClass().addClass("fa fa-envelope-o");
    var $row = $editButton.closest("tr").off("mousedown");

    $row.find("td").not(':first').not(':last').each(function(i, el) {
      enableEditText($(this))
    });

    $row.find('td:first').each(function(i, el) {
      enableEditSelect($(this))
    });
  }
  
  function enableEditText($cell) {
    var txt = $cell.text();
    $cell.empty().append($('<input>', {
      type : 'text',
      value : txt
    }).data('original-text', txt));
  }

  function enableEditSelect($cell) {
    var txt = $cell.text();
    $cell.empty().append($('<select>', {
      class : 'select-basic'
    }).append(options.map(function(option) {
      return $('<option>', {
        text  : option.key,
        value : option.value
      })
    })).data('original-value', txt));
}

  function updateRows(commit) {
     $table.find('tbody tr td i.fa.fa-envelope-o').each(function(index, button) {
      updateRow($(button), commit);
    });
  }

  function updateRow($saveButton, commit) {
    $saveButton.removeClass().addClass('fa fa-pencil-square');
    var $row = $saveButton.closest("tr");

    $row.find('td').not(':first').not(':last').each(function(i, el) {
      var $input = $(this).find('input');
      $(this).text(commit ? $input.val() : $input.data('original-text'));
    });

    $row.find('td:first').each(function(i, el) {
      var $input = $(this).find('select');
      $(this).text(commit ? $input.val() : $input.data('original-value'));
    });
  }
});

 $(document).ready(function() {
    	 var url = 'http://www.json-generator.com/api/json/get/bXcKDeAbyq?indent=2';
      table = $('#example2').DataTable({
        ajax: url,
        order: [[ 0, "desc" ]],
        rowReorder: {
          dataSrc: 'place',
          selector: 'tr'
        },
        columns: [ {
          data: 'name'
        }]
      });

    }); 
});

 div.addRow {
      line-height: 45px;
      background-color: #fff;
      padding-left: 10px;
      border-bottom: 1px solid;
      border-top: 1px solid #e5e5e5;
    }

<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="//cdn.rawgit.com/DataTables/RowReorder/ce6d240e/js/dataTables.rowReorder.js"></script>
<link href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="//cdn.datatables.net/rowreorder/1.2.0/css/rowReorder.dataTables.min.css" rel="stylesheet"/>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

 <table id="example" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th>order</th>
      <th>name</th>
      <th>country</th>
      <th>action</th>
    </tr>
  </thead>
</table>

<table id="new-row-template" style="display:none">
  <tbody>
    <tr>
      <td>999</td> <!-- Use a large number or row might be inserted in the middle -->
      <td>__NAME__</td>
      <td>__COUNTRY__</td>
      <td>
        <i class="fa fa-pencil-square" aria-hidden="true"></i>
        <i class="fa fa-minus-square" aria-hidden="true"></i>
      </td>
    </tr>
  </tbody>
</table>
<br>
<div class="pull-right">
  <button type="button" id="btn-cancel" class="btn btn-default" data-dismiss="modal">Cancel</button>
  <button type="button" id="btn-save" class="btn btn-primary" data-dismiss="modal">Save</button>
</div>

<br>
<br>
<h1>
 table 2
</h1><br>
<br>
<table id="example2" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th> name</th>
    </tr>
  </thead>
</table>


<br>
<br>
<h1>
 table 2
</h1><br>
<br>
<table id="example2" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th> name</th>
    </tr>
  </thead>
</table>

推荐答案

我对您的代码进行了一些更改。请试试看看这个分辨率是否适合你:

I've made some changes to your code. Try it out please to see whether this resolution is appropriate for you:

$(document).ready(function() {
  var dataUrl = 'http://www.json-generator.com/api/json/get/ccTtqmPbkO?indent=2';
  var options = [{
      key: 'option 1',
      value: 1
    },
    {
      key: 'option 2',
      value: 2
    },
    {
      key: 'option 3',
      value: 3
    }
  ];

  var rowCache = [];

  function mouseUp(event) {
    var ctrl = $(document.elementsFromPoint(event.clientX, event.clientY)).filter('input.border-highlight');

    if (ctrl.length > 0 && rowCache.length > 0) {
      var el = rowCache[0];
      var data = el.row.data();

      if (data.length > 0) {
        ctrl.val(data[0].name);
        el.row.remove().draw();
      }
    }

    rowCache = [];
    $('#example tr td:nth-child(2) input').removeClass('border-highlight');
  }

  $(document).ready(function() {
    var $table = $('#example');
    var dataTable = null;

    $('body').mouseup(mouseUp);

    $table.on('mousedown', 'td .fa.fa-minus-square', function(e) {
      dataTable.row($(this).closest("tr")).remove().draw();
    });

    $table.on('mousedown.edit', 'i.fa.fa-pencil-square', function(e) {
      enableRowEdit($(this));
    });

    $table.on('mousedown', 'input', function(e) {
      e.stopPropagation();
    });

    $table.on('mousedown.save', 'i.fa.fa-envelope-o', function(e) {
      updateRow($(this), true); // Pass save button to function.
    });

    $table.on('mousedown', '.select-basic', function(e) {
      e.stopPropagation();
    });

    dataTable = $table.DataTable({
      ajax: dataUrl,
      rowReorder: {
        dataSrc: 'order',
        selector: 'tr'
      },
      columns: [{
        data: 'order'
      }, {
        data: 'name'
      }, {
        data: 'place'
      }, {
        data: 'delete'
      }]
    });

    $table.css('border-bottom', 'none')
      .after($('<div>').addClass('addRow')
        .append($('<button>').attr('id', 'addRow').text('Add New Row')));

    // Add row
    $('#addRow').click(function() {
      var $row = $("#new-row-template").find('tr').clone();
      dataTable.row.add($row).draw();
      // Toggle edit mode upon creation.
      enableRowEdit($table.find('tbody tr:last-child td i.fa.fa-pencil-square'));
    });

    $('#btn-save').on('click', function() {
      updateRows(true); // Update all edited rows
    });

    $('#btn-cancel').on('click', function() {
      updateRows(false); // Revert all edited rows
    });

    function enableRowEdit($editButton) {
      $editButton.removeClass().addClass("fa fa-envelope-o");
      var $row = $editButton.closest("tr").off("mousedown");

      $row.find("td").not(':first').not(':last').each(function(i, el) {
        enableEditText($(this))
      });

      $row.find('td:first').each(function(i, el) {
        enableEditSelect($(this))
      });
    }

    function enableEditText($cell) {
      var txt = $cell.text();
      $cell.empty().append($('<input>', {
        type: 'text',
        value: txt
      }).data('original-text', txt));
    }

    function enableEditSelect($cell) {
      var txt = $cell.text();
      $cell.empty().append($('<select>', {
        class: 'select-basic'
      }).append(options.map(function(option) {
        return $('<option>', {
          text: option.key,
          value: option.value
        })
      })).data('original-value', txt));
    }

    function updateRows(commit) {
      $table.find('tbody tr td i.fa.fa-envelope-o').each(function(index, button) {
        updateRow($(button), commit);
      });
    }

    function updateRow($saveButton, commit) {
      $saveButton.removeClass().addClass('fa fa-pencil-square');
      var $row = $saveButton.closest("tr");

      $row.find('td').not(':first').not(':last').each(function(i, el) {
        var $input = $(this).find('input');
        $(this).text(commit ? $input.val() : $input.data('original-text'));
      });

      $row.find('td:first').each(function(i, el) {
        var $input = $(this).find('select');
        $(this).text(commit ? $input.val() : $input.data('original-value'));
      });
    }
  });

  $(document).ready(function() {
    var url = 'http://www.json-generator.com/api/json/get/bXcKDeAbyq?indent=2';
    table = $('#example2').DataTable({
      ajax: url,
      order: [
        [0, "desc"]
      ],
      rowReorder: {
        dataSrc: 'place',
        selector: 'tr'
      },
      columns: [{
        data: 'name'
      }]
    });

    table.on('mousedown', 'tbody tr', function() {
      var $row = $(this);

      var r = table.rows(function(i, data) {
        return data.name == $row.children().first().text();
      });

      if (r[0].length > 0) {
        $row.parents('table').find('tr').removeClass('highlight');
        $row.addClass('highlight');
        $('#example tr td:nth-child(2) input').addClass('border-highlight');
      }

      rowCache.push({
        row: r
      });
    });

  });

});

div.addRow {
  line-height: 45px;
  background-color: #fff;
  padding-left: 10px;
  border-bottom: 1px solid;
  border-top: 1px solid #e5e5e5;
}

tr.highlight td {
  background-color: #D0ECE7 !important;
}

.border-highlight {
  border-color: #D0ECE7 !important;
  border-width: 3px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/rowreorder/1.2.0/js/dataTables.rowReorder.min.js"></script>

<link data-require="datatables@*" data-semver="1.10.12" rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/rowreorder/1.2.0/css/rowReorder.dataTables.min.css" />
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">


<table id="example" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th>order</th>
      <th>name</th>
      <th>country</th>
      <th>action</th>
    </tr>
  </thead>
</table>

<table id="new-row-template" style="display:none">
  <tbody>
    <tr>
      <td>999</td>
      <!-- Use a large number or row might be inserted in the middle -->
      <td>__NAME__</td>
      <td>__COUNTRY__</td>
      <td>
        <i class="fa fa-pencil-square" aria-hidden="true"></i>
        <i class="fa fa-minus-square" aria-hidden="true"></i>
      </td>
    </tr>
  </tbody>
</table>
<br>
<div class="pull-right">
  <button type="button" id="btn-cancel" class="btn btn-default" data-dismiss="modal">Cancel</button>
  <button type="button" id="btn-save" class="btn btn-primary" data-dismiss="modal">Save</button>
</div>

<br>
<br>
<h1>
  table 2
</h1><br>
<br>
<table id="example2" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th> name</th>
    </tr>
  </thead>
</table>

JSFiddle: a href =http://jsfiddle.net/f7debwj2/47/ =nofollow noreferrer> http://jsfiddle.net/f7debwj2/47/

JSFiddle: http://jsfiddle.net/f7debwj2/47/

这篇关于如何将列拖放到另一列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆