2

私のコードにはブール値のチェックボックスがあります。私の認識では、オンチェックではその値を true として返し、チェックを外すとその値を false として返す必要があります。しかし、私は次のような別の状況に直面しています。

ページの初期ロード時に、メッセージが表示されます:「選択されていません」 チェックボックスをオンにすると、値が表示されます:「true」 「true」、何回チェックしたりチェックを外したりしても。

誰かが私に何が問題で、どのように修正して望ましい結果を得ることができるかを教えてもらえますか:

オートフォーム HTML コードは次のとおりです。

{{#autoForm  collection='Collections.Category' validation='submit' id='CategoryInsertForm' class="form-horizontal  form-with-legend"  role="form" type='method' meteormethod='CategoryInsertMethod' }}

{{ currentFieldValue 'isParent' }}

{{> afFormGroup  name='isParent' id='isParent' type="boolean-checkbox"}}

{{#if afFieldValueIs name="isParent" value= 'true'}}
{{> afFieldInput name='parentId' id='parentId' class='form-control'}}
{{/if}}
{{/autoForm}}

JSコードは次のとおりです。

Template.registerHelper("currentFieldValue", function (fieldName) {
    return AutoForm.getFieldValue( fieldName) || "not selected";
});

スキーマコードは次のとおりです。

Collections.Category =  new Mongo.Collection('category');

Schemas.Category = new SimpleSchema({

    catId:{
         type: String,
         optional: true,
         unique: true
    },
    isParent: {
        type: Boolean,
        optional: true,
        defaultValue: false,
      // ,allowedValues: [true, false]
        label: "Parent category"

    },
    parentId: {
        type: String,
        label: "ParentID",
        optional: true

    },
    title: {
        type: String,
        optional:true
    }

});

Collections.Category.attachSchema(Schemas.Category);
4

2 に答える 2

1

次のようなヘルパーを作成して、これを回避しました。

Template.myTemplate.events({
'change .myCheckboxClass': function(event) {
    clickedElement = event.target;
    clickedElement.value = !clickedElement.checked;
}
});
于 2015-04-14T15:09:09.477 に答える
0

現在、同様の問題に対して次の設定を使用しています。

テンプレート:

<template name='test'>
 {{autoFormTest}}
 {{> quickForm collection="Sample" id="sampleForm" type="update" doc=this}}
</template>

JS:

Template.test.helpers({ 
    autoFormTest: function(){
        return AutoForm.getFieldValue("radiobuttonField", "sampleForm").toString();
    }
})

スキーマの関連部分:

radiobuttonField: {
    optional: true,
    type: Boolean,
    label: "Is it true?",
    autoform: {
        type: "boolean-radios",
        trueLabel: "Yes",
        falseLabel: "No "
    }
}

ヘルパーはfalseまたはtrue文字列を返しますが、デフォルトはfalseです。したがって、質問に関連する2つの重要な部分があります。

  1. ヘルパーでフォームの名前を指定する必要がありますAutoForm.getFieldValue(..., "sampleForm")

  2. ブールセレクターから文字列値(書き出すことができる)を取得したい場合は、それを文字列に変換する必要がありますAutoForm.getFieldValue(...).toString()

それが役立つことを願っています。

于 2016-09-28T11:41:20.040 に答える