Cie*_*iel 2 kendo-ui kendo-mvvm
我试图使用Kendo UI MVVM框架和Kendo UI拖放机制; 但我很难找到如何从draggable对象中删除数据.
我的代码是这样的......
var viewModel = kendo.observable {
Cart : [],
Items : [
{
Id : "item/10",
Name: "CD ROM"
},
{
Id : "item/11",
Name: "DVD ROM"
}
};
Run Code Online (Sandbox Code Playgroud)
那么我有一个粗略的模板绑定...
<script id="products-template" type="text/x-kendo-template">
<li class="draggable">
<div data-bind="text: Name"></div>
</li>
</script>
Run Code Online (Sandbox Code Playgroud)
然后在列表中调用它...
<div id="shopping-items-available">
<ul data-template="products-template" data-bind="source: Items">
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
然后有一个标准的"放弃目标"(取自剑道文档)
<div id="droptarget">Start dragging.</div>
Run Code Online (Sandbox Code Playgroud)
使用以下CSS
#droptarget {
border: 1px solid #959595;
height: 198px;
width: 300px;
font-size: 36px;
border-radius: 37px;
text-align: center;
line-height: 198px;
color: #a1a1a1;
text-shadow: 0 1px 1px #fff;
margin: 0 0 30px 220px;
cursor: default;
background: #dddddd;
background: -moz-linear-gradient(top, #dddddd 0%, #c1c1c1 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#dddddd), color-stop(100%,#c1c1c1));
background: -webkit-linear-gradient(top, #dddddd 0%,#c1c1c1 100%);
background: -o-linear-gradient(top, #dddddd 0%,#c1c1c1 100%);
background: -ms-linear-gradient(top, #dddddd 0%,#c1c1c1 100%);
background: linear-gradient(top, #dddddd 0%,#c1c1c1 100%);
}
Run Code Online (Sandbox Code Playgroud)
现在在javascript中,我把shopping-items-availablediv变成了一个draggable.
$("body").kendoDraggable({
hint: function (target) {
return $(target).clone().addClass("draggable");
},
filter: ".draggable"
});
Run Code Online (Sandbox Code Playgroud)
最后,我初始化放置目标.
$("#droptarget").kendoDropTarget({
drop: droptargetOnDrop
});
Run Code Online (Sandbox Code Playgroud)
但在我的代码中,我似乎无法获得有关被删除项目的实际数据.
function droptargetOnDrop(e) {
console.log(e);
$("#droptarget").text("You did great!");
$(".draggable").removeClass("hollow");
}
Run Code Online (Sandbox Code Playgroud)
被删除项目的数据由droptargetOnDropas 接收e.draggable.currentTarget.
如果要将项目移动到目标区域,则应执行以下操作:
$("#droptarget").append(e.draggable.currentTarget);
Run Code Online (Sandbox Code Playgroud)
注意执行此操作,您将移动该项目.如果要附加副本,则应附加节点的克隆:
$("#droptarget").append($(e.draggable.currentTarget).clone());
Run Code Online (Sandbox Code Playgroud)
编辑:为了在不使用kendoDataSource的情况下获取数据项,我建议将模板更改为:
<script id="products-template" type="text/x-kendo-template">
<li class="draggable" data-id="${Id}">
<div data-bind="text: Name"></div>
</li>
</script>
Run Code Online (Sandbox Code Playgroud)
这会Id在被拖动的DOM中保存(或允许您查找元素的任何信息).然后在处理程序中我们检索Id并搜索与之对应的项目Id.
function droptargetOnDrop(e) {
var dom = e.draggable.currentTarget;
var id = $(dom).data("id");
var items = viewModel.Items;
for (var i = 0; i < items.length; i++) {
if (items[i].Id == id) {
alert("Found : " + JSON.stringify(items[i]));
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑如果您决定使用kendoListView,则应将模板定义为:
<script id="products-template" type="text/x-kendo-template">
<li class="draggable">
<div>${Name}</div>
</li>
</script>
Run Code Online (Sandbox Code Playgroud)
该列表的HTML容器将是:
然后将ListView初始化为:
var list = $("#shopping-items-available > ul").kendoListView({
template : $("#products-template").html(),
dataSource: viewModel.Items
}).data("kendoListView");
Run Code Online (Sandbox Code Playgroud)
最后droptargetOnDrop功能:
function droptargetOnDrop(e) {
var dom = e.draggable.currentTarget;
var item = list.dataSource.getByUid(dom.data("uid"));
alert("Found : " + JSON.stringify(item));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2657 次 |
| 最近记录: |