Default values based on filter when adding records

p38

Active Member
Hi,

Is it possible to populate the elements with the values selected in a dropdown filter when adding new records?

This will speed up data entry tremendously.

In other words, if I filter using a drop down on the element "Category" = 'Gold Medal", then if I add new records, the element "Category" will already be populated with the filter value?

Paul
 
Hi
This is possible. Edit your element, set 'Eval' to yes, and in its default value textarea you can currently add this code:

PHP:
        $elementId = 8; // REPLACE 8 WITH YOUR ELEMENT'S ID

        $app = JFactory::getApplication();
        $pluginManager = FabrikWorker::getPluginManager();
        $model = $pluginManager->getElementPlugin($elementId);
        $listModel = $model->getListModel();
        $listid = $listModel->getId();
        $key = 'com_fabrik.list' . $listid . '_com_fabrik_' . $listid . '.filter';
        $filters = JArrayHelper::fromObject($app->getUserState($key));
        $index = array_search($elementId, $filters['elementid']);
        $value = $filters['value'][$index];
        return $value;

This retrieves the filters from the current session, finds out which index contains the current element ids value and then returns it

OR I've added a helper in the latest commit, to do this for you, so instead all that code can be replaced with:

PHP:
return FabrikHelperElement::filterValue(8);

Again replace 8 with your element's id.
 
Hi
This is possible. Edit your element, set 'Eval' to yes, and in its default value textarea you can currently add this code:

PHP:
        $elementId = 8; // REPLACE 8 WITH YOUR ELEMENT'S ID

        $app = JFactory::getApplication();
        $pluginManager = FabrikWorker::getPluginManager();
        $model = $pluginManager->getElementPlugin($elementId);
        $listModel = $model->getListModel();
        $listid = $listModel->getId();
        $key = 'com_fabrik.list' . $listid . '_com_fabrik_' . $listid . '.filter';
        $filters = JArrayHelper::fromObject($app->getUserState($key));
        $index = array_search($elementId, $filters['elementid']);
        $value = $filters['value'][$index];
        return $value;

This retrieves the filters from the current session, finds out which index contains the current element ids value and then returns it

OR I've added a helper in the latest commit, to do this for you, so instead all that code can be replaced with:

PHP:
return FabrikHelperElement::filterValue(8);

Again replace 8 with your element's id.
 
Absolutely brilliant, well done Rob, thanks, works perfectly.

This is a gem of an item, and hopefully many will pick this up and use it, makes data capture much much quicker.

Once again thanks

Paul
 
Hi Rob, please can you look at using this with inline edits, cause if I try and use this with inline edit, I get an "INTERNAL SERVER ERROR" message.

If I press cancel, the element value is still saved in the record, but I have to refresh the list to show the new value.

This all works using the form edit procedure.

Paul.
 
hi
I did a quick test and couldn't replicate the error. When does it occur for you? What element are editing with the inline-edit plugin?

thx
Rob
 
You can try {scores___tournament}

I have switched it off for now, but you can simply login and switch on for testing.

Just to be sure, this is the return code I am using, is this correct?

return FabrikHelperElement::filterValue(290);

It also give to different error messages depending on filtered or not.

If not filtered, it gives a "null" popup.

If filtered, it give INTERNAL SERVER ERROR

Paul
 
ok thanks I can replicate this one now... working on it.... will post back as soon as I know what's up
 
yeah really really not sure what was going on or why, but I've added in some duct tape to the code which stops it from setting the http 500 error.

I've put the changes on your site and in github

-Rob
 
Hi Rob, further to this, how does one get the underlying value for a database join? It seems this method actually returns the label, and I also am looking for the value associated.

Paul.
 
How is your join element and the filter set up?

The way it works is kinda how I would expect it to. If the element is a join, filterValue() will return the 'value' if it's a direct match which can only produced one result. For instance, if you have your filter as a 'dropdown', in which case you are filtering dwn to a single, specific row. So we can provide the FK (foreign key) value.

But if you've set your filter to be a 'field', and search on 'foo', which could produce any number of matches ... there's no way we can provide a single "value". Your filter may have match 10 records which match on 'foobar' 'foo 123', 'John Foo', etc. So we can't give you a single matching key value.

This behavior is actually not ideal, as you don't really know if you are getting a value or a label.

Rob?

-- hugh
 
Yes, I am using a databasejoin, with a label set to a different element as the value.

Problem is, one needs to get hold of the value, and not the label when working further with data, as the value is representative of the true link to the joined database.
 
Right, but as per my question, how is your filter set up? Is it a dropdown, or a field, or an auto-complete? Exact match?

As explained above, if the filter you applied to that element can, with 100% certainty, be refined to a single row, then you will already be getting a value, not a label, from filterValue(). You'll only be getting a "label" if we can't know if that filter was for a single row, or could potentially match multiple rows. If it could have matched multiple rows, we can't give you a value, period. And it shouldn't actually be a "Label", it'll actually just be whatever text they typed in the filter field.

Think of this situation. You are filtering on "States", and you have a filter of 'field' type on your States join element, and someone types "Ala". That could match "Alabama" or "Alaska", so we can't give you a value. All we can do is tell you they typed "Ala".

And that's the reason I said the behavior is inconsistent. If you knew you were always going to get label text (full or partial), you could do your own lookup on the target table, and process it however you need to. But as you don't know for sure which you are going to get, that makes things a little harder. Well, I think you can be sure in some contexts. So far the only truly inconsistent one I can find is 'auto complete' filters, depending on whether the user took one of the suggestions from the auto complete menu, or submitted it with just what they typed.

So ... answer my question about how the filter is set up - probably easiest to upload a screenshot - and I'll do some more testing for ya.

-- hugh
 
sorry,

  • filter is a dropdown.
  • element is a database join.
  • value is an ID, primary key
  • label is description field.
stock standard lookup facility.

using this code
Code:
return FabrikHelperElement::filterValue(290);
in the eval'ed default field, returns the label value which is from the description field.

It should return the value from the id field, as this is the whole point of a default value., and also the element is defined as an integer, so it cannot save a label value as the default

so I suggest 2 functions........

Code:
FabrikHelperElement::filterValue(290);
will return the actual value
Code:
FabrikHelperElement::filterLabel(290);
will return the label value

paul
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top