Contents

SearchControl

A SearchControl presents a search interface which then returns a list of results from the server to allow the user to make a selection from very large data sets.

Search controls require that items have attached data as the display method is traditionally a table of results and the columns of data are mapped to the values in the attached data.

SearchControl is abstract and so must be extended in order to be configured correctly. This is because knowledge of what columns to display in the results and how to apply the search cannot be automatically determined.

<?php

namespace Rhubarb\Leaf\Controls\Common\Examples\SelectionControls;

use Rhubarb\Crown\String\StringTools;
use Rhubarb\Leaf\Controls\Common\SelectionControls\SearchControl\SearchControl;

class ExampleSearchControl extends SearchControl
{
    protected function getResultColumns()
    {
        return ["FirstName", "Surname"];
    }

    protected function getCurrentlyAvailableSelectionItems()
    {
        $items = parent::getCurrentlyAvailableSelectionItems();

        $filteredItems = [];

        foreach($items as $item) {
            if (StringTools::contains($item->label, $this->model->searchPhrase, false)) {
                $filteredItems[] = $item;
            }
        }

        return $filteredItems;
    }
}
                                    
<?php

namespace Rhubarb\Leaf\Controls\Common\Examples\SelectionControls;

use Rhubarb\Leaf\Views\View;

class SearchControlExampleView extends View
{
    protected function createSubLeaves()
    {
        $this->registerSubLeaf(
            $control = new ExampleSearchControl("control")
        );

        $control->setSelectionItems(
            [
                [ 99, "John Smith", ["FirstName" => "John", "Surname" => "Smith" ]],
                [ 98, "Jane Doe", ["FirstName" => "Jane", "Surname" => "Doe" ]],
                ExampleContact::all()->addSort("FirstName")
            ]
        );
    }

    protected function printViewContent()
    {
        print $this->leaves["control"];
    }

    public function getDeploymentPackage()
    {
        $package = parent::getDeploymentPackage();
        $package->resourcesToDeploy[] = __DIR__.'/search.css';

        return $package;
    }


}
                                    
.results.drop-down {
    background: white;
    border: 1px solid #999;
}

.results-list tr td {
    padding: 4px;
}

.results-list tr.active {
    background: lightblue;
}
                                    

This basic implementation applies the search by overriding getCurrentlyAvailableSelectionItems() and filtering items that don't contain the search phrase ($this->model->searchPhrase). This would not scale however to large database searches.