Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取宽度的绑定处理程序_Javascript_Html_Knockout.js - Fatal编程技术网

Javascript 获取宽度的绑定处理程序

Javascript 获取宽度的绑定处理程序,javascript,html,knockout.js,Javascript,Html,Knockout.js,我更新了小提琴。 在这本书中,我有三个div。在中间div中有循环的文本。 当div变小的时候,我想把它拿出来放在另一个可观察的array中,它绑定到一个类似这样的选择类型 <div data-bind="collection:{items:itemsThatFit,itemsDontFit:itemsDontFit"> <span data-bind="text:$data"></span> </div> <select data

我更新了小提琴。 在这本书中,我有三个div。在中间div中有循环的文本。 当div变小的时候,我想把它拿出来放在另一个可观察的array中,它绑定到一个类似这样的选择类型

<div data-bind="collection:{items:itemsThatFit,itemsDontFit:itemsDontFit">
   <span data-bind="text:$data"></span>
</div>


<select data-bind="visible:itemsDontFit.length>0, options: itemsDontFit"></select>
你很接近

html是

<div data-bind="foreach: developers"> 
  <span data-bind="textWidth:$data, sourceArray:$root.developers,
                   overflowArray:$root.overflow"></span> 
</div>
更新后的小提琴位于

您很接近

html是

<div data-bind="foreach: developers"> 
  <span data-bind="textWidth:$data, sourceArray:$root.developers,
                   overflowArray:$root.overflow"></span> 
</div>

更新的小提琴位于

您试过什么吗?没有与您的
itemsFit
itemsontfit
相关的代码。首先,我将检索ko绑定中元素的
宽度
。您尝试过什么吗?没有与您的
itemsFit
itemsontfit
相关的代码。首先,我将检索ko绑定中元素的
宽度。
ko.bindingHandlers['textWidth'] = {
  'update': function (element, valueAccessor, allBindingsAccessor) {
    var allBindings = allBindingsAccessor();
    // use ko text binding to do actual output.
    ko.bindingHandlers['text'].update(element, valueAccessor);
    var value = valueAccessor();
    ko.utils.unwrapObservable(value);
    var $parentEement = $(element).parent();

    var childSum = 0;
    $parentEement.children().each(function () {
      childSum += $(this).width();
    });

    if (childSum > $($parentEement[0]).width()) {
      var source = allBindings.sourceArray;
      var overflow = allBindings.overflowArray;
      source.remove(value);
      overflow.push(value);
      $(element).text('');
    }
  }
};