1

In a table I have a list of users,and some options that I can selecte for them.

||USER          ||      OPTIONs  ||
||User1         ||  Opt1 || Opt2 ||
||User2         ||  Opt1 || Opt2 ||

The users are displayed as radio button, the options are checkboxes of info about the user that I want to display(both/just one at time).

1 I want to disable the selection of the options if you didn't select the user before them. After the user is selected you can select the options you want

2 I need to change something in the code becose if now I select the user 1 and the option 1, and then I select the user 2 I can see the info for the option 1 of user 2 without clicking on the option 1 check box.

This is my code:

HTML

<template name="patients">
  <table>
     <tr>
        <th>Patient</th>
        <th>Options</th>
     </tr>
  </table>
  {{#each Users}}
  <table>
     <tr>
       <th>
          <label><input type="radio" class="selected" name="patient" value="{{this._id}}">{{this.profile.lastName}} {{this.profile.firstName}}</label>
       </th>
       <td>
         <div class="patinf">
           <label><input type="checkbox" class="infos" name="{{this._id}}"  id="showInfos"><span>OPT1</span></label>
           <label><input type="checkbox" class="answers" name="{{this._id}}" id="showAnswers"><span>OPT2</i></span></label>
        </div>
      </td>
    </tr>
  </table>
  {{/each}}
  {{>display}}
</template>


<template name="display">
  {{#if isInfosClicked}}
      <div name="showInfos">
        <h4>Infos for {{data.profile.lastName}} {{data.profile.firstName}}</h4>

      </div>
  {{/if}}
  {{#if isAnswersClicked}}
    <div name="showAnswers">
        <h4>Answers for {{data.profile.lastName}} {{data.profile.firstName}}</h4>
    </div>
  {{/if}}
</template>

This is my JS

     Template.patients.helpers({
        Users: function() {
            return Meteor.users.find({});
        },
     });

     Template.patients.events({
        'click .selected': function (){
        var selPat = $('input[name="patient"]:checked').val();
        Session.set("selPat",selPat);
        }
     });


     Template.patients.events({
        'click .infos': function(){
          Session.set("infosClicked", true);
         },
        'click .answers': function(){
          Session.set("answersClicked", true);
         },        
     });

     Template.display.helpers({
        isInfosClicked: function(){
            return Session.get("infosClicked");
        },
        isAnswersClicked: function(){
            return Session.get("answersClicked");
        },
        data: function(){
            var PAT= Meteor.users.findOne({ _id: Session.get("selPat")});
            return PAT;
       }
     });
U Rogel
  • 1,893
  • 15
  • 30
Jenny
  • 33
  • 7

1 Answers1

0

The code bellow should put you on the track to where you want to get.

JS:

Template.patients.helpers({
 Users: function() { // I used the fake Users collection
        return Meteor.users.find({}).fetch();
    },
    isChecked: function(){ // disable the checkboxes if not choosen
        return !(Session.get("selPat") === this._id);
    }
});

Template.patients.events({
    'click .selected': function (e){
        var selPat = $('input[name="patient"]:checked').val();
        if(selPat !== Session.get("selPat")){ 
            // if a different user is choosen, 
            // clear the checkboxes and the Sessions
            Session.set("infosClicked", false);
            Session.set("answersClicked", false);
            $(`input[type="checkbox"]`).prop('checked', false);
        }
        Session.set("selPat",selPat);
    },
    'click .infos': function(){
        Session.set("infosClicked", !Session.get("infosClicked"));
     },
    'click .answers': function(){
        Session.set("answersClicked", !Session.get("answersClicked"));
     },        
});


Template.display.helpers({
    isInfosClicked: function(){
        return Session.get("infosClicked");
    },
    isAnswersClicked: function(){
        return Session.get("answersClicked");
    },
    data: function(){ // I used the fake Users collection
        var PAT= Meteor.users.findOne({ _id: Session.get("selPat")});
        return PAT;
   }
});

In the html the main difference is adding disabled attr with helper logic. Other than that I formed it in a table as you showed.

HTML:

<template name="patients">
    <table>
       <tr>
          <th>Patient</th>
          <th colspan="2">Options</th>
       </tr>

        {{#each Users}}
        <tr>
            <td>
                <label><input type="radio" class="selected" name="patient" value="{{this._id}}">{{this.profile.lastName}} {{this.profile.firstName}}</label>
            </td>
            <td>
                <div class="patinf">
                    <label><input type="checkbox" class="infos" name="{{this._id}}" id="showInfos" disabled="{{isChecked}}"><span>OPT1</span></label>
                </div>
            </td>
            <td>
                <div class="patinf">
                    <label><input type="checkbox" class="answers" name="{{this._id}}" id="showAnswers" disabled="{{isChecked}}"><span>OPT2</span></label>
                </div>
            </td>
        </tr>
      {{/each}}
    </table>
    {{>display}}
</template>


<template name="display">
    {{#if isInfosClicked}}
        <div name="showInfos">
          <h4>Infos for {{data.profile.lastName}} {{data.profile.firstName}}</h4>

        </div>
    {{/if}}
    {{#if isAnswersClicked}}
      <div name="showAnswers">
          <h4>Answers for {{data.profile.lastName}} {{data.profile.firstName}}</h4>
      </div>
    {{/if}}
</template>

After making it to work with this, I would suggest you take a look at ReactiveVar, it is a more recommended solution for the template level variables, and you can combine it with template data context than you have more control on your data and the template state.

U Rogel
  • 1,893
  • 15
  • 30
  • It doesn't work: it seems that .filter doesn't work with Meteor.users that is my User collection. What do you suggest? – Jenny Dec 14 '17 at 09:57
  • I used `filter` just because I used a fake users collection. Use it with `Meteor.findOne()` as you did before. – U Rogel Dec 14 '17 at 09:58
  • I have this error Exception in template helper: TypeError: Cannot read property '_id' of undefined at Object.Meteor.users.findOne – Jenny Dec 14 '17 at 10:43
  • I've edited the JS code, it is now using a real `Meteor.users` collection. – U Rogel Dec 14 '17 at 11:11
  • Now it works but if I remove the check to a checkbox the relative part doesn't become hide.... – Jenny Dec 14 '17 at 12:00
  • @Jenny Sorry not sure what you mean, say you choose a user, then an option, you see some info bellow, but then if you change the user the info is still displayed? – U Rogel Dec 15 '17 at 10:01
  • I mean everything is ok in the selection of the user: the checkboxes are disabled if they are not on the same row of the selected user. The problem I have now is for the checkbox: I want to have a show/hide effect when I select the checkboxes: if I select the checkbox opt1 I display the relative information, now I wnat to discheck the checkbox and hide the information. Like here: https://stackoverflow.com/questions/18307323/how-to-show-hide-an-element-on-checkbox-checked-unchecked-states-using-jquery – Jenny Dec 15 '17 at 11:04
  • Sure! edited the answer. Just added for the click event handlers `Session.set("var-name", !Session.get("var-name"))` then it is alternating the var boolean state. – U Rogel Dec 15 '17 at 12:31
  • Now it's PERFECT! Thank you so much for the help! – Jenny Dec 15 '17 at 12:47