How to return multiple values from #each helper

Hello guys…

I wanna get output with #each helper like this…

     <tbody>
         {{#each deviceList}}
         <tr>
             {{#each deviceData}}
             <td><input id="{{inputId}}" value="{{deviceValue}}"></td>
             {{/each}}
          </tr>
          {{/each}}
     </tbody>

If I replace deviceValue with this I get all the elements inside the collection…

My objective is to set the id of each inputs…

on the client side…

Template.devices.helpers({
deviceData: function() {
      var device = [], id = [];
      var i=0;
      for(key in this) {
        if(key!='_id') {
          var value = this[key];
          device.push(value);
          id.push(this._id+'_'+i;
          i++;
        }
      }
      return {inputId: id, deviceValue: device};
    }
});

Is this possible…??
Or plzz suggest me how can I set the ids of each inputs…

I think you meant you get all the fields in the record, including _id.

Following that, try this._id and see what happens.

thanx for ur suggestion…
but…

i want to get with inputId, ids like _id _1, _id _2, and so on for the individual inputs generated…

Could you do this instead (leave html as is):

Template.devices.helpers({
  deviceList: function() {
    var a = [];
    var i=0;
    for (key in this) {
      if (key!='_id'){
        var value = this[key];
        a.push({inputId:this._id+'_'+i,deviceValue:value});
        i++;
      }
    }
    return a;
  }
});
1 Like

It’s confusing for me when you use _id_1/2/3 because Meteor/Mongo uses _id for each document.

When you say inputId, you mean like HTML DOM object IDs? Did I get that right? As in $('#someid')?

Also, no need to create two separate arrays.

Here’s my take:

Template.devices.helpers({
	deviceData: function() {
		var retval = [];
		for (key in this) {
			if (key!='_id') {
				retval[key] = this[key];
			}
		}
		return retval;
	}
});

Sorry - didn’t paste it as code the first time. It’s early where I am.

The line
var value = this[key];
is redundant in my code; that came from first sample.

I’m just returning an array of objects to match what’s in the template vars on the html side.

Yupp @necmettin that is DOM object Id…

Thanx @jaclynn
it’s working…
thanx for ur suggestion…

1 Like