【问题标题】:Knockout-Sortable catch complex ObjectsKnockout-Sortable 捕获复杂对象
【发布时间】:2013-02-19 06:51:52
【问题描述】:

我正在为我的页面使用 Knockout.js。我的 ViewModel 包含一个对象数组。每个对象都包含一个子数组。更一般地说,这应该表示一个包含列和列内容的表。

首先我正在处理 foreach:itemArray 以获取列。然后我使用嵌套的 foreach:childs,其中 childs 是列内容的数组。

我的孩子应该可以在列之间拖动。因此我用我找到的 js 替换了嵌套的 foreach

https://github.com/rniemeyer/knockout-sortable

http://jsfiddle.net/rniemeyer/Jr2rE/

再次使用代码 - 第一阶段:

<div id="lanesContainer" data-bind="foreach: lanes">

然后跟随嵌套的foreach(在#lanesContainer 内)

<ul data-bind="sortable: { template: 'laneTemplate', data: childs, afterMove: $root.dropCallback }">

我的项目现在可以拖动了,但不知何故放下失败了。我的调试器在 js 的以下部分中断:

//take destroyed items into consideration
if (!templateOptions.includeDestroyed) {
  targetUnwrapped = targetParent();
  for (i = 0; i < targetIndex; i++) {
    //add one for every destroyed item we find before the targetIndex in the target array
    if (targetUnwrapped[i] && targetUnwrapped[i]._destroy) {
      targetIndex++;
    }
  }
}

它在第三行中断,因为 targetParent 是一个对象,而不是一个函数。我该如何解决这个问题?

【问题讨论】:

    标签: jquery knockout.js jquery-ui-draggable knockout-sortable


    【解决方案1】:

    更改targetUnwrapped = targetParent();

    targetUnwrapped = ko.utils.unwrapObservable(targetParent);
    

    【讨论】:

      【解决方案2】:

      好的,谢谢。 我的 ViewModel 包含一个函数,每次放置都会调用该函数。

      self.dropCallback = function (arg) {
          var item = arg.item;
          var sourceParent = arg.sourceParent();
          var targetParent = arg.targetParent();
          blah(); //console.log("Moved '" + arg.item.name() + "' from " + arg.sourceParent.id + " (index: " + arg.sourceIndex + ") to " + arg.targetParent.id + " (index " + arg.targetIndex + ")");
      };
      

      变量 sourceParent 和 targetParent 现在代表用于 foreach 的数组。我怎样才能“走”一个更高的级别,到达下一个父母?

      【讨论】:

      • 好的,它是 sourceParentNode.parent()
      猜你喜欢
      • 1970-01-01
      • 2017-04-14
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      相关资源
      最近更新 更多