用于敲除数据绑定列表的 Bootstrap 弹出窗口

Dan*_*ana 1 javascript jquery twitter-bootstrap knockout.js

我已经使用淘汰赛和引导弹出窗口工作了几天,我有一个数据表,它使用淘汰赛数据绑定显示一些信息。

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

有没有办法为每个valuedata-bind显示不同的弹出窗口tehlist

更新

我已经改为:

<button type="button" id="tehValue" class="btn btn-default" data-bind="text: $data.Value, click: getinfo" style="border:none"></button></td>
Run Code Online (Sandbox Code Playgroud)

和功能:

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

我仍然只在点击时获得第一个值的弹出窗口。

有没有办法在不使用 Id 的情况下显示按钮的弹出框,而只使用 getinfo 函数进行 onclick?

Bry*_*ger 5

这是下面解决方案的小提琴。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);
  }
};
Run Code Online (Sandbox Code Playgroud)

并且您的函数成为传入的 ko 计算值。

this.getInfo = ko.computed(function() {
    return this.name() + " " + Math.random()
  }, this);
Run Code Online (Sandbox Code Playgroud)

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

  <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-bind="popover: getInfo, popoverTitle: category">?</button>
Run Code Online (Sandbox Code Playgroud)

下面是整个解决方案,或者您可以单击上面的小提琴链接。

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'));

});
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)