Set value of a field in edit form

QuestionsSet value of a field in edit form
Tasagore Seibei asked 1 year ago

Hi

I have a table with tree fields: ‘key’, ‘name’ and ‘value’

In the grid ‘key’ is hidden and is used just to filter the grid from a url variable, example https://mydomain.com/grid.pgp?key=1234 will only shows registers with key=1234, that works fine.

If the user wants to add a new register the dialog only asks form fields ‘name’ and ‘value’, that’s right, but I need also insert the ‘key’ value with a preset value (in this example ‘1234’)

Is there any way to get this behavior?

3 Answers
Best Answer
Tasagore Seibei answered 1 year ago

I’ve found the way, but something does not work ok. This is the code of the first field:
$col = array();
$col[“title”] = “UUID”;
$col[“name”] = “UUID”;
$col[“width”] = “20”;
$col[“editoptions”] = array(“defaultValue”=> $_GET[‘key’]);
$col[“editable”] = true;
$cols[] = $col;

When I add a new record, the UUID value is right:

But when I add the register, the first field is always recorded as NULL:

Any idea?

Tasagore Seibei replied 1 year ago

Sorry, the screenshots:

Add dialog:

https://imgur.com/a/PcKBF1l

and data stored in table:

https://imgur.com/jOiAzxa

Abu Ghufran Staff answered 1 year ago

Hello,

You can try following:

Show a grid that contains all fields. (key, name, value) and you can make key field hidden = true and editable = false; It won’t show key in listing/add/edit screens.

To filter the grid on page load via URL, you can use this: ($g is grid object)

$k = isset($_GET["key"]) ? $_GET["key"] : "0"; 
$g->select_command = "SELECT name, value FROM table1 WHERE key = $k";

Now to insert the key while insertion, You can use on_insert event handler: for e.g.

$e["on_insert"] = array("add_data", null, true);
$g->set_events($e);

function add_data($data) {
    $data["params"]["key"] = $_GET["key"];
}

For more details on grid events, refer this link.

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Abu Ghufran Staff replied 1 year ago

PS: Events are supported in full version if not already using.

Tasagore Seibei answered 1 year ago

Hi Abu Thanks for the answer. The only way I\’ve got this working is creating a new field with autoincrement values (as PK), then all works fine. If I try to use a table with a PK with no autoincrement values it fails, don\’t know why. Anyway it\’s working. Regards

Your Answer

20 + 18 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?