gpt4 book ai didi

javascript - 在更改更新图像位置 json 时使用 Angular

转载 作者:行者123 更新时间:2023-11-27 23:24:31 26 4
gpt4 key购买 nike

我被困在我的项目中,我把希望寄托在你们身上,告诉我如何进一步前进。目前我的代码允许向左或向右拖放图像。这应该定义图像位置。现在我不知道如何创建一个函数来更新我的 json 中的图像位置。让我向您展示我的代码,我将进一步解释

这是我从服务器获得的图像响应。

[{"id_image":0,"thumbImage":"http://lorempixel.com/242/242/?52704","image":"http://lorempixel.com/1024/768/?37489","position":0},
{"id_image":1,"thumbImage":"http://lorempixel.com/242/242/?20352","image":"http://lorempixel.com/1024/768/?89352","position":1},
{"id_image":2,"thumbImage":"http://lorempixel.com/242/242/?27924","image":"http://lorempixel.com/1024/768/?45708","position":2},
{"id_image":3,"thumbImage":"http://lorempixel.com/242/242/?45140","image":"http://lorempixel.com/1024/768/?88511","position":3},
{"id_image":4,"thumbImage":"http://lorempixel.com/242/242/?10979","image":"http://lorempixel.com/1024/768/?98296","position":4}]

这是我的 HTML,它显示并允许向左或向右拖放图像

<form>
{{images}}
<div class="row clearfix">
<div ui:sortable ng:model="images" id="main" style="overflow:auto;">
<div ng:repeat="i in images track by $index" id="editProductImages">
<figure class="d_xs_inline_b">
<div>
<img src="{{i.thumbImage}}" alt="{{product[0].name}}">
</div>
</figure>
</div>
</div>
</div>
</form>

我还添加了一点 JQuery 来使其工作,因为整个模板已经用 JQuery 编写了

//Sort product images
$(function() {
$('#editProductImages').sortable({
connectWith: '#main',
scroll : false
});
});

这就是我的项目目前的样子 Sort images

这是我现在的 Controller 功能

productsFactory.getProduct(alias).then(function(data){
//Fetch product data
$scope.product = data;
//Fetch images
$scope.images = data[0].images;
})
//Sort images function, how??
$scope.updateImagePosition = function(){

}

问题是我不希望用户按下任何提交按钮。我不希望当用户向左或向右移动任何图像时立即发生这种情况。我希望你们能帮助我。如果您需要任何其他信息,请告诉我,我会提供

最佳答案

您可以使用sortableupdate 属性。我已经在这里展示了它是如何工作的。您可以编辑它来更改您拥有的 JSON。正如下面的代码所示,这也适用于 Angular!

var app = angular.module("myapp", []);
app.controller("c", function($scope) {
$("#sortable1").sortable({
connectWith: ".connectedSortable",
change: function(event, ui) {
$(this).attr('data-previndex', ui.item.index());
},
update: function(event, ui) {
var start_pos = $(this).attr('data-previndex');
var index = ui.item.index() + 1;
var text = ui.item.text();

$(this).removeAttr('data-previndex');
$("#msg").append(text + " moved from " + start_pos + " to " + index + "</br>");
}
}).disableSelection();
});
#sortable1,
#sortable2 {
border: 1px solid #eee;
width: 142px;
min-height: 20px;
list-style-type: none;
margin: 0;
padding: 5px 0 0 0;
float: left;
margin-right: 10px;
}
#sortable1 li,
#sortable2 li {
margin: 0 5px 5px 5px;
padding: 5px;
font-size: 1.2em;
width: 120px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">


<body ng-app="myapp" ng-controller="c">

<ul id="sortable1" class="connectedSortable">
<li class="ui-state-default">Item 1</li>
<li class="ui-state-default">Item 2</li>
<li class="ui-state-default">Item 3</li>
<li class="ui-state-default">Item 4</li>
<li class="ui-state-default">Item 5</li>
</ul>
<p id="msg"></p>



</body>

关于javascript - 在更改更新图像位置 json 时使用 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35078433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com