In many cases you want grid to perform 3 basic operations, which are: editing, deleting and adding entries to a table. While this functionality is really easy to achieve by using MVC and entity editor, you may need to know how to perform those actions yourself.

In this demonstration you can see one of the variants how to implement this functionality. This demonstration is limited to only a single page, you might find multi-page implementation a more effective.

You have probably noticed by now, that Agile Toolkit is being smart about tracking changes on forms and if you did change anything then attempt to edit other record, you will be warned about loosing your changes.

Another quality of ATK is that form is going to auto-focus it's first field when loaded.

Add or edit record

Name Surname Gender Edit Delete
tester tester M
<?php
 
// You might need to use $p=$this; if you are inserting this code into page/*.php

// In this example we are attempting to combine a form and a grid element on the
// single page.

$h=$p->add('View_HtmlElement')->setElement('h2')->set('Add or edit record');

// Form and grid will need to communicate to each-other so we better add them
// both to the page.
$f=$p->add('Form');
$g=$p->add('Grid');

// Set source for both grid and form. Both objects are relying no dynamic query
// object, so you might apply additional conditions if you wish to.
$f->setSource('user');
$g->setSource('user');

// Again - adding fields can be done by models, but we do it manually for our
// example. Please try to use Models in your application for higher efficiency.
$f->addField('line','name');
$f->addField('line','surname');
$f->addField('dropdown','gender')
  ->
setValueList(array('M'=>'Male','F'=>'Female'));

// This function performs two actions. First it looks into $_GET['id'] and if
// that is set, it is going to load form data from the database with specified
// id. Secondly, that ID will also be marked as Sticky, which means if you
// are to submit a form, the argument is not going to be lost.
$f->setConditionFromGET('id');

// Below we rely on reloading parts of the page through javascript. Note that
// we either are willing to pass ID or are willing to drop it (in case cancel is clicked)
$f->addSubmit('OK');
$f->addButton('Cancel')->js('click',$f->js()->reload(array('id'=>false)));
if(
$f->isSubmitted()){
  
$f->update();
  
$js=$g->js(null,$f->js()->reload());
  if(
$_GET['id'])
    
$js->atk4_grid('reloadRow',$_GET['id']);
  else
    
$js->reload();
  
$js->execute();
}


$g->addColumn('text','name');
$g->addColumn('text','surname');
$g->addColumn('text','gender');

// Column with type button is going to execute ajax request to the same page
// where grid is and will pass ID through the GET argument same as button's name.
// In our case edit=213 will be added.
$g->addColumn('button','edit');

// if you add column 'delete' then Grid will be capable of deleting entries from the
// database. Note that all the conditions you might have applied on the grid are
// also going to be used for deleting (by cloning dynamic query object) - this is 
// to insure that user is not going to delete some non-relevant data.
$g->addColumn('delete','delete');

if(
$_GET['edit']){
  
$f->js()->reload(array('id'=>$_GET['edit']))->execute();
}


blog comments powered by Disqus