tacowordpress / addbysearch

This package is deprecated. Please use tacowordpress/addmany for newer projects.
1 stars 1 forks source link

Additional buttons like "Show All" #3

Open brianhaveri opened 8 years ago

brianhaveri commented 8 years ago

The "Show All" button works for many scenarios, but it would be nice if we could do something like add a "Show Recent" button, which called a specific static method or function:

'event_ids'=>['type'=>'text', 'class'=>'addbysearch', 'model'=>'Event', 'data-button-show-recent'=>'Show Recent;Event::getRecent']

That might not be the exact syntax, but rather just an indication of the idea.

jasand-pereza commented 8 years ago

I've been meaning to make AddBySearch use AJAX for awhile now and this would be a perfect use case for it. We could allow any number of buttons that would get filtered posts through a method on the server.

I'll look into this. Thanks for request/idea!

On Wed, Jun 8, 2016 at 2:10 PM, Brian Haveri notifications@github.com wrote:

The "Show All" button works for many scenarios, but it would be nice if we could do something like add a "Show Recent" button, which called a specific static method or function:

'event_ids'=>['type'=>'text', 'class'=>'addbysearch', 'model'=>'Event', 'data-button-show-recent'=>'Show Recent;Event::getRecent']

That might not be the exact syntax, but rather just an indication of the idea.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tacowordpress/addbysearch/issues/3, or mute the thread https://github.com/notifications/unsubscribe/ABqcqf0VVebLVoKO0xYY9OfBMXZ4h1IZks5qJyGxgaJpZM4IxWZ4 .

jasand-pereza commented 8 years ago

I created a branch named "filtering-options", downloadable through composer under the package name "dev-filtering-options". Similar to your idea, this utilizes a select menu to give you any number of filtering options. Here is an example of how to set it up using the "getFields" method.

  public function getFields() {
    return array(
      'events' => array(
        'type' => 'select',
        'class' => 'addbysearch',
        'data-post-type' => 'Event',
        'options' => array(
            '\Event::getRecent' => 'Show recent events',
            '\Event::getPast' => 'Show past events'
        )
      )
    );
  } 

Example of a getRecent method

public static function getRecentEvents() {
    $events = Event::getWhere(array(...)); // your filtering criteria

    // IMPORTANT: you must maintain the order of posts
    $pairs = [];
    foreach($events as $e) {
      $pairs[$e->ID] = $e->post_title;
    }
    return $pairs;
  }

There are a few differences. In order to use this interface, you must make the field type "select", and assign options where the key is the namespaced class/method and the value is a descriptive name of what the option is doing.

I haven't throughly tested it, but so far everything is working, including the old way of not defining extra filtering options. I'm also not sure if this will bork any AddBySearch fields you're currently using, so be careful.

brianhaveri commented 8 years ago

Excellent. I'll give it a test run this week, but my initial reaction is that the syntax looks intuitive.