ListOptions_Rendered and Row_CustomAction are very useful events in PHPMaker.
Below is an example of carrying out a custom action that brings up a user input field that passes a value to the Custom Action. It uses the preConfirm
function of Sweetalerts 2 to capture the value(s) entered and passes it to the server. The value(s) are captured using $_POST on the server side
- List page-> ListOptions_Load
$opt = &$this->ListOptions->Add("my_menu"); // Add Custom Link
$opt->OnLeft = TRUE; // Link on left
- List page-> ListOptions_Rendered
$this->ListOptions->Items["my_menu"]->Body ="<div class='sub-button shadow'>
<a class='btn-success btn-fab' id='restock' value=".urlencode($this->description->CurrentValue)." href='#' onclick=\" Swal.fire({
title: 'RESTOCK',
text: '".$this->description->CurrentValue."',
input: 'number',
inputPlaceholder: 'How many?',
showCancelButton: true,
confirmButtonText: 'Restock',
showLoaderOnConfirm: true,
preConfirm: (pqty) => {
ew.submitAction(event, {f:'".CurrentTableName()."', action: 'myupdate', method: 'ajax', data: { qty: pqty}, key: " . $this->KeyToJson(true) . ",
success: refreshTable})
}
});\"'>
<i class='bi bi-box-seam'></i>
</a>
</div>";
- List page-> Row_CustomAction
function Row_CustomAction($action, $row)
{
$qty = $_POST["qty"];
// Return false to abort
if ($action == "myupdate") { // Check action name
$rsnew = ["in_stock" => ($row["in_stock"]+$qty)];// Array of field(s) to be updated
$result = $this->update($rsnew, "id = " . $row["id"]); // Update the current record only
if (!$result) { // Failure
$this->setFailureMessage("Action failed on " . $row["description"]);
return FALSE; // Abort and rollback
} else if ($this->SelectedIndex == $this->SelectedCount) { // Last row
$this->setSuccessMessage("Action on ".$row["description"].", succeeded.");
}
return TRUE;
}
}