0

I would like to implement the following user-group structure in my application:

 Group_1
   Group_1_1
   Group_1_2
 Group_2
   Group_2_1
     Group_2_1_1

I did manage to do that with django-mptt, but I am facing the following problem at the moment:

In the user-interface I would like to have something like this:

Memberships:
--------------------
[ ] Group_1
   [ ] Group_1_1
   [ ] Group_1_2
[ ] Group_2
   [ ] Group_2_1
      [ ]Group_2_1_1 

and if a User wants to join Group_1_1 and Group_2_1, the checkboxes for joining Group_1 and Group_2 are automatically checked as well, like this:

Memberships:
--------------------
[x] Group_1
   [x] Group_1_1
   [ ] Group_1_2
[x] Group_2
   [x] Group_2_1
      [ ]Group_2_1_1 

It feels like it isn't too hard to accomplish with this pseudocode:

If checked_group has parent:
  check parent_group

There is a JQuery solution for this, but I think it should be able to accomplish without the use of that, but if it really isn't - what's the best way of implementing the linked app to a django project?

ekad
  • 14,436
  • 26
  • 44
  • 46
Icho Tolot
  • 11
  • 3

1 Answers1

1

In user interface you can easily implement with JQuery. For example:

<div>
  <input class="parent" type="checkbox"  /> Group_1
  <ul>
    <li><input class="child" type="checkbox" />Group_1_1</li>
    <li><input class="child" type="checkbox" />Group_1_2</li>
  </ul>
</div>
<div>
  <input class="parent" type="checkbox"  /> Group_2
  <ul>
    <li><input class="child" type="checkbox" />Group_2_1</li>
    <li><input class="child" type="checkbox" />Group_2_1</li>
  </ul>
</div>
<script>
   $( document ).ready(function() {
      $('.child').click(function(){
        $(this).closest('div').find('.parent').attr('checked', true)
      });
   });
</script>

In your view you can use the same pseudocode logic:

if child_group_obj.parent:
    child_group_obj.parent.checked = True
RéÑjïth
  • 458
  • 5
  • 13
  • I would like to have this functionality inside the django-admin and preferably by using the existing checkbox-classes - I still think it shouldn't be that hard - will continue to search for another solution, but thanks for your time! – Icho Tolot Feb 18 '15 at 21:38
  • I never tried that. I think you can override django-admin templates. – RéÑjïth Feb 20 '15 at 05:50