使用 Angularjs 和 Dragula 在特定的 Json 条目上放置元素?
Dropping Element on specific Json entries with Angularjs and Dragula?
我正在学习Angularjs和Dragular。
我有一个数据集,在删除时我必须将数据插入到另一个容器的数据集值中。例如:
这是数据集
$scope.items1 = [
{
'name' : 'x',
'age' : '4'
},
{
'name' : 'y',
'age' : '5'
},
{
'name' : 'z',
'age' : '6'
}
];
我想将 items1
的每个元素放入 items2
值中。 items2
看起来像:
$scope.items2 = [
{
'key' : 'aaa',
'value' :""
},
{
'key' : 'bbb',
'value' : ""
},
{
'key' : 'ccc',
'value' : ""
},
{
'key' : 'ddd',
'value' : ""
}
];
一旦删除,items2
应该看起来像:
{
name:'aaa',
value: [{
'key' : 'aaa',
'value' :""
}]
}
每一个。我该怎么做?
以下是可能的解决方案之一:
angular.module('myApp', ['dragularModule']).controller('MyCtrl', function(dragularService, $element, $scope, $timeout) {
$scope.items1 = [{
name: 'x',
age: '4'
}, {
name: 'y',
age: '5'
}, {
name: 'z',
age: '6'
}];
$scope.items2 = [
{
'key': 'info',
'value': []
}, {
'key': 'info',
'value': []
}, {
'key': 'info',
'value': []
},
];
// timeout due to document not ready, jsfiddle settings issue?
$timeout(function() {
dragularService('#items1');
dragularService('.items2');
});
});
div {
border: 1px solid blue;
min-width: 200px;
padding: 10px
}
.can-be-empty {
min-height: 10px;
min-width: 200px;
}
<link href="https://rawgit.com/luckylooke/dragular/master/dist/dragular.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://rawgit.com/luckylooke/dragular/master/dist/dragular.js"></script>
<div class='app' ng-app="myApp" ng-controller="MyCtrl">
<h2>items1</h2>
<div id="items1" class="can-be-empty">
<div ng-repeat="item in items1">item {{item.name + ' age:' + item.age}}</div>
</div>
<h2>items2</h2>
<div ng-repeat="container in items2" class="items2">
<div class="can-be-empty" ng-repeat="item in container.value">
<div>item {{item.name + ' age:' + item.age}}</div>
</div>
</div>
</div>
Link 到代码笔:https://codepen.io/luckylooke/pen/LyoWzR
我正在学习Angularjs和Dragular。
我有一个数据集,在删除时我必须将数据插入到另一个容器的数据集值中。例如:
这是数据集
$scope.items1 = [
{
'name' : 'x',
'age' : '4'
},
{
'name' : 'y',
'age' : '5'
},
{
'name' : 'z',
'age' : '6'
}
];
我想将 items1
的每个元素放入 items2
值中。 items2
看起来像:
$scope.items2 = [
{
'key' : 'aaa',
'value' :""
},
{
'key' : 'bbb',
'value' : ""
},
{
'key' : 'ccc',
'value' : ""
},
{
'key' : 'ddd',
'value' : ""
}
];
一旦删除,items2
应该看起来像:
{
name:'aaa',
value: [{
'key' : 'aaa',
'value' :""
}]
}
每一个。我该怎么做?
以下是可能的解决方案之一:
angular.module('myApp', ['dragularModule']).controller('MyCtrl', function(dragularService, $element, $scope, $timeout) {
$scope.items1 = [{
name: 'x',
age: '4'
}, {
name: 'y',
age: '5'
}, {
name: 'z',
age: '6'
}];
$scope.items2 = [
{
'key': 'info',
'value': []
}, {
'key': 'info',
'value': []
}, {
'key': 'info',
'value': []
},
];
// timeout due to document not ready, jsfiddle settings issue?
$timeout(function() {
dragularService('#items1');
dragularService('.items2');
});
});
div {
border: 1px solid blue;
min-width: 200px;
padding: 10px
}
.can-be-empty {
min-height: 10px;
min-width: 200px;
}
<link href="https://rawgit.com/luckylooke/dragular/master/dist/dragular.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://rawgit.com/luckylooke/dragular/master/dist/dragular.js"></script>
<div class='app' ng-app="myApp" ng-controller="MyCtrl">
<h2>items1</h2>
<div id="items1" class="can-be-empty">
<div ng-repeat="item in items1">item {{item.name + ' age:' + item.age}}</div>
</div>
<h2>items2</h2>
<div ng-repeat="container in items2" class="items2">
<div class="can-be-empty" ng-repeat="item in container.value">
<div>item {{item.name + ' age:' + item.age}}</div>
</div>
</div>
</div>
Link 到代码笔:https://codepen.io/luckylooke/pen/LyoWzR