ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Safecracker and GWCODE Categories Checkboxes not staying checked

February 21, 2013 6:43pm

Subscribe [2]
  • #1 / Feb 21, 2013 6:43pm

    bdcads

    30 posts

    Here is what I am trying to do.

    I am using Safecracker to do the add/edit posting, but when the user click on a checkbox, then
    go to edit the posting, the check box is not there.

    I am using Gwcode Categories Plugin, outputting my categories as checkboxes
    because it has more feature and flexibility then the built in safecracker categories tag

    GWCode Categories

    <ul>
     {exp:gwcode_categories group_id="9"} 
     <li>
     <input type="checkbox"  id="categories" name="category[]" value="{cat_id}" {checked} /> 
     <span>{cat_name}</span>
     </li>
     {/exp:gwcode_categories}
    </ul>

    but when I use Safecracker

    <ul>
    {categories}
    <li><input type="checkbox" name="category[]" id="categories" value="{category_id}" {checked} /> 
    <span>{category_name}</span> </li>
    {/categories}
    </ul>

    the checkbox is there.
    why I cant just used the {checked}  in the Gwcode Categories and why does it have to be in
    {categories}{/categories}

    I want more flexibility.

     

  • #2 / Feb 21, 2013 8:00pm

    ahmad saad

    364 posts

    Hi bdcads,

    First thank’s for u , I don’t use this addon before , but I will :lol:

    I go to GWCode website and read the DOC and there is no {checked} var as you can see in

    http://gwcode.com/add-ons/gwcode-categories/documentation

    and in http://gwcode.com/add-ons/gwcode-categories/examples/example1 they said:

    Showing categories for an entry is easy, you can do this with ExpressionEngine’s native {categories} variable pair: http://expressionengine.com/user_guide/modules/channel/channel_entries.html#categories

    Or, with GWcode Categories:

    so it’s works like channel entries categories var pair which don’t have {checked} var also.

    however Safecracker categories var pair have this var .

    I hope this will be helpfull

    Regards,

    Ahmad saad

  • #3 / Feb 22, 2013 12:12pm

    bdcads

    30 posts

    Hi Ahmad,
    thanks for responding

    well here is the code that I have right now and trying to make it work with safecracker

    {exp:gwcode_categories group_id="9"} 
     {if has_children && parent_id == '0' }
      <!-- parent with children -->
      <li class="checkbox checkbox_parent">  
       <span><b>{cat_name}</b></span>
      </li>
     {if:elseif depth == 2}
      <!-- child at second level -->
      <li class="checkbox">  
       <input type="checkbox" id="categories" name="category[]" value="{cat_id}" checked="{checked}" />
       <span>{cat_name}</span>
      </li>
     {if:elseif depth == 3}
      <!-- child at third level -->
      <li class="checkbox">  
       <input type="checkbox"  id="categories" name="category[]" value="{cat_id}" checked="{checked}" />
       <span>{cat_name}</span>
      </li>
     {if:else}
      <!-- parent with no children -->
      <li class="checkbox_parent_nochild">
       <input type="checkbox"  id="categories" name="category[]" value="{cat_id}" checked="{checked}"  /> 
       <span>{cat_name}</span>
      </li>
     {/if}
    {/exp:gwcode_categories}
  • #4 / Feb 22, 2013 12:53pm

    bdcads

    30 posts

    Why in safecracker categories, why cant you do a simple if else statement inside?
    unless I am doing something very very wrong

    <ul>
    {categories category_group_id="9"} 
            {if category_depth == 1 }
             <!-- parent with children -->
             <li class="checkbox checkbox_parent">  
              <span><b>{category_name} parent</b></span>
             </li>
          {/categories}
    
    </ul>

    tried this with all of tags given, not only {category_depth}, tried it with {category_parent} also

  • #5 / Feb 22, 2013 3:33pm

    ahmad saad

    364 posts

    Hi bdcads,

    try:

    <ul>
    {categories category_group_id="9"} 
            {if {category_depth} == 1 }
             <!-- parent with children -->
             <li class="checkbox checkbox_parent">  
              <span><b>{category_name} parent</b></span>
             </li>
          {/categories}
    
    </ul>

    Regards,

    Ahmad saad

  • #6 / Feb 22, 2013 4:36pm

    bdcads

    30 posts

    The issue before was everything being returned in a linear way, rather than nested as they probably should have been. This solution makes it “nested” so we can theoretically do whatever we want to it.

    We ended up doing it this way…

    <?php $typesArray = array(); ?>
    
    {categories category_group_id="9"}
     <?php
     $typesArray[ '{category_id}' ] = array(
      'category_name'  => '{category_name}',
      'category_id'  => '{category_id}',
      'category_group_id' => '{category_group_id}',
      'category_parent' => '{category_parent}',
      'category_depth' => '{category_depth}',
      'checked'   => '{checked}'
      );
    
     ?>
    {/categories}
    
    <?php
    $typesArrayNested = array();
    foreach($typesArray as $type_key => $type_val) {
     
     switch(true) {
    
      case ( $type_val['category_depth'] == 1 ) : 
       $typesArrayNested[ $type_val['category_id'] ]['info'] = $type_val;
       $parentCat = $type_val['category_id'];
       break;
    
      case ( $type_val['category_depth'] == 2 && $type_val['category_parent'] == $parentCat ) : 
       $typesArrayNested[ $parentCat ]['children'][ $type_val['category_id'] ] = $type_val;
       break;
     }
    }
    
    // show parent with children
    foreach( $typesArrayNested as $type ) {
     
     if( isset($type['children']) ) {
      echo '<li class="checkbox_parent_nochild listing_checkboxes_' . $type['info']['category_depth'] . ' parent_' . $type['info']['category_id'] . '">';
       echo '<span>' . $type['info']['category_name'] . '</span>';
      echo '</li>';
     }
    
     // attempt to show children or a parent without children
     switch(true) {
    
      // we have children
      case ( isset($type['children']) ) :
       foreach($type['children'] as $child) {
        echo '<li class="checkbox_parent_nochild listing_checkboxes_' . $child['category_depth'] . ' parent_' . $child['category_id'] . '">';
         echo '<input type="checkbox"  id="categories" name="category[]" value="' . $child['category_id'] . '" ' . $child['checked'] . ' /> ';
         echo '<span>' . $child['category_name'] . '</span>';
        echo '</li>';
       }
       break;
    
      // parent with no children
      case ( !isset($type['children']) ) :
       echo '<li class="checkbox_parent_nochild listing_checkboxes_' . $type['info']['category_depth'] . ' parent_nochild">';
        echo '<input type="checkbox"  id="categories" name="category[]" value="' . $type['info']['category_id'] . '" ' . $type['info']['checked'] . ' /> ';
        echo '<span>' . $type['info']['category_name'] . '</span>';
       echo '</li>';
       break;
     }
    }
    ?>
  • #7 / Feb 22, 2013 4:38pm

    bdcads

    30 posts

    Hi bdcads,

    try:

    <ul>
    {categories category_group_id="9"} 
            {if {category_depth} == 1 }
             <!-- parent with children -->
             <li class="checkbox checkbox_parent">  
              <span><b>{category_name} parent</b></span>
             </li>
          {/categories}
    
    </ul>

    Regards,

    Ahmad saad

    I think you copied the exact code I pasted in my post above yours. No difference between the two :(

  • #8 / Feb 22, 2013 6:00pm

    ahmad saad

    364 posts

    Hi bdcads,

    the differnce is {if category_depth == 1 } (u code) {if {category_depth} == 1 } (me code)

    use category_depth with the parentheses.

    and I don’t recommend to use php as you post above , it’s better to create a littel pluging to check if a specific entry belong to a specific cat , then use it with your gwcode_categories .

    Regards,

    Ahmad saad.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases