单选复选框

时间:2019-03-06 12:21:11

标签: c# loops input knockout.js onclick

我只想在我的foreach循环中选择一个复选框,现在我可以选择多个。我有一个click事件,但是在选择复选框时不会取消选中其他复选框。怎么了谢谢

<div class="consulting-editors" data-bind="foreach: ConsultingEditors">
    <input type="checkbox" name="Promote" data-bind="checked: Promote, click: $parent.promoterSelectedOnclick" /> Display as main editor 
</div>

ConsultingEditors: KnockoutObservableArray<NavigatorAuthorApi> = ko.observableArray();  

promoterSelectedOnclick = (selectedEditor: NavigatorAuthorApi) => {

    if (this.ConsultingEditors().some(e => e.Promote)) {
        this.ConsultingEditors().filter(e => e.AuthorRef != selectedEditor.AuthorRef).forEach((e) => {
            e.Promote = false;
        });
    }

    return this.ConsultingEditors();
}

export type NavigatorAuthorApi =
    {
        SortOrder: number,
        FirmRef: number,
        FirmName: string,
        AuthorRef: number,
        AuthorName: string,
        DisplayString: string,
        EditorImage: ByteString[],
        Promote: boolean
    }   

2 个答案:

答案 0 :(得分:0)

如果希望属性更改反映在DOM上,

Promote应该是observable

class viewModel {

  ConsultingEditors = ko.observableArray([
    { Promote: ko.observable(false), AuthorRef: 1},
    { Promote: ko.observable(false), AuthorRef: 2 },
    { Promote: ko.observable(false), AuthorRef: 3 }
  ]);

  promoterSelectedOnclick = (selectedEditor) => {
    this.ConsultingEditors().filter(e => e.AuthorRef != selectedEditor.AuthorRef).forEach((e) => {
      e.Promote(false);
    });
    return true;
  }
}

ko.applyBindings(new viewModel())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<div class="consulting-editors" data-bind="foreach: ConsultingEditors">
  <input type="checkbox" name="Promote" data-bind="checked: Promote, click: $parent.promoterSelectedOnclick" /> Display
</div>

答案 1 :(得分:0)

两件事,

  1. 您需要click函数返回true,否则click绑定将与选中的绑定冲突。

  2. Promote用作布尔值,但UI必须对其具有可观察性,以对其值的任何更改做出反应。用户界面需要做出的反应必须是可观察到的。

function viewModel(){
    var self=this;

    self.ConsultingEditors = ko.observableArray([
    	new NavigatorAuthorApi(false, 1),
      new NavigatorAuthorApi(false, 2),
      new NavigatorAuthorApi(false, 3)
    ]);

    self.promoterSelectedOnclick = function(selectedEditor){
        if (self.ConsultingEditors().some(e => e.Promote())) {
            var others = self.ConsultingEditors().filter(e => e.AuthorRef != selectedEditor.AuthorRef);
            others.forEach((e) => {
                e.Promote(false);
            });
        }
        return true;
    }
}

function NavigatorAuthorApi(promote, authorRef)
{
		var self = this;
  	self.SortOrder = null;
    self.FirmRef = null;
    self.FirmName = null;
    self.AuthorRef = authorRef;
    self.AuthorName = null;
    self.DisplayString = null;
    self.EditorImage = null;
    self.Promote = ko.observable(promote);
}   
    
ko.applyBindings(new viewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<div class="consulting-editors" data-bind="foreach: ConsultingEditors">
    <input type="checkbox" name="Promote" data-bind="checked: Promote, click: $parent.promoterSelectedOnclick" /> Display as main editor 
</div>