jquery - How can I check checkboxes (or not) based on the value of a field in a Document (Meteor)? -


i need read values mongodb collection , check (or not) checkboxes in meteor template based on field in document matches value of checkbox.

to take step first, populate template checkboxes so:

<div id="seljoblocs" name="seljoblocs">   {{#each joblocs}}     <input type="checkbox" value={{jl_jobloc}}><label>{{jl_jobloc}}</label>   {{/each}} </div> 

...populating value , label template's "joblocs" helper:

joblocs: function() {   return joblocations.find({}, {     sort: {       jl_jobloc: 1     },     fields: {       jl_jobloc: 1     }   }); } 

when selection made "worker" input select element, can note worker selected so:

template.matchworkerswithjoblocs.events({     'change #selworker': function(event, template) {         var workerid = template.find('#selworker').value;         // todo: add code query collection , check appropriate checkboxes     }, 

...so can check checkboxes matches found (a "match" means worker qualified assigned job/location). iow, returned document field value (jobloc), want check appropriate checkboxes.

my question is, how can that?

is case (hopefully!) that, within following spacebars loop:

{{#each joblocs}}   <input type="checkbox" value={{jl_jobloc}}><label>{{jl_jobloc}}</label> {{/each}} 

...i can have "ischecked" helper this:

{{#each joblocs}}   <input type="checkbox" value={{jl_jobloc}} {{ischecked}}><label>{{jl_jobloc}}</label> {{/each}} 

...that either returns empty string or string "checked" based on whether meteor method returns true:

template.matchworkerswithjoblocs.helpers({     ischecked: function () {     var workerid = $('#selworker').val;     if (null == workersjoblocslookup.findone( {wjllu_workerid: workerid, wjllu_jobloc: joblocs.jl_jobloc})) {         return '';     } else {         return 'checked';     } } 

that way, checkbox checked (because function returns "checked", checks checkbox) or not, because function returns empty string.

iow, nitty gritty, "joblocs.jl_jobloc" field spacebars loop available/accessible within helper, can see if has corresponding document in workersjoblocslookup collection?

not sure i'm sure you're asking, can access values of items in current row / handlebars iteration in helper this._id or this.somefield

possibly if reading correctly:

template.matchworkerswithjoblocs.helpers({    getjobslocs: function() {      var job = this.joblocs.jl_jobloc;      return workersjoblocslookup.findone({        fieldwithjobloc: job      });    }  });

if trying return more 1 result, use find, not findone, , may want put inside if (template.instance().subscriptionsready()) {} function.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -