How to Edit a Record that belongs to a generated table from a Custom File (v2021)

Tips submitted by PHPMaker users
Post Reply
mobhar
User
Posts: 11660

How to Edit a Record that belongs to a generated table from a Custom File (v2021)

Post by mobhar »

This following code will show you how to add a new button in a Custom File, and when this button is clicked by end-users, then a new Modal dialog will be shown that contains an Edit page to display the record which has OrderID = 11076. By using this technique, then we are able to edit the certain record based on the certain table that will be generated its Edit page from PHPMaker v2021.

To implement it easily, then let's use demo2021 project file that you may download and test in your localhost, from this link: https://phpmaker.dev/demo2021.zip.

After opening this project using PHPMaker v2021, then from News custom file, and just change the code from Content section to this following one:

<div class="card">
	<div class="card-header">
		<h5 class="m-0">Latest News</h5>
	</div>
	<div class="card-body">
		<h6 class="card-title">2020/9/3 - PHPMaker 2021 Released</h6>
		<p class="card-text">For more information, please visit PHPMaker website.</p>
		<a href="https://phpmaker.dev" class="btn btn-primary">Go to PHPMaker website</a>
	</div>
</div>

<?php if (IsAdmin()) { ?>
<script>
    if (!ew.vars.tables.orders) ew.vars.tables.orders = <?= JsonEncode(GetClientVar("tables", "orders")) ?>;
</script>
<a class='btn btn-primary ew-row-link ew-edit' data-caption='Edit' href="#" onclick="return ew.modalDialogShow({lnk: this, btn: 'SaveBtn', url:' ordersedit/11076'});">Edit My Order</a><br><br>
<?php } ?>

<?php CurrentPage()->showMessage(); ?>

As you can see from the code above, we define the business-logic: only admin user that will have the ability to display this button, therefore can edit order record with OrderID = 11076.

In addition, we have to add the Javascript code in order to define the related table variables in client side. This is very important, especially if the form contains the date and/or datetime fields with DateTimePicker extension. If we did not add this Javascript code, then the calendar button will not be shown properly in the related fields. So, make sure you do not forget to add it.


Post Reply