使用键值对的 Knockout Bindings 在 foreach 内部

Knockout Bindings using Key value pair Inside foreach

我正在使用 BreezeJs 进行淘汰赛,以便从我的本地数据库检索和存储数据。我在这里面临的问题是 foreach 绑定中的键值绑定。我想要做的是 将 'Name' 属性显示到文本框中,然后从我的数据库中检索相应的 'Id' 属性。

"operator"[successfully accessed from breeze] is a attribute defined in database which have int value 1, hence my text box must display the corresponding value from "operators" [defined in my javascript file] i.e.,"Subtraction" for one record

var operators = [
{id: 0, name: 'addition'},
{id: 1, name: 'subtraction'},
{id: 2, name: 'division'},
{id: 3, name: 'multiplication'}
   ];

我的 HTML 绑定

<div data-bind="foreach: jobs">
      <div>
        <label>FirstNumber :</label>
        <input data-bind="value: first_no" />
    </div>

         <div>
        <label>operator :</label>
         <input type="text" data-bind="text:operators.name, value: $root.operator"/>
    </div>
     <div>
        <label>Second Number:</label>
        <input data-bind="value: second_no" />
    </div><div>
        <label>Result :</label>
        <input data-bind="value: result" />
    </div></div>

这些result,second_no,operator,first_no是我的列名 数据库,jobs 是一个 observableArray.

我知道我不正确,但我需要找到一些方法来解决上述问题。

注意: 我正在使用 breezeJS 从数据库中获取和存储数据,这就是我提到标签的原因它,虽然上面的问题是淘汰赛而不是breeze。

在没有看到其余代码的情况下,您的代码的最大问题是使用了 with 绑定而不是 foreach。 foreach用于迭代,with用于改变上下文

<div data-bind="foreach: jobs">
      <div>
        <label>FirstNumber :</label>
        <input data-bind="value: first_no" />
    </div>

         <div>
        <label>operator :</label>
         <input type="text" data-bind="text:operators.name, value: $root.operator"/>
    </div>
     <div>
        <label>Second Number:</label>
        <input data-bind="value: second_no" />
    </div><div>
        <label>Result :</label>
        <input data-bind="value: result" />
    </div>
</div>