Obtaining form multiplier ID for header and rows after saving

_rasel^
Member | 59
+
0
-

Hello,

I have a form multiplier and in the Ajax action after saving, I want to get the ID to display a delete button. It is currently working, but I know it's not the ideal way because I'm passing the ID to the form using $this->template->id. Here's the code:

public function updFormSucceeded(UI\Form $form, $values) {
	$this->template->id = $this->db->table('table')->insert($values)->id;
	$this->redrawControl("formSnippetArea");
	$this->redrawControl("cpBtnSnippet");
}

And here's how I display the delete button:

{snippet cpBtnSnippet}
	{ifset $id}<a href="{plink del! id => $id}" class="btn btn-default ajax">Vymazať</a>{/ifset}
{/snippet}

However, I'm facing a problem when I want to do this for the rows of the multiplier. I thought it would work like this:

public function updFormSucceeded(UI\Form $form, $values) {
		$data = ['id', ['multiplier' => ['lid' => 55]]];
		$form->setDefaults($data);
		$this->redrawControl("formSnippetArea");
		$this->redrawControl("cpSnippet");
}

However, the lid value doesn't get passed to $_multiplier->components[‚lid‘]->value and it still has the value ''. Where am I making a mistake?

_rasel^
Member | 59
+
0
-

I finally solved it like this (simplified code below). It's certainly not ideal, but it's a workable solution – maybe it will help someone, or start a debate in a different direction.

public function actionModal($id)
	{
		if(isset($id)) {
			$form = $this['cpForm'];
			...
			$data = array_merge($dataH, ['multiplier' => $dataL]);
			$form->setDefaults($data);

			$this->template->id = $id;
			$this->template->mul = $this->db->getRows($id);
		}
	}
public function updCpFormSucceeded(UI\Form $form, $values) {
	$id = (int) $this->getParameter('id');

	if ($this->isAjax()) {
		$this->payload->isModal = true; // don't close the window
	}

	$insId = $this->db->updCp($id, $values); // if id is null then insert, otherwise update and fn() returns inserted id
	if(isset($insId)) {
		$this->template->id = $iuId;
		$this->template->mul = $this->db->getRows($insId); // insId header id
		$this->redrawControl("formSnippetArea");
		$this->redrawControl("cpBtnSnippet");
		$this->redrawControl("cpSnippet");
	}
	$this['dataGrid']->reload();
}
<div n:multiplier="multiplier">
{ifset $mul}
	{php $mid = $mul->fetch()->lid ?? null}
	{ifset $mid}
		<a href="{plink pdf!, l => $mid}" role="button" target="_blank" class="btn btn-sm btn-default">PDF</a>
		<a href="{plink pdf2!, l => $mid}" role="button" target="_blank" class="btn btn-sm btn-default">PDF2</a>
	{/ifset}
{/ifset}
</div>

...

{snippet cpBtnSnippet}
<div class="modal-footer">
	{ifset $id}<a href="{plink del! id => $id}" class="btn btn-default ajax">Delete</a>{/ifset}
</div>
{/snippet}