【问题标题】:Bootstrap popover for a knockout data-bind list用于淘汰数据绑定列表的引导弹出窗口
【发布时间】:2017-03-20 20:48:00
【问题描述】:

几天来,我一直在使用淘汰赛和引导弹出框,我有一个数据表,它使用淘汰赛数据绑定显示一些信息。

<tbody data-bind="foreach: tehlist()">
  <tr>
     <td data-bind="text: $data.Category"></td>
     <td data-bind="text: $data.Name"></td>
     <td><button type="button" id="tehValue" class="btn btn-default" data-bind="text: $data.Value" style="border:none" onclick="getinfo()"></button></td>
  </tr>
</tbody>

当点击 tehValue 时,它​​会触发一个显示随机消息的函数:

function getinfo(veh, item) {
    $('#tehValue').popover({
        content: 'Dana' + Math.random(),
        html: true
    });  
}

问题是它只为第一个点击的值显示弹出窗口,而不是其他的。

有没有办法为数据绑定tehlist 的每个value 显示不同的弹出窗口?

更新

我已经改成:

<button type="button" id="tehValue" class="btn btn-default" data-bind="text: $data.Value, click: getinfo" style="border:none"></button></td>

以及功能:

getinfo = function (item, event) {
        $('#tehValue').popover({
            content: 'Dana' + Math.random(),
            html: true
        });
    }

我仍然只为点击的第一个值获得弹出框。

有没有一种方法可以在不使用 Id 而只使用 getinfo 函数的 onclick 的情况下显示按钮的弹出框?

【问题讨论】:

  • 您是否查看了click 绑定上的(stackoverflow)文档? stackoverflow.com/documentation/knockout.js/7101/…
  • 我在检查您的评论后更新了我的问题,但我仍然有同样的问题......也许与 id 有关:(
  • 当然,你不能多次使用同一个id。由于foreach 使用下面的html 作为模板,它会给每个列表项相同的id。查看自定义绑定(处理 DOM 的首选方式)。如果你只是想要一个快速而肮脏的修复,你可以使用event.target(不推荐)。
  • 但是,有没有一种方法可以在不使用 Id 的情况下显示按钮的弹出框,而只使用 getinfo 函数在点击时显示?
  • 如果您坚持使用 viewmodel 的 getinfo 方法来显示弹出窗口(这并不是真正的淘汰方式),您可以使用:$(event.target).popover({ /* options */ })

标签: javascript jquery twitter-bootstrap knockout.js


【解决方案1】:

这是下面解决方案的小提琴。 http://jsfiddle.net/bdellinger/LkqTU/32342/

如何为你的弹出框做一个自定义绑定。

ko.bindingHandlers.popover = {
  init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
    ko.bindingHandlers.value.init(element, valueAccessor, allBindings);
    var source = allBindings.get('popoverTitle');
    var sourceUnwrapped = ko.unwrap(source);
    $(element).popover({
      trigger: 'focus',
      content: valueAccessor(),
      title: sourceUnwrapped
    });
  },
  update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
    var value = valueAccessor();
    ko.bindingHandlers.value.update(element, valueAccessor);
  }
};

你的函数变成了一个传入的 ko 计算值。

this.getInfo = ko.computed(function() {
    return this.name() + " " + Math.random()
  }, this);

然后像这样在 html 中调用它。

  <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-bind="popover: getInfo, popoverTitle: category">?</button>

下面是完整的解决方案,或者你可以直接点击上面的小提琴链接。

javascript。

ko.bindingHandlers.popover = {
  init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
    ko.bindingHandlers.value.init(element, valueAccessor, allBindings);
    var source = allBindings.get('popoverTitle');
    var sourceUnwrapped = ko.unwrap(source);
    $(element).popover({
      trigger: 'focus',
      content: valueAccessor(),
      title: sourceUnwrapped
    });
  },
  update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
    var value = valueAccessor();
    ko.bindingHandlers.value.update(element, valueAccessor);
  }
};


function teh(category, name) {
  var self = this;
  this.category = ko.observable(category);
  this.name = ko.observable(name);
  this.getInfo = ko.computed(function() {
    return this.name() + " " + Math.random()
  }, this);
}

function model() {
  var self = this;
  this.tehlist = ko.observableArray('');

}

var mymodel = new model();

$(document).ready(function() {
  ko.applyBindings(mymodel);
  mymodel.tehlist.push(new teh('car', 'honda'));
  mymodel.tehlist.push(new teh('dog', 'pug'));
  mymodel.tehlist.push(new teh('language', 'spanish'));
  mymodel.tehlist.push(new teh('pc', 'dell'));

});

html

<table class="table table-striped">
  <thead>
    <tr>
      <th>Category</th>
      <th>Name</th>
      <th>Get Info</th>
    </tr>
  </thead>
  <tbody data-bind="foreach: tehlist">
    <tr>
      <td>
        <label data-bind="text:category" </td>
          <td>
            <label data-bind="text:name" </td>
              <td>
                <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-bind="popover: getInfo, popoverTitle: category">?</button>
              </td>
    </tr>
  </tbody>
</table>

<div>

【讨论】:

  • 就是这样!非常感谢! :)
  • 嘿@bryan 有什么方法可以在单击按钮时关闭此弹出窗口。我的意思是它可以通过外部点击来工作,但是我们有什么方法可以通过点击按钮来关闭它。
  • @Bharat 我很抱歉,但我已经一年多没有使用淘汰赛了,所以我很生疏。也许您可以将其作为一个不同的问题提出并在其中引用此问题。
猜你喜欢
  • 2014-06-04
  • 2017-03-02
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多