2015-02-23 51 views
5

我一直在拖網SO問題的答案,應該是非常簡單的東西,但對於我的生活我無法弄清楚。Meteor-AutoForm:如何更新基於另一個控件的選擇選項

基本上我有流星自動窗體有兩個選擇控件:

<template name="processFormTemplate"> 
    {{#autoForm id="processForm" collection="Processes" type=formAction doc=doc validation="blur"}} 
     <div class="col-md-12"> 
      {{> afQuickField name="elementId" options=elements}} 
      {{> afQuickField name="categoryId" options=categories}} 
      {{> afQuickField name="title"}} 
      {{> afQuickField name="desc" rows=4}} 
     </div> 
     {{>formButtons}} 
    {{/autoForm}} 
</template> 

然後,這些有幫手來填充選項:

Template.processFormTemplate.helpers({ 
    elements: function() { 
    return getFormElements(); 
    }, 
    categories: function(elementId) { 
    return getFormCategories(this.doc.elementId); 
    } 
}); 

的lib/methods.js

getFormElements = function() { 

     var options = []; 

    Elements.find({}, {sort: {ref:1}}).forEach(function (element) { 
        options.push({ 
         label: element.title, value: element._id 
        }); 
       }); 

    return options; 

}; 

getFormCategories = function(elementId) { 

    var options = []; 
    var filter = {}; 

    if (!isBlank(elementId)) { 
     filter.elementId = elementId; 
    } 

    Categories.find(filter, {sort: {ref:1}}).forEach(function (d) { 
        options.push({ 
         label: d.title, value: d._id 
        }); 
       }); 

    return options; 

}; 

現在我知道這是行不通的,因爲助手沒有反應,但我不知道如何改變這種行爲河我還試圖鉤住「變」事件,但這個永遠不會觸發因爲某些原因:

Template.processFormTemplate.events({ 
'change #elementId': function(e) { 
    console.log($('[name="elementId"]').val() + ' is now selected'); 
} 
}); 

所需的行爲是,當一個新的elementId在第一個列表,在第二選項列表中選擇應根據選定的elementId進行刷新。

任何幫助非常感謝。

謝謝, 大衛

回答

12

在我花費數小時解決問題之前,我遇到了同樣的問題。你必須使用簡單的模式來獲得所選選項的值這樣使用自動窗體的API調用Autoform.getFieldValue:

Schemas.Store = new SimpleSchema({ 
center: { 
    type: String, 
    optional: true, 
    autoform: { 
     type: "select", 
     options: function() { 
      return Centers.find().map(function (c) { 
       return {label: c.name, value: c._id}; 
      }); 
     } 
    } 
}, 
region: { 
    type: String, 
    optional: true, 
    autoform: { 
     type: "select", 
     options: function() { 
      if (Meteor.isClient) { 
       var docId = ''; 

       docId = AutoForm.getFieldValue('storesForm', 'center'); 


       return Regions.find({center: docId}).map(function (c) { 
        return {label: c.name + ' (' + c.code + ')', value: c._id}; 
       }); 
      } 
     } 
    } 
}, 
store_name: { 
    type: String 
} 
}); 

BTW,我還在使用[email protected]由於使用自動窗體時遇到的問題。 getFieldValue在5.0.3 5.0

問題我已經報aldeed:https://github.com/aldeed/meteor-autoform/issues/785#issuecomment-84600515

+1

該解決方案爲我工作的偉大。謝謝! – Aaron 2015-04-04 21:02:32

+2

嗨那裏 - 謝謝你的答案...你會如何使用你的解決方案跨多種形式需要相同的行爲? – dsc 2015-04-08 12:06:53

+0

@Woppi好奇你爲什麼在你的區域autoform選項函數中有if(Meteor.isClient())...? – Wes 2015-07-29 15:41:52

5

我已經成功地得到這個工作 - 幾件事情是錯誤的:

  1. 我需要一個「身份證」添加到第一選擇控制,所以我可以捕捉到它的change事件:

    {{> afQuickField name="elementId" id="elementId" options=elements}} 
    

然後我用一個反應快譯通變量,我則s在改變的事件中。會話變量可能會做同樣的工作。

Template.processFormTemplate.events({ 
'change #elementId': function() { 
    dict.set('activeElementId', $('select#elementId').val()); 
} 
}); 

,並以此作爲我的助手類的參數:

categories: function(elementId) { 
    return getFormCategories(dict.get('activeElementId')); 
    } 

希望這有助於其他人有類似的問題。

相關問題