krause
I'm new here

Select CMS_INPUT_CHECKBOX per script

Jump to solution

Hey community,

again and again these annoying checkboxes Smiley Wink

I would like to select (de-selection is easy) all checkboxes in a CMS_INPUT_CHECKBOX per FS_BUTTON:

Formular:

<CMS_INPUT_CHECKBOX name="pt_checkbox" useLanguages="no">

    <ENTRIES>

      <ENTRY value="A">

        <LANGINFOS>

          <LANGINFO lang="*" label="A"/>

        </LANGINFOS>

      </ENTRY>

      <ENTRY value="B">

        <LANGINFOS>

          <LANGINFO lang="*" label="B"/>

        </LANGINFOS>

      </ENTRY>

      <ENTRY value="C">

        <LANGINFOS>

          <LANGINFO lang="*" label="C"/>

        </LANGINFOS>

      </ENTRY>

    </ENTRIES>

    <LANGINFOS>

      <LANGINFO lang="*" label="the options"/>

    </LANGINFOS>

  </CMS_INPUT_CHECKBOX>

  <FS_BUTTON name="pt_select" allowEmpty="yes" onClick="script:select_all" useLanguages="no">

    <LANGINFOS>

      <LANGINFO lang="*" label="select all"/>

    </LANGINFOS>

    <PARAMS>

      <PARAM name="check">#field.pt_checkbox</PARAM>

    </PARAMS>

  </FS_BUTTON>

Script "select_all":

context.logInfo("checked 1 = " + theCheckbox.get());

Set theSet = new HashSet();

theSet.add("A");

theSet.add("C");

theCheckbox.set(theSet);

context.logInfo("checked 2 = " + theCheckbox.get());

No rocket science, but after clicking the button nothing happens :smileydevil:. The log says everything is ok, the selection works, but does not have any effect in the formular.

INFO  06.02.2013 15:17:00.543 (de.espirit.firstspirit.access.BaseContextImpl): checked 1 = []

INFO  06.02.2013 15:17:00.543 (de.espirit.firstspirit.access.BaseContextImpl): checked 2 = [A, C]

When I try to update an CMS_INPUT_FIELD per script everything works fine.

Where is the mistake? Any hints?

I guess this is the same error Re: Scripting: Zugriff auf die Werte einer CMS_INPUT_CHECKBOX, but Reza's solution doesn't work for me, because I NEED a Button to select....

Thanks & greets,

Steffi

FirstSpirit 4.2.484.54957

0 Kudos
1 Solution

Accepted Solutions
broszeit
I'm new here

Hi Steffi,

i asked for this problem myself and was briefed about the following issue:

- the FormField of a checkbox contains a Set of Options but you were trying to set a Set of Strings (HashSet<String> instead of HashSet<Option>)

In order to get the needed options you have to use the class "OptionFactory".

The OptionFactory can be fetched with the help of the GomEditorProvider of your template:

optionFactory = element.getFormData().getForm().findEditor("pt_checkbox").getOptionFactory()

Afterwards you can use the OptionModel to get the Options:

optionModel = optionFactory.getOptionModel(context,language,false);

theSet.add(optionModel.getOption("A")) will activate the checkbox with value "A".

If you want to activate _all_ checkboxes you can use the Iterator which is provided by the optionModel.

Regards

Rouven

View solution in original post

0 Kudos
6 Replies
broszeit
I'm new here

Hi Steffi,

i asked for this problem myself and was briefed about the following issue:

- the FormField of a checkbox contains a Set of Options but you were trying to set a Set of Strings (HashSet<String> instead of HashSet<Option>)

In order to get the needed options you have to use the class "OptionFactory".

The OptionFactory can be fetched with the help of the GomEditorProvider of your template:

optionFactory = element.getFormData().getForm().findEditor("pt_checkbox").getOptionFactory()

Afterwards you can use the OptionModel to get the Options:

optionModel = optionFactory.getOptionModel(context,language,false);

theSet.add(optionModel.getOption("A")) will activate the checkbox with value "A".

If you want to activate _all_ checkboxes you can use the Iterator which is provided by the optionModel.

Regards

Rouven

0 Kudos
andre
I'm new here

see also:  != Beanshell this post

0 Kudos

Hi Rouven,

again, you made my day!

This works, thanks a lot!

Steffi

import de.espirit.firstspirit.access.editor.value.OptionFactory;

context.logInfo("checked 1 = " + theCheckbox.get());

HashSet theSet = new HashSet();

optionFactory = element.getFormData().getForm().findEditor("pt_checkbox").getOptionFactory();

optionModel = optionFactory.getOptionModel(context,null,false);

theSet.add(optionModel.getOption("A"));

theSet.add(optionModel.getOption("B"));

theCheckbox.set(theSet);

context.logInfo("checked 2 = " + theCheckbox.get());

0 Kudos

Hey Community,

now my checkboxes operate smoothly! The last question is:

Is it possible to select the checkboxes only for the current language?

lang.jpg

By clicking the "select all" button in language tab "EN" all checkboxes in all language tabs are selected. But in that case I only want the english checkboxes to be selected.

Of course I have to use the language in the "getOptionModel()" function, but how do I get the current language from the language tab in the script?

optionModel = optionFactory.getOptionModel(context,<CURRENT_LANGUAGE_TAB_VALUE>,false);

Or is it possible to pass the current language per param in the FS_BUTTON, something like this:

<PARAMS>
    <PARAM name="currentLanguage">current_language_tab_value</PARAM>
</PARAMS>

Thanks a lot and happy thursday,

Steffi

0 Kudos

Hi Steffi,

<CMS_INPUT_CHECKBOX name="pt_checkbox" useLanguages="no">

you have to set the attribute "useLanguages" of the checkbox to the value "yes".

Afterwards you can use the object "language" and give it to the method getOptionModel:

optionModel = optionFactory.getOptionModel(context, language, false);

Regards

Rouven

0 Kudos

Haha, I could't see the forest for the trees 😉

Thanks again!

Steffi

0 Kudos