javascript - 使用 Knockout.js 自定义绑定(bind)进行 Bootstrap 选择

标签 javascript jquery html twitter-bootstrap knockout.js

我正在我的网站上创建“添加”或“邀请”选项,我需要首先获取该网站的所有用户,并以预先输入或类似选择选择器选项的方式显示它们,一旦选择了用户,就应该添加该用户到邀请列表。这就像 trello 邀请董事会或组织一样。 请参阅此处 trello:https://trello.com/

在非常基本的步骤中,我尝试使用 knockout 现场教程示例列表和集合示例。 (http://learn.knockoutjs.com/#/?tutorial=collections)

这是我的代码

HTML

<h2>Your seat reservations (<span data-bind="text: seats().length"></span>)</h2>
<button class="btn btn-deffault" data-bind="click: addSeat, enable: seats().length < 5">Reserve another seat</button>
<table>
    <thead><tr>
        <th>Passenger name</th><th>Meal</th><th>Surcharge</th><th></th>
    </tr></thead>
    <!-- Todo: Generate table body -->
    <tbody data-bind="foreach: seats">
        <tr>
            <td><input data-bind="value: name" /></td>
            <td><select class="selectpicker" data-bind="options: $root.availableMeals, value: meal, optionsText: 'mealName'" data-live-search="true"></select></td>
            <td><input data-bind="value: formattedPrice"></td>
            <td><a href="#" data-bind="click: $root.removeSeat">Remove</a></td>
        </tr>
    </tbody>
</table>
<h3 data-bind="visible: totalSurcharge() > 0">
    Total surcharge: $<span data-bind="text: totalSurcharge().toFixed(2)"></span>
</h3>

这是 knockout 码

//selectpicker的自定义绑定(bind)

ko.bindingHandlers.selectPicker = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            if ($(element).is('select')) {
                if (ko.isObservable(valueAccessor())) {
                    ko.bindingHandlers.value.init(element, valueAccessor, allBindingsAccessor);
                }
                $(element).selectpicker();
            }
        },
        update: function (element, valueAccessor, allBindingsAccessor) {
            if ($(element).is('select')) {
                var selectPickerOptions = allBindingsAccessor().selectPickerOptions;
                if (typeof selectPickerOptions !== 'undefined' && selectPickerOptions !== null) {
                    var options = selectPickerOptions.options,
                        optionsText = selectPickerOptions.optionsText,
                        optionsValue = selectPickerOptions.optionsValue,
                        optionsCaption = selectPickerOptions.optionsCaption;
                    if (ko.utils.unwrapObservable(options).length > 0) {
                        ko.bindingHandlers.options.update(element, options, ko.observable({ optionsText: optionsText, optionsValue: optionsValue, optionsCaption: optionsCaption }));
                    }
                }
                if (ko.isObservable(valueAccessor())) {
                    ko.bindingHandlers.value.update(element, valueAccessor);
                }
                $(element).selectpicker('refresh');
            }
        }
    };

function AllUsers(data){
    var self = this;
    self.name = name;
    self.meal = ko.observable(initialMeal);


    self.formattedPrice = ko.computed(function() {
        var price = self.meal().price;
        return price ? "$" + price.toFixed(2) : "None";        
    });

    self.formatPrice = ko.observable(self.formattedPrice());

    self.about_me = ko.observable(data.about_me);
    self.email = ko.observable(data.email);
    self.uname = ko.observable(data.uname);
    self.uuid = ko.observable(data.uuid);
    self.everyuser = [
     {aboutMe: self.about_me(), email: self.email(), name: self.uname, id: self.uuid()}
    ];

}

function PostViewModel() {
    var self = this;

    self.allusers= ko.observableArray([]);

    self.gert = [
        { mealName: "Standard (sandwich)", price: 0 },
        { mealName: "Premium (lobster)", price: 34.95 },
        { mealName: "Ultimate (whole zebra)", price: 290 }
    ];    



    $.getJSON('/users', function (json) {
        var t = $.map(json.users, function(item) {
            console.log("Something",item);

            return new AllUsers(item);
        });
        self.allusers(t);
    });


}

ko.applyBindings(new PostViewModel());

但是在 HTML 代码中使用类 selectpicker 时,代码无法正常工作并且不显示选项,并且如果不使用 selectpicker,则出现不应该出现的简单下拉菜单。

<td><select class="selectpicker" data-bind="options: $root.availableMeals, value: meal, optionsText: 'mealName'" data-live-search="true"></select></td>

任何了解 Knockout.js 的人请帮忙

最佳答案

看来您正在使用自定义绑定(bind)的名称作为 <select> 的 CSS 类。 。为了正确应用绑定(bind),您需要在 data-bind 中执行此操作。属性。

<select data-bind="selectpicker: meal"></select>

knockout documentation对此也很有帮助。

关于javascript - 使用 Knockout.js 自定义绑定(bind)进行 Bootstrap 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185757/

相关文章:

javascript - 如果存在则删除类并使用 jquery 添加新的类

javascript - 等待 Facebook 像素加载

密码的 JavaScript 正则表达式至少包含 6 个字符,1 个数字,1 个字母,任何特殊字符

css - 在 CSS HTML 中对齐翻转卡片

javascript - 如何在谷歌应用程序脚本中的多个 html 文件上设置默认主页并将其发布为 WebApp

javascript - 本地存在字体时忽略谷歌字体加载

javascript - 如何在 IE 中将字体图标精确地居中对齐

javascript - JQuery 同时显示/隐藏列表一中的图像

javascript - Ondblclick 和 Dblclick 之间的区别

javascript - 无法读取 null 的属性 'style' - 在 Chrome 的 Javascript 控制台中