Перетащите tr таблицы 1 на tr таблицы 2 - PullRequest
0 голосов
/ 28 декабря 2018

Я работаю над сопоставлением функций между двумя таблицами и не могу выполнить перетаскивание одной строки таблицы в строку другой таблицы.

Прежде всего, я хочу знать, возможно ли ее использованиетаблицы?

Я разрабатываю в угловых 1,5

Фрагмент кода:

<table class="table table-hover header-fixed">
                <thead>
                    <th>Part Num</th>
                    <th>Line Abbrev</th>
                    <th>Desc</th>
                    <th>Qty</th>
                    <th></th>
                </thead>
                <tbody>
                    <tr ng-if="$ctrl.ppseList.length > 0"
                        ng-repeat="p in $ctrl.ppseList"
                        ng-class="{'active': ($index == $ctrl.pair.secondary)}"
                        ng-click="click($index, false)" 
                        draggable="true"
                        ng-dragstart="onDragStart($event, $index)"
                        ng-dragend="onDragEnd($event)">
                        <td>{{p.PartNum}}</td>
                        <td>{{p.LineAbbrev}}</td>
                        <td>{{p.Desc}}</td>
                        <td>{{p.ReqQty}}</td>
                        <td>
                            <button type="button" name="button"
                                class="btn btn-xs btn-success"
                                ng-click="addCartItemToUncategorizedList(p, $index);"><i class="fa fa-plus"></i></button>
                            <button type="button" name="button"
                                class="btn btn-xs btn-danger"
                                ng-click="removeCartItemFromCart(p, $index);"><i class="fa fa-minus"></i></button>
                        </td>
                    </tr>
                    <tr ng-if="$ctrl.ppseList.length == 0">
                        <td>Cart is empty <i class="fa fa-star-half-empty"></i></td>
                    </tr>
                </tbody>
            </table>
<table class="table table-hover header-fixed"
                   >
                <thead>
                    <th>Part Num</th>
                    <th>Line Abbrev</th>
                    <th>Desc</th>
                    <th>Qty</th>
                </thead>
                <tbody>
                    <tr ng-if="$ctrl.uncategorizedList.length > 0"
                        ng-repeat="p in $ctrl.uncategorizedList"
                        ng-class="{'active': ($index == $ctrl.pair.primary)}"
                        ng-click="click($index, true)" ng-dragover="onDragOver($event)"
                   ng-dragend="onDragEnd($event)">
                        <td>{{p.Partnum}}</td>
                        <td>{{p.LineAbbrv}}</td>
                        <td>{{p.Desc}}</td>
                        <td>{{p.ReqQty}}</td>
                    </tr>
                    <tr ng-if="$ctrl.uncategorizedList.length == 0">
                        <td colspan="4">No parts to match.</td>
                    </tr>
                </tbody>
            </table>

Javascript:

$scope.onDragStart = function (e, index) {
console.log('Started', e);
e.target.style.opacity = '0.4';
};



$scope.onDragEnd = function (e) {
    console.log('End', e);
  };



$scope.onDrop = function (e) {
    console.log('Drop', e.dataTransfer.getData('Text'));
};

$scope.onDragOver = function (e) {
    console.log('Drag Over', e);
    if (e.preventDefault) {
      e.preventDefault(); // Necessary. Allows us to drop.
    }

    e.dataTransfer.dropEffect = 'move';  // See the section on the DataTransfer object.

  return false;
};

Проблема: Когда я начинаю перетаскивать tr из первой таблицы, вызывается только событие dragstart, и больше ничего не происходит.Буду признателен за любую помощь.

Редактировать:

Код директивы:

App.directive('ngDragenter', function($parse) {


return function(scope, element, attrs) {
    var fn = $parse(attrs.ngDragenter);
    element[0].ondragenter = function(e) {
      scope.$apply(function() {
        // scope.$eval(attrs.ngDragenter);
        fn(scope, {
          $event: event
        });
      });
      e.stopPropagation();
      e.preventDefault();
    };
  }
})
.directive('ngDragleave', function($parse) {
  return function(scope, element, attrs) {
    var fn = $parse(attrs.ngDragleave);
    element[0].ondragleave = function(e) {
      scope.$apply(function() {
        // scope.$eval(attrs.ngDragleave);
        fn(scope, {
          $event: event
        });
      });
      e.stopPropagation();
      e.preventDefault();
    };
  }
})
.directive('ngDragstart', function($parse) {
  return function(scope, element, attrs) {
    var fn = $parse(attrs.ngDragstart);
    element[0].ondragstart = function(e) {
      scope.$apply(function() {
        // scope.$eval(attrs.ngDragleave);
        fn(scope, {
          $event: event
        });
      });
      e.stopPropagation();
      e.preventDefault();
    };
  }
})
.directive('ngDragend', function($parse) {
  return function(scope, element, attrs) {
    var fn = $parse(attrs.ngDragend);
    element[0].ondragend = function(e) {
      scope.$apply(function() {
        // scope.$eval(attrs.ngDragend);
        fn(scope, {
          $event: event
        });
      });
      e.stopPropagation();
      e.preventDefault();
    };
  }
})
.directive('ngDragover', function($parse) {
  return function(scope, element, attrs) {
    var fn = $parse(attrs.ngDragover);
    element[0].ondragover = function(e) {
      scope.$apply(function() {
        // scope.$eval(attrs.ngDragend);
        fn(scope, {
          $event: event
        });
      });
      e.stopPropagation();
      e.preventDefault();
    };
  }
})
.directive('ngDrop', function($parse) {
  return function(scope, element, attrs) {
    var fn = $parse(attrs.ngDrop);
    element[0].ondrop = function(e) {
      scope.$apply(function() {
        // scope.$eval(attrs.ngDragend);
        fn(scope, {
          $event: event
        });
      });
      e.stopPropagation();
      e.preventDefault();
    };
  }
});

1 Ответ

0 голосов
/ 28 декабря 2018

Чтобы достичь ожидаемого результата, используйте опцию перетаскивания HTML5 ниже

  1. Используйте индекс и установите идентификатор строки для первой таблицы
  2. Используйте перетаскиваемый true, чтобы разрешить перетаскивание для первых строк таблицы
  3. Используя идентификатор перетаскиваемого элемента, добавить к второй таблице

codepen- https://codepen.io/nagasai/pen/BvwWwd

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.tab1 = [{
      id: 1,
      name: "a"
    },{
      id: 2,
      name: "b"
    },{
      id: 3,
      name: "c"
    }]
  
  $scope.tab2 = [{
      id: 1,
      name: "aa"
    },{
      id: 2,
      name: "bb"
    },{
      id: 3,
      name: "cc"
    }]
});

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev, el) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  el.appendChild(document.getElementById(data));
}
table tr td {
  border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
  <div>Table 1
  <table>
    <tr ng-repeat="x in tab1" id="row{{$index}}" draggable="true"
ondragstart="drag(event)">
      <td>{{x.id}}</td>
      <td>{{x.name}}</td>
    </tr>
  </table>
    </div>
  <div>Table 2
  <table ondrop="drop(event, this)" ondragover="allowDrop(event)">
    <tr ng-repeat="x in tab2">
      <td>{{x.id}}</td>
      <td>{{x.name}}</td>
    </tr>
  </table>
    </div>

</div>


</body>

Опция 2: Использование библиотеки углового перетаскивания с элементами списка

http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...