简体   繁体   English

Kendo ListView:通过选择所需项目切换到编辑模板

[英]Kendo ListView: switching to the edit template by selecting the desired item

I have this simple example illustrating the problem: http://dojo.telerik.com/AROMAZ 我有一个说明问题的简单示例: http : //dojo.telerik.com/AROMAZ

I want to select (click) an item to make it switch to its 'edit' template. 我想选择(单击)一个项目以使其切换到“编辑”模板。 It works fine only if I click on the edited item's 'cancel' icon before selecting a new item. 仅当我在选择新项目之前单击编辑项目的“取消”图标时,它才能正常工作。

If I select a new item without manually deselecting the previous one, it stops working. 如果我选择一个新项目而不手动取消选择上一个项目,则它将停止工作。

I don't what to rely nor have the 'cancel' button. 我没有什么可依靠的,也没有“取消”按钮。

It should be easy.. Click the item you want to edit (to switch to its 'edit' template). 它应该很容易。单击您要编辑的项目(切换到其“编辑”模板)。 Selecting one should deselect the previously selected item. 选择一个应取消选择先前选择的项目。 ie edit one at at time. 即一次编辑一个。

I think the problem is I couldn't find a way to auto-select / un-edit an item (if there is any selected) before manually editing another one. 我认为问题是在手动编辑另一个项目之前,我找不到自动选择/取消编辑项目(如果有选择项)的方法。

EDIT 1: 编辑1:

Placing this.cancel(); 放置this.cancel(); before this.edit(selected) doesn't work as expected. this.edit(selected)之前无法正常工作。 Notice this code was commented out in the original dojo example. 注意,此代码在原始的dojo示例中已被注释掉。

When you select a new item, the previously selected item gets un-edited (this is fine). 当您选择一个新项目时,先前选择的项目将不被编辑(可以)。 However, the newly selected item doesn't get edited (undesired behavior), and an exception is thrown (undesired behavior). 但是,新选择的项目不会被编辑(不良行为),并且会引发异常(不良行为)。

The exception is: 例外是:

Uncaught TypeError: Cannot read property 'uid' of undefined
    at init.edit (kendo.all.js:53910)
    at init.change (VM1332 result:42)
    at init.trigger (kendo.all.js:124)
    at init.change (kendo.all.js:53707)
    at init.trigger (kendo.all.js:124)
    at init._notify (kendo.all.js:25836)
    at init.value (kendo.all.js:25811)
    at init._tap (kendo.all.js:25725)
    at init.d (jquery-1.12.4.min.js:2)
    at init.trigger (kendo.all.js:124)

The addition of this.cancel(); this.cancel(); is illustrated in this modified dojo: http://dojo.telerik.com/AROMAZ/7 在此修改后的dojo中进行了说明: http : //dojo.telerik.com/AROMAZ/7

Note: To see the exception, open the browser's Developer Tools (ie Shift+Ctr+I in Chrome) 注意:要查看异常,请打开浏览器的开发人员工具(即Chrome中的Shift + Ctr + I)

EDIT 2: 编辑2:

Placing this.save(); 放置this.save(); before this.edit(selected) can throw exceptions too. this.edit(selected)也可以引发异常之前。 Example: http://jsfiddle.net/horacioj/mkJTG/417/ 示例: http//jsfiddle.net/horacioj/mkJTG/417/

Instead of cancel try to use: 而不是取消尝试使用:

this.save();

Before 之前

this.edit(selected);

Seems this solutions fully fits your wants. 看来此解决方案完全符合您的需求。

EDIT 编辑

To avoid an exception when you click on the same element while in edit mode: 为避免在编辑模式下单击同一元素时出现异常,请执行以下操作:

$("#listView").kendoListView({
dataSource: dataSource,
template: "<div style='padding: 10px; border: 1px solid red;'>#= Id # </div>",
editTemplate: "<div style='padding: 10px; border: 1px solid red;'>EDITING #= Id # </div>",
selectable: "single",
change: function(e) {
    var index = this.select().index();        
    var dataItem = this.dataSource.at(index);

    if(e.sender.LastIndex != index) {
      this.save();        
      this.edit(this.select());          
    }        

    e.sender.LastIndex = index;
}});

I think I got it working exactly as I want. 我认为我完全可以按照自己的意愿进行操作。 See http://jsfiddle.net/horacioj/t45n0hdj/ 参见http://jsfiddle.net/horacioj/t45n0hdj/

It does a this.cancel(); 它执行this.cancel(); before the this.edit() . this.edit()之前。 If this.select(); 如果this.select(); fails (and therefore the .edit() would throw and exception), then it does the .select() searching the item by index (or id). 失败(因此.edit()会抛出异常),然后它会执行.select()按索引(或ID)搜索项目。 This prevents the exception to happen. 这样可以防止发生异常。

A variable remembering the last item that was edited helps to prevent keeping an item edited if it was already selected (ie clicking the same item will toggle its state instead of keeping it in edit mode). 记住最后一个已编辑项目的变量有助于防止对已被选中的项目进行编辑(例如,单击同一项目将切换其状态,而不是使其处于编辑模式)。

Basically: 基本上:

var lastEditedIndex = -1;

$("#listView").kendoListView({
  ....
  ....
  change: function(e) {
    var index = this.select().index();
    this.cancel();
    var selected = this.select();
    if (selected.length === 1) {
      this.edit(selected);
      lastEditedIndex = index;
    } else {
      selectByIndex(index);
    }
  }


function selectByIndex(index) {
  if (lastEditedIndex === index) return;

  var listView = $('#listView').data('kendoListView');
  var itemWithID = listView.dataSource.at(index);
  listView.select(listView.element.children('[data-uid="' + itemWithID.uid + '"]').first());
  lastEditedIndex = index;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM