Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
Hi there,

I have a customer who is a car salesman and want a website, so far I have been able to make everything so that he can add his cars to the site. Now, for convenience, he would like checkboxen in the admin with the most used options of a car. As;

Air conditioner
Electrical windows
ABS
etcetera

So if he check the checkbox of Air conditioner this option will appear on the site and the others not.

I think you know what I meen :)

Does someone has a snippet for me how to do this thanks.
Kindly post your HTML which is to be couchified. We'll start from there.
For convenience, I only have added the pieces of code that are relevant.
Everything works well, only the customer has to enter the options themselves, since the options are often the same, it would be handy to have check boxes with the options, so he only have to check the one he needs. :)

Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Occasions' clonable='1' order='2'>
   
    <cms:repeatable name='options' >
        <cms:editable type='text' name='options'
        label='All options'
        desc='Type all the option of the occassion. Example: ABS, Airbag etc.'
        />     
    </cms:repeatable>
   
</cms:template>
<cms:if k_is_page >


Code: Select all
<div class="row">
  <div class="info">
    <span class="name">Options:</span>
    <span class="input"> <ul>
       <cms:show_repeatable 'options' >
        <li>
          <span><cms:show options/></span>
        </li>
       </cms:show_repeatable>
      </ul> </span>
  </div>
</div>
I am sure user needs not only check what he needs, but also add/remove options with ease. This can be achieved via dynamic generation of options in a checkbox. Let's see how we can do that.

We'll have to set up the list somewhere once and make it editable for admin. Values from that list must appear as checkboxes in each cloned page. Define your checkbox editable in this fashion -

Code: Select all
<cms:editable
  name="my_options"
  label="Options" desc="Check all applicable"
  dynamic='opt_values'
  opt_values='my_options.inc'
  type='checkbox'
/>

<cms:globals>
    <cms:repeatable name='options' label='All options' desc='Example: ABS, Airbag etc. One option per row!' >
        <cms:editable type='text' name='option' />
    </cms:repeatable>
</cms:globals>


To explain the code - we are using a (undocumented) parameter 'dynamic' and telling it that 'opt_values' is the field that instead of containing the usual static value, actually needs to be filled dynamically by using the snippet that is its value.
The value in 'opt_values' ('my_options.inc') is executed by Couch and the result is placed as the computed value of 'opt_values'.

If your CouchCMS version is at least 2.1 and you have studied its release notes (viewtopic.php?f=5&t=11105) you'll also recognize the 'Globals' part. Go to 'Manage globals' section for that template and add a couple of options -


2019-01-13-001.png
options
2019-01-13-001.png (11.06 KiB) Viewed 1029 times


Next, create a snippet 'my_options.inc' in your snippets folder (couch/snippets by default). Place the following code in it and it will be executed upon each load of cloned page in backend filling our checkboxes editable with options dynamically -

Code: Select all
<cms:get_global 'options' >
    <cms:show_repeatable 'options'>

        <cms:show option /> ||

    </cms:show_repeatable>
</cms:get_global>


2019-01-13-002.png
checkboxes
2019-01-13-002.png (3.37 KiB) Viewed 1029 times


Display code is also pretty simple -
Code: Select all
<div class="row">
  <div class="info">
    <span class="name">Options:</span>
    <span class="input">
        <ul>
           <cms:each my_options as='option'>
               <li><span><cms:show option/></span></li>
           </cms:each>
        </ul>
    </span>
  </div>
</div>


Does it help?

P.S. One possible issue with dynamic approach - if admin removes one of the options in globals, it will be hid from checkboxes as expected, but not from the saved pages in database. I.e. if there was an option 'Diesel' and some cars had it listed, then if it is removed from the main list, the cars still show 'Diesel' among the options, because list only affects the visibility of options in checkboxes. Admin must re-save such pages to persist the change.
Damn your good.

Works perfectly. Thanks you very must.

Don't have a lot of money, but will donate.
Hope that other people find this useful to and donate.
You are most welcome, Kassi. I have received your "thank you" :)
6 posts Page 1 of 1
cron