Mark onclick Checkboxes on the left

Viewed 85

enter image description here

I have this perms mannaging form to set up perms for the users. The idea I have is:

  • When "Manage" is selected, view/edit/lend gets selected aswell and disabled.
  • If any of the others get selected, the rest in the left side should get selected and disabled aswell.

The idea is that users can't select edit but not view, or lend but not edit... and so on... but if for example Edit gets selected, lend doesnt and view does.

Code Snippet:

$('.manage_check').on('change',function(){
  $(this).parents('.perms-group').find('input[type="checkbox"]:not(".manage_check")').click();
  $(this).parents('.perms-group').find('input[type="checkbox"]:not(".manage_check")').prop("disabled",true);
});
<!--begin: Form Wizard Step 2-->

<script src="https://library.entornocbs.eu/assets/plugins/global/plugins.bundle.js" type="text/javascript"></script>
<link href="https://library.entornocbs.eu/assets/plugins/global/plugins.bundle.css" rel="stylesheet" type="text/css">
<link href="https://library.entornocbs.eu/assets/css/style.bundle.css" rel="stylesheet" type="text/css">
<script src="https://library.entornocbs.eu/assets/js/scripts.bundle.js" type="text/javascript"></script>
<script>
    var KTAppOptions = {
        "colors": {
            "state": {
                "brand": "#1dc9b7",
                "light": "#ffffff",
                "dark": "#282a3c",
                "primary": "#5867dd",
                "success": "#1dc9b7",
                "info": "#36a3f7",
                "warning": "#ffb822",
                "danger": "#fd3995"
            },
            "base": {
                "label": ["#c5cbe3", "#a1a8c3", "#3d4465", "#3e4466"],
                "shape": ["#f0f3ff", "#d9dffa", "#afb4d4", "#646c9a"]
            }
        }
    };
</script>
<div class="kt-wizard-v1__content" data-ktwizard-type="step-content">
    <div class="kt-form__section kt-form__section--first">
        <div class="kt-wizard-v1__form">
            <div class="form-group form-group-last row">
              <label class="col-lg-2 col-form-label">Users:</label>
              <div data-repeater-list="" class="col-lg-10">
                  <div data-repeater-item="" class="form-group perms-group row align-items-center">
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>View:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_users" type="checkbox" value="" id="users_view" name="users_view">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Edit:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_users edit_check" type="checkbox" id="users_edit" name="users_edit">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Manage:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_users manage_check" type="checkbox" id="users_manage" name="users_manage">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                  </div>
              </div>
              <label class="col-lg-2 col-form-label">Library:</label>
              <div data-repeater-list="" class="col-lg-10">
                  <div data-repeater-item="" class="form-group perms-group row align-items-center">
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>View:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_library" type="checkbox" id="library_view" id="" name="library_view">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Edit:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_library edit_check" type="checkbox" id="library_edit" name="library_edit">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Lend:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_library lend_check" type="checkbox" id="library_lend" name="library_lend">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Manage:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_library manage_check" type="checkbox" id="library_manage" name="library_manage">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                  </div>
              </div>
              <label class="col-lg-2 col-form-label">Text Books:</label>
              <div data-repeater-list="" class="col-lg-10">
                  <div data-repeater-item="" class="form-group perms-group row align-items-center">
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>View:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_textbooks" type="checkbox" id="textbook_view" name="textbook_view">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Edit:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_textbooks edit_check" type="checkbox" id="textbook_edit" name="textbook_edit">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Lend:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_textbooks lend_check" type="checkbox" id="textbook_lend" name="textbook_lend">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Manage:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_textbooks manage_check" type="checkbox" id="textbook_manage" name="textbook_manage">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                  </div>
              </div>
              <label class="col-lg-2 col-form-label">Teacher Books:</label>
              <div data-repeater-list="" class="col-lg-10">
                  <div data-repeater-item="" class="form-group perms-group row align-items-center">
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>View:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_teacherbooks" type="checkbox" id="teacherbook_view" name="teacherbook_view">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Edit:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_teacherbooks edit_check" type="checkbox" id="teacherbook_edit" name="teacherbook_edit">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Lend:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_teacherbooks lend_check" type="checkbox" id="teacherbook_lend" name="teacherbook_lend">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                      </div>
                      <div class="col-md-2">
                          <div class="kt-form__group--inline">
                              <div class="kt-form__label">
                                  <label>Manage:</label>
                              </div>
                              <div class="kt-form__control">
                                  <span class="kt-switch kt-switch--outline kt-switch--icon kt-switch--success">
                                    <label>
                                        <input class="perms_teacherbooks manage_check" type="checkbox" id="teacherbook_manage" name="teacherbook_manage">
                                        <span></span>
                                    </label>
                                </span>
                              </div>
                          </div>
                          <div class="d-md-none kt-margin-b-10"></div>
                      </div>
                  </div>
              </div>
            </div>
        </div>
    </div>
</div>
<!--end: Form Wizard Step 2-->

The question would be:

  • How can I set to disable all perms when "Manage" of a section is checked?
  • How can I check and disable view when edit or lend is selected?.

2nd Bullet explained:

If you select "view" -> View gets selected as normal.
If you select "edit" -> view gets selected and disabled.
If you select "Lend" -> view/edit get selected and disabled.
If you select "Manage" -> view/edit/lend get selected and disabled.

It should be toggeable also, so when unselecting it, it unchecks and enable the switchers back. Also, as the function to select does .click(), if you select view and then select "manage", it unchecks view and checks edit. It should always check, not .click, when selecting "Manage"

PD: This comes from another post I made today, but since I already closed it, though would be better to create a new one with the new issue.

1 Answers

You might need to adjust this solution to fit your current markup. I tried to make it generic to look easier to understand.

Assuming you use this markup:

<section id="users">
  <h3>
    Users
  </h3>
  <label>View</label>
  <input type="checkbox" value="view" data-group="users" />
  <label>Edit</label>
  <input type="checkbox" value="edit" data-group="users" />
  <label>Manage</label>
  <input type="checkbox" value="manage" data-group="users" />
</section>
<section id="library">
  <h3>
    Library
  </h3>
  <label>View</label>
  <input type="checkbox" value="view" data-group="library" />
  <label>Edit</label>
  <input type="checkbox" value="edit" data-group="library" />
  <label>Lend</label>
  <input type="checkbox" value="lend" data-group="library" />
  <label>Manage</label>
  <input type="checkbox" value="manage" data-group="library" />
</section>
<section id="textBooks">
  <h3>
    Text Books
  </h3>
  <label>View</label>
  <input type="checkbox" value="view" data-group="textBooks" />
  <label>Edit</label>
  <input type="checkbox" value="edit" data-group="textBooks" />
  <label>Lend</label>
  <input type="checkbox" value="lend" data-group="textBooks" />
  <label>Manage</label>
  <input type="checkbox" value="manage" data-group="textBooks" />
</section>
<section id="teacherBooks">
  <h3>
    Teacher Books
  </h3>
  <label>View</label>
  <input type="checkbox" value="view" data-group="teacherBooks" />
  <label>Edit</label>
  <input type="checkbox" value="edit" data-group="teacherBooks" />
  <label>Lend</label>
  <input type="checkbox" value="lend" data-group="teacherBooks" />
  <label>Manage</label>
  <input type="checkbox" value="manage" data-group="teacherBooks" />
</section>

The key here is to link the checkboxes in groups. I used a data-group attribute.

Then you can have something on these lines on your JavaScript (jQuery) code:

$(function() {
  // By having a set of rules defined like this it makes it easier for you to expand them
  const ruleSet = [{
      checked: "manage",
      checkAndDisable: ["view", "edit", "lend"]
    },
    {
      checked: "view",
      checkAndDisable: []
    },
    {
      checked: "edit",
      checkAndDisable: ["view"]
    },
    {
      checked: "lend",
      checkAndDisable: ["view", "edit"]
    }
  ];

  const toggleElementsCheck = (elements, toggle) => {
    elements.each(function() {
      $(this).prop("checked", toggle);
    })
  };

  const toggleElementsDisable = (elements, toggle) => {
    elements.each(function() {
      $(this).prop("disabled", toggle);
    })
  };

  $("input[type=checkbox]").on("click", function() {
    // Which data-group the clicked checkbox belongs into?
    const group = $(this).data("group");

    // What are the other checkboxes in this group?
    const checkboxes = $("input[data-group=" + group + "]").not($(this));

    // What's the value of the clicked checkbox?
    const value = $(this).val();

    // What are the rules for this group of checkboxes?
    const rule = ruleSet.find(x => x.checked === value);

    // What are the values of the checkboxes that need manipulation on this group?
    const checkAndDisable = rule.checkAndDisable;

    // Does this click represents a "check" or an "uncheck"?
    const isChecked = $(this).is(":checked");

    // Loop through the checkboxes in this group and manipulate them accordingly
    $(checkboxes).each(function() {
      let value = $(this).val();
      let toggle = checkAndDisable.includes(value);

      toggleElementsCheck($(this), isChecked ? toggle : false);
      toggleElementsDisable($(this), isChecked ? toggle : false);
    });
  });
});

Working example - JSFiddle

Related