Implementing a search form that updates grid or li

2019-09-15 05:51发布

问题:

I am trying to implement a search form which would show results gotten from the db when given (a) keyword(s), then updating a grid or lister with the search results. Only i'm a bit lost as to how to update the grid...

I've looked at some similar questions (ie: How to refresh grid after update from modal form in atk4? ) but I can't seem to get the grid to update after submitting my form. code follows, any tips on what i'm doing wrong, or a better way to do this? later i'd like the search and update several grids/listers from different db tabels, but if I'd get one to work that would already be a start... :)

$f=$this->add('Form');
$f->addField('Search','buscar', 'Enter keywords:');
$f->addSubmit('Search');

$g=$this->add('Grid');
$g->js(true)->addClass('myreload');
$g->js('myreload')->reload();
$g->setSource('conflicto');
$g->addColumn('nombre');        
$g->addColumn('idconflicto');

if($f->isSubmitted())  
{
     $keywords=$f->getElement('buscar')->get();
     $g->dq->where("nombre LIKE '".$keywords."'");
     $this->js(null,$this->js()->_selector('.myreload')->trigger('myreload')) ->univ() ->closeDialog() ->successMessage('Actualizado') ->execute();
}

回答1:

okay, so what you should do is following:

if (isset($_GET["q"])){
    $q = $_GET["q"];
    $g->dq->where("field like '%" . mysql_real_escape_string($q) . "%');
}

if ($f->isSubmitted()){
    $g->js()->reload(null, array("q" => $f->get("q")))->execute();
}

not tested, but should work.



回答2:

Have you tried using the built in quicksearch on the grid ?

You should be able to add the quicksearch to your grid so your page would look something like this

class page_yourpage extends Page {
    function init(){
       parent::init();
       $p=$this;

       $c=$p->add('Grid');
       $c->setSource('conflicto');

       if($c->grid){
          $c->grid->addPaginator(20);
          $c->grid->addQuickSearch(array('nombre'));
        } // end if
     } // end init
} // end class

Note setSource receives the table name, not a model name. The array parameter to the quicksearch identifies which field(s) it will search by.

This results in adding a search field with a magnifying class icon so you enter the search text and it will search the fields specified and update the grid with rows matching that search.

If you want to change the search (e.g. to be case insensitive as it only does exact match as supplied), you can create a new version such as MySearch.php in yoursite/lib which extends QuickSearch and in the init, then override the applyDQ() function and include a strtolower() on the search string entered and LOWER(column) on the mysql table column.

class MySearch extends QuickSearch () {

 function applyDQ($dq){
            if(!($v=$this->get('q')))return;

            $v=strtolower(addslashes($v));  // quote it

            $q=array();
            foreach($this->fields as $field){
                    $q[]="lower($field) like '%".$v."%'";
            }
            if($q){
                    $dq->having(join(' or ',$q));
            } // end if
    } // end function
 } // end class

Then you would need to add it to your grid on your page with this code so it uses your implementation of MySearch instead of the default one.

 $c->grid->addQuickSearch(array('nombre'), 'MySearch');

ATK4 should take care of the grid reload for you based on the search.



回答3:

Your question is using Agile Toolkit 4.1 syntax. In 4.2 you can use this:

$g=$this->add('Grid');
$q=$this->api->dsql()->table('conflicto');

if (isset($_GET["q"])){
    $q->where('field','like', '%'.$q.'%'); 
                              // 3rd argument is escaped automatically
}

$g->setSource($g);

However I must remind you that the best of course is to use $g->setModel($m->addCondition(..))



标签: atk4