javascript - 使用 Knockout JS 进行数据绑定(bind)

标签 javascript c# html twitter-bootstrap knockout.js

我遇到了无法绑定(bind)数据然后使用 cshtml 显示它的问题。我尝试了不同的方法来制作可观察的数组,我在想,我的主要问题来自于尝试利用我所谓的“有界数据”...以下是我的 cshtml(c#-html) 代码,然后是我的 js 代码。

 <!--*****Unfinished*****-->
                    <td>
                        <label class="element-label">Continuous (Vibratory) Acceleration</label>
                        <select class="form-control device-family-selector" , data-bind="options: changeAuxFlange.availableVForces, optionsText: 'forceName', value: changeAuxFlange.selectedForces, optionCaption: 'Choose a force...'"></select>
                    </td>
                    <td>
                        <input style="width:50px; text-align:right;" , data-bind="text: changeAuxFlange.selectedForces" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label class="element-label">Maximum (Shock) Acceleration</label>
                        <select class="form-control device-family-selector" , data-bind="options: changeAuxFlange.availableSForces, optionsText: 'forceName', value: changeAuxFlange.selectedForces, optionCaption: 'Choose a force...'"></select>
                    </td>
                    <td>
                        <input style="width:50px; text-align:right;" , data-bind="value: changeAuxFlange.selectedForces" />
                    </td>
                    <!--**********-->

查看模型:

"use strict";
function ViewModel()
{
    // it would make more sense with the current setup to make the ViewModel be the Application, but I have set it up like this in case some day it is desired that this tool creates multiple applications in one session
    this.application = ko.observable(new Application('New Application', this));
    this.requestSearchMode = ko.observable(false);
}

function Application(name, parentViewModel)
{....
this.sections =
    {
        gForceSection: initGforceSection(this),
        pumpSection: initPumpSection(this),
        calcLoadsSection: initCalcLoadsSection(this)
    }....
}

    function initGforceSection(application)
{
    var data = ko.observableArray();

    var gForceSection = new Section('G-Forces', data, application);


    var self = this;
    var Force = function (name, value) {
        this.forceName = name;
        this.forceValue = value;
    };
    var vibForce = {
        availableVForces: ko.observableArray([
            { vForce: "Skid steer loader", value: 4 },
            { vForce: "Trencher (rubber tires)", value: 3 },
            { vForce: "Asphalt paver", value: 2 },
            { vForce: "Windrower", value: 2 },
            { vForce: "Aerial lift", value: 1.5 },
            { vForce: "Turf care vehicle", value: 1.5 },
            { vForce: "Vibratory roller", value: 6 }
        ]),
        selectedForces: ko.observable()
    };
    var shockForce = {
        availableSForces: ko.observableArray([
            { sForce: "Skid steer loader", value: 10 },
            { sForce: "Trencher (rubber tires)", value: 8 },
            { sForce: "Asphalt paver", value: 6 },
            { sForce: "Windrower", value: 5 },
            { sForce: "Aerial lift", value: 4 },
            { sForce: "Turf care vehicle", value: 4 },
            { sForce: "Vibratory roller", value: 10 }
        ]),
        selectedForces: ko.observable()
    };


    gForceSection.families = ko.observableArray();
    productData.getPumpFamilies(function (data) {
        gForceSection.families(data);
        addPump(application);
    });

    gForceSection.tbxNumberofPumps = ko.computed(function () { return gForceSection.data().length });

    return gForceSection;
}
//CREATE VIEWMODEL
var viewModel = new ViewModel;

ko.applyBindings(viewModel);
/******/

最佳答案

viewModels 是一系列嵌套对象,这使得引用变得非常复杂。我可以看到您正在尝试逻辑地构造数据,但这使得很难提供帮助。 knockout 有 context用于从绑定(bind) View 模型开始的绑定(bind)。您可以使用 with 绑定(bind)更改元素/部分的上下文。

否则,您必须为 Knockout 提供完整路径,例如data-bind="value: app.gforcesection.someitem.someProperty - 如果路径中的项目未定义,这可能会导致错误。

我删除了很多结构,使其成为一个工作示例来尝试提供帮助: http://jsfiddle.net/Quango/3y9qhnv9/

新的 viewModel 现在是一个“平面”对象,所有属性都直接在其上。我不确定为什么要将输入框绑定(bind)到强制,因此我修改了这些输入框以绑定(bind)到每个输入框的 value 属性。希望这可以帮助您朝着正确的方向前进。

关于javascript - 使用 Knockout JS 进行数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28157533/

相关文章:

javascript - 内联 PHP 停止使用 if 函数和 javascript

javascript - 控制台日志 JSON 结果

javascript - 如何使用 Java 从 Selenium WebDriver 中的不可见下拉元素中选择选项

c# - 我怎样才能在一个类中拥有一个属性来存储图片/图像?

c# - 如何在 Collection<T> 继承类中找到元素索引?

javascript foreach 数组并用逗号和空格分隔并显示为字符串

javascript - 将光标外观更改为字母

c# - 为什么 C# 不允许只读局部变量?

html - 尝试通过正确缩放导航栏品牌偏移来获得更短的导航栏

javascript - 尝试访问本地存储时 javascript/html5 中的 Dom 异常 18