Thursday, January 1, 2009

P4A Table, a delete row helper

Sometimes, when I work with P4A, I prefer to allow the deletion of a record with a click on the table row. Look at this screenshot to have an idea:



Doing that with P4A is not hard: you have to add an action col, intercept the afterClick event and then call the db_source deleteRow event. Yet now it's even simpler because we have added the addDeleteRow helper to the P4A core :


$mytable->addDeleteRow();



If you want you can also call the helper with a custom label col and a custom confirmation message:



$mytable->addDeleteRow('Delete Row','Do you want to delete this row?');


You can find this helper in the svn repository or you can copy and past my code in the libraries directory:


<?php
function P4A_Table_addDeleteRow($table$params)  
{      
    if (isset(
$params[0])) {
        
$col_label $params[0];
    } else {
        
$col_label 'Delete';
    }
        
    if (isset(
$params[1])) {
        
$message $params[1];
    } else {
        
$message 'Delete current element';                   
    }

    
$table->addActionCol('delete');
    
$table->cols->delete->setWidth(150)
                        ->
setLabel($col_label)
                        ->
requireConfirmation('onClick'$message);
    
$table->data->intercept($table->cols->delete,'afterClick','deleteRow');

    return 
$table;
}


Happy P4A hacking!