Making a column editable based on value of another column

QuestionsMaking a column editable based on value of another column
Michael Murphy asked 3 years ago

In this question, OP asks for a way to prevent a row from being edited based on the value of a column. I’m already doing this in my loadComplete function.

Is it possible to just make certain columns in that row non-editable (rather than the entire row) based on the value of a column?

7 Answers
Abu Ghufran Staff answered 3 years ago

You can check ‘read-only based on conditions’ from this documentation section.

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Michael Murphy answered 3 years ago

Hi Abu, thanks for pointing me to that documentation. The JS callback function looks like what I need, but unfortunately, it’s not working for me. The `formid` that is supplied as an argument to the callback function is `undefined`, so I can’t get any data from my row with it. Here’s my code:


//query that gets a list of strings to use in the select dropdown
$result = mysqli_query($con, "select...");
while($row = mysqli_fetch_assoc($result)) {
$select .= $row['item'] . ":" . $row['item'] . ";";
}
$select = ":;" . substr($select,0,-1);

$col = array();
$col["name"] = "my_column";
$col["title"] = "My Column";
$col["width"] = "150";
$col["editable"] = true;
$col["edittype"] = "select";
$col["editoptions"] = array("value"=>"$select");
$col["editrules"] = array("required"=>true, "readonly"=>true, "readonly-when"=>"check_wr");
$cols[] = $col;

...

...
function check_wr(formid) {
console.log(formid); // prints "undefined"
id = jQuery("input[name=idInt]:first, select[name=idInt]:first",formid).val();
id = parseInt(id);
console.log(id); // prints a blank line
return false; // leaving this here so column is editable until problem is sorted
}
...

Any idea what might be the issue here?

Abu Ghufran Staff answered 3 years ago

The formid only comes if you open edit dialog. If you are using inline editing it will be undefined.

In both case, it should work. I see one more difference. You have use :first instead of :last in field selector (input[name=idInt]:first)

The :first could return the autofilter search field element as it has similar name. The :last will return the editing field.

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Michael Murphy answered 3 years ago

Thanks for that clarification. I’m specifically looking to achieve this for inline editing.

In fact, I think I see what the problem is. The column value that I need to filter on is always readonly. So, when I start the inline editing, and the callback function runs, there is no input box for it to read the column value from. I thought about trying to select the readonly cell, but that doesn’t have a unique id/name for me to select.

Any suggestion on what I could do here?

Abu Ghufran Staff answered 3 years ago

In sql select query, select same column again with different alias (idInt_tmp), and map it to a grid column that is hidden + non-readonly, and use that new column in your callback conditions.

Let me know in case of further help.

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Michael Murphy answered 3 years ago

Hi Abu,

Your suggestion worked great. I had thought of doing this, but assumed that since the column was hidden, I would have the same problem of not having an editable input field to select. However, there really is an editable field there, it is just hidden. Thank you for setting me straight!

I also had an issue with using the :last selector in my callback function, as I have two grids on my page with the same column name, so :last was selecting the autofilter search field in the second grid, instead of the editable cell in the first grid.

However I solved this by changing this line:


idInt = jQuery("input[name=idInt_tmp]:last, select[name=idInt_tmp]:last", formid).val();

to:


idInt = jQuery("input[name=idInt_tmp][rowid], select[name=idInt_tmp][rowid]", formid).val();

which works, because the editable cell in the grid has a “rowid” attribute, and the filter field doesn’t. This works for both inline editing and the edit dialog box.

Abu Ghufran Staff answered 3 years ago

That’s great. Glad to know it’s solved now.

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Your Answer

1 + 10 =

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?