Forums

dynamic dropdown filtering

admin_wiky 18 Sep, 2014
Hi all,

I have a CCv5 list of data and I need them filter by dynamic dropdown. Max helped me with the code of dynamic dropdown menu:

$keys = \GCore\Libs\Arr::getVal(\GCore\Libs\Arr::getVal($rows, explode(".", "[n].zakladni")), explode(".", "[n].filialka_id"));
    $values = \GCore\Libs\Arr::getVal(\GCore\Libs\Arr::getVal($rows, explode(".", "[n].zakladni")), explode(".", "[n].filialka_id"));
    $options = array_combine($keys, $values);
    $field = array 
    (
      'name' => 'fltr[zakladni][filialka_id]',
      'id' => '',
      'options' => 
      array (
      ),
      'empty' => '',
      'values' => 
      array (
      ),
      'label' => 
      array (
        'text' => '',
        'position' => 'left',
      ),
      'sublabel' => '',
      'multiple' => '0',
      'size' => '',
      'class' => '',
      'title' => '',
      'style' => '',
      'params' => '',
      ':data-load-state' => '',
      ':data-tooltip' => '',
      'type' => 'dropdown',
      'container_id' => '0',
    );
    $field["options"] = $options;
    echo \GCore\Helpers\Html::formLine($field["name"], $field);
    echo '<div class="form-group gcore-form-row" id="form-row-1">
            <div class="gcore-input gcore-display-table" id="fin-button3">
              <input name="button3" id="button3" type="submit" value="Filtrovat" class="" style="" data-load-state="" />
            </div>
          </div>';

filtering works fine, data are loaded by filter condiotion. values of dropdown:

Array
(
    [501] => 501
    [502] => 502
    [553] => 553
)

Problem is as u can see that the form hasnt empty option so there isnt way how get back all data in list after using of filter. U cant show all data after filtering.
Max give me an advice to modify the line
$options = array_combine($keys, $values);

to this:
options = array_merge(array('' => ''), array_combine($keys, $values));

after this modification is empty value in dropdown, but filtering doenst work.

Array
(
    [] => 
    [0] => 501
    [1] => 502
    [2] => 553
)

As u can see, problem is in values of keys in array, that has 0..2 but the array should looks like previous array.
Array
(
[] =>
[501] => 501
[502] => 502
[553] => 553
)
any idea what is wrong?
thx
GreyHead 18 Sep, 2014
Hi Homeopat,

Please try adding this line:
 . . .
   $options = array_combine($keys, $values);
   array_unshift($options, array( '' => 'Please select')); // add this line
   $field = array 
. . .

Bob
admin_wiky 18 Sep, 2014
Hi Bob,
same result, doesnt work
Array
(
    [0] => Array
        (
            [] => Please select
        )

    [1] => 501
    [2] => 502
    [3] => 553
)

instead of 1,2,3 I need same value so 501,502,503

thx
GreyHead 18 Sep, 2014
Hi homeopat,

Hmmm . . . then please try this version.
 . . .
   $options = array_combine($keys, $values);
   array_unshift($options, 'Please select' ); // add this line
   $field = array 
. . .

Bob
admin_wiky 18 Sep, 2014
Hi Bob,

it throws this array

Array
(
    [0] => Please select
    [1] => 501
    [2] => 502
    [3] => 553
)

and it is wrong. for example, line [1] => 501 should looks like [501] => 501
If I dont use array_unshift or array_merge(array('' => ''), array_combine($keys, $values)); then format is correct:

Array
(
    [501] => 501
    [502] => 502
    [553] => 553
)


thank you
GreyHead 18 Sep, 2014
Answer
Hi Homepoat,

Bother . . . I think that is because the keys are numeric - so PHP resets them all. Maybe this version . . .
 . . .
   array_unshift($keys, ''); // add this line
   array_unshift($values, 'Please select'); // and this line
   $options = array_combine($keys, $values);
   $field = array 
. . .

Bob
admin_wiky 18 Sep, 2014
thats it .. thank you bob๐Ÿ™‚
admin_wiky 27 Oct, 2014
Hi,

I have one more question. What I have to do If I want use two dropdowns for filtering?
I used previous code, modified name of variables. data are loaded to dropdown, but filtrering doesnt work.
admin_wiky 27 Oct, 2014
solved. mistake in code.
NickOg 25 Jan, 2015
Hi Homeopat

Do you have a copy of Max's example that I can look at please? I am struggling with this and maybe that would give me a push along. ๐Ÿคจ

Thanks

Nick
NickOg 25 Jan, 2015
Hi Homeopat,

Forget that - I cam now follow the code and see that the answer is already there.๐Ÿ˜ถ

Regards
Nick
Mileto 22 Feb, 2015
Hi, I'm new in chronoform and chronoconnectivity.
Can i have the response for this problem? Right code? i have some problem!

ty

Angelo from Italy
๐Ÿ˜ƒ
Max_admin 22 Feb, 2015
Hi Angelo,

As posted by the original author, the solution posted works well, so if you have the same problem then you can use the same solution, or you may let us know the problem you have exactly so we can try to help!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Mileto 22 Feb, 2015
Ok Admin,
ty

i'll use same solution!!

Your tools are awesome!!!
aandree 13 Mar, 2015
Hi,
I have tried this solution and it works pretty well. But only till the moment I use native pagination of the results (_PAGINATOR_LIST_ ; _PAGINATOR_INFO_; _PAGINATOR_NAV_). Then it dynamically shows only values which are on the current page of listing. Is there any possibility to change the dynamic dropdown list to show all the possible values - from the whole listed table?

Thanks in advance!
aandree 13 Mar, 2015
Thanks for the topic homeopat (dรญky๐Ÿ˜€ ), now it works except one detail - I have tried to insert Bob's code (and also tried some modifications) for reseting the filter:
 
. . .
   array_unshift($keys, ''); // add this line
   array_unshift($values, 'Please select'); // and this line
   $options = array_combine($keys, $values);
   $field = array 
. . .

but without any result. Is there any solution how to load the initial list by selecting some default value from the dropdown list?
Max_admin 13 Mar, 2015
You mean to select a value at the dropdown when the page loads ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
aandree 13 Mar, 2015
I will better explain a scenario:

1) I have a full CC listing table without any filter applied and the dynamic dropdown list with loaded IDs from _content table.
2) If I select one ID from the list, and press Submit button, the CC listing table loads only this one record. That's what I wanted.
3) Now I want to go back to the initial state (full CC listing table) by selecting some value from the dropdown list (let's follow Bob's example and the value would be "Please select" and it is on the first place of the dropdown list values). Then after reloading the page (after pressing Submit button) I would like to have back the full CC listing table.
Max_admin 19 Mar, 2015
Hi aandree,

Then you may try to change this line:
$options = array_combine($keys, $values);

to:
$options = array("" => "Please select") + array_combine($keys, $values);


Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.