2

If I wanted to update the Elements listing based on an action taken from a plugin, would it be possible to do this?

Setup:

  • We're on an Elements listing page.
  • We filter the results with a search query.
  • We then click an Element's name to move to its individual editing page

The Problem:

Currently in my tests with Google Chrome if I:

  • Filter a list by a search query (say lastName::T* on the Users listing page),
  • Click on one of the usernames
  • Hit "Back"

My search is not saved, i.e., I'm returned to a non-filtered list.

My workaround to this is to write a simple JS plugin that stores your search query - if populated - when you navigate away from the page. I then re-populate the search query when you re-enter the page but I have to tell Craft to execute it, IE to fire updateElements.

My current workaround for this is to fire a change event on the search box if the search query is stored and it's not an Ajax request. It works, it just feels kind of hacky.

So I'm wondering if there's a more native, less hacky solution.

  • Hey Peter, can you provide more context? What page in the CP would this be happening on, what exactly is this “onSourceSelectChange” event you’re referring to, etc. – Brandon Kelly Jul 16 '14 at 22:27
  • Hi Brandon, absolutely. I'll update the question to make it more clear. – Peter Tell Jul 17 '14 at 1:21
  • Sorry Peter, didn’t get a notification that you had responded. (I think SE only gives notifications for comments that include @username, unless the comment is on your own post.) Just provided an answer. – Brandon Kelly Jul 25 '14 at 14:58
  • No problem whatsoever! Thanks for the feedback. – Peter Tell Jul 25 '14 at 15:01
4

The cleanest way to do this would be to provide your own Javascript class that overrides Craft.BaseElementIndex, which stores the latest search value in localStorage:

MyElementTypeIndex = Craft.BaseElementIndex.extend(
{
    onAfterHtmlInit: function()
    {
        // Do we have a stored search value?
        if (this.instanceState.searchVal)
        {
            this.$search.val(this.instanceState.searchVal);
        }

        this.base();
    },

    updateElements: function()
    {
        // Store the current search val in localStorage
        this.setInstanceState('searchVal', this.$search.val());

        this.base();
    }
});

// Register this as the official index class for this element type!
Craft.registerElementIndexClass('MyElementType', MyElementTypeIndex);

Save that file somewhere in your plugin’s resources/ folder (let’s say it’s craft/plugins/myplugin/resources/js/index.js), and then include it from your plugin’s init() function:

class MyPlugin extends BasePlugin
{
    // ...

    public function init()
    {
        if (craft()->request->isCpRequest())
        {
            craft()->templates->includeJsResource('myplugin/js/index.js');
        }
    }
}
| improve this answer | |
  • That's what I was looking for, thanks Brandon! – Peter Tell Jul 25 '14 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.