Forums

help with javascript

admin_wiky 23 Oct, 2015
Hi all,

I would like to ask you for help with ...
I need dynamicaly show some checkbox elements depending on choose of dropdown menu in my CF. CF can do this between 2 dropdown menus, but not between dropdown menu and checkboxes.
So there is the way in javascript. I found out some JS script which works here, but ... I dont know how to modify this php code representing checkbox group (data from DB)

<?php
$keys = \GCore\Libs\Arr::getVal(\GCore\Libs\Arr::getVal($form->data, explode(".", "vlastnosti")), explode(".", "[n].vlastnost"));
$values = \GCore\Libs\Arr::getVal(\GCore\Libs\Arr::getVal($form->data, explode(".", "vlastnosti")), explode(".", "[n].vlastnost"));
$options = array_combine($keys, $values);
$field = array (
  'name' => 'him[vlastnost][]',
  'id' => 'checkbox_group5',
  'options' => 
  array (
  ),
  'empty' => '',
  'values' => 
  array (
  ),
  'label' => 
  array (
    'text' => 'Vlastnost HIM',
    'position' => 'left',
  ),
  'sublabel' => '',
  'horizontal' => '1',
  'class' => '',
  'title' => '',
  'style' => '',
  'params' => '',
  ':data-load-state' => '',
  ':data-tooltip' => '',
  'ghost' => '0',
  'ghost_value' => '',
  'brackets' => '0',
  'type' => 'checkbox_group',
  'container_id' => '0',
);
$field["options"] = $options;
echo \GCore\Helpers\Html::formLine($field["name"], $field);
?>


to this javascrip - exactly how to modify function createC() or how to get php array to JS array mentioned lower:

<script type="text/JavaScript">
// variable dances is 1D array
var dances = 
  function picked(s)
  {
    if(s==0)
    {
      removeC()
    }
    else if(s<3)
    {
      createC('0')
    }
    else if(s<5)
    {
      createC('124')
    }
    else
    { 
      createC('123456')
    }
  }
  function createC(p)
  {
    removeC();
    var p = p.split('');
    var root = document.getElementById('che');
    for(var i=0;i<p.length;i++)
    {
      var t = dances[Number(p[i])];
      var oInp = document.createElement('input');
      var oTxt = document.createTextNode(t);
      var oBr = document.createElement('br');
      oInp.setAttribute('type','checkbox');
      oInp.setAttribute('name',t);
      oInp.setAttribute('value',t);
      root.appendChild(oInp);
      root.appendChild(oTxt);
      root.appendChild(oBr);
    }
  }
  function removeC()
  {
    var root = document.getElementById('che');
    while(root.hasChildNodes())
    {
      root.removeChild(root.childNodes[0])
    }
  }
  onload = function()
  {
    document.getElementsByName('him[druh]')[0].selectedIndex=0;
  }
</script>

problem is in loading of data from db to JS where did try this php inject to JS:
var dances =<?php $keys = \GCore\Libs\Arr::getVal(\GCore\Libs\Arr::getVal($form->data, explode(".", "vlastnosti")), explode(".", "[n].vlastnost"));echo json_encode($keys); ?>;


thank you for help!
Jozka
GreyHead 25 Oct, 2015
Hi Jozka,

Do you need to show/hide a whole checkbox group (or groups); or do you need to show/hide just some of the checkboxes within a checkbox group??

Bob
admin_wiky 26 Oct, 2015
Hi Bob,
I need to show/hide just some of checkboxes in checkbox group.

thank you
This topic is locked and no more replies can be posted.