Отключить конкретное текстовое поле из динамического создания текстового поля с помощью Knockout.js

Мне нужно отключить конкретное текстовое поле из списка динамически созданных текстовых полей. Рассмотрим код, в котором я динамически создаю 5 текстовых полей с помощью «кнопки создания», из которого мне нужно отключить 2-е текстовое поле.

  <input type="button" value="Create TextBox" data-bind="click: addCourse"/>
  <input type="button" value="Disable 2nd TextBox" data-bind="click: disable"/>

  <div data-bind="foreach: cources">
        <div>
            <input type="text"  data-bind="value: textValue,disable: disableStatus"/>
        </div>          
  </div>
  <div data-bind="foreach: cources">
      <div>
         <span type="text" data-bind="text: textValue"/>
       </div>
   </div>

js-код:

function CourseViewModel(){
    this.textValue = ko.observable(''); 
    this.disableStatus = ko.observable(false); 
}

function CeremonyViewModel() {

    this.cources = ko.observableArray();


    this.addCourse = function(){
        this.cources.push(new CourseViewModel());
    };
    this.disable = function()
    {
        this.disableStatus(true);
    }
}

ko.applyBindings(new CeremonyViewModel());

person sona    schedule 07.01.2015    source источник


Ответы (1)


Просто используйте индекс массива:

this.disable = function()
{
    if (this.cources().length > 1) {
        this.cources()[1].disableStatus(true);
    }
}

http://jsfiddle.net/422b5ju7/1/

person Wayne Ellery    schedule 07.01.2015