Editor - row callback disable editor in specific cell

Editor - row callback disable editor in specific cell

dpanscikdpanscik Posts: 202Questions: 47Answers: 0

Hello.

I was wondering if there was a crafty way to disable editor for a specific cell using row callback?

I want to disable the editor for specific cells during certain "if then" logic conditions.

David

Answers

  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954

    The Editor dependent() API can be used to conditionally disable fields.

    Kevin

  • dpanscikdpanscik Posts: 202Questions: 47Answers: 0

    Im not sure how to format this. The following disables the field in all rows. I am trying to disable the field only in certain rows.

                rowCallback: function (row, data, val, meta) {
                    //test to disable editor
                    if (data["PoNumber"] == "12333") {
                        editor.disable(['PoNumber'],$(row));
                    }
                  }
    
  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954
    edited April 2023

    As I suggested before use dependent() to conditionally disabled fields. Here is a simple example that disables the position filed if the value is Director.
    https://live.datatables.net/guwafemu/375/edit

    The disable() API affects the form not the field and only takes one parameter - the field name. So you need to enable or disable when the form is open. Your rowCallback is globally disabling the editor form field PoNumber, not just for that row. You don't have anything to re-enable.

    Kevin

  • dpanscikdpanscik Posts: 202Questions: 47Answers: 0

    That solution works. I was hoping to do this within rowCallback just to keep all my logic in one spot.

    On a side note: Once a decision is made to disable a field, I don't ever want to re-enable that particular field. So missing the "enable" function does not bother me.

  • dpanscikdpanscik Posts: 202Questions: 47Answers: 0

    Here is the next phase of the logic. And perhaps a even bigger reason to do this in rowCallback.

    The logic using PoType does not work (obviously). If I cant do this in rowCallback, I need help to figure out how to do this here...

            editor.dependent('PoNumber', function (val, data, callback, e) {
                return (data["PoType"] ) === "child" ?
                    { disable: 'PoNumber' } :
                    { enable: 'PoNumber' };
            });
    
  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954

    The problem is Editor doesn't keep track of disabled fields on a row by row basis. The dependent() API is used to customize the Editor form when opened. One of the options is to enable/disable fields. If you disable() a field its disable each time the form is opened until you use enable().

    Kevin

  • dpanscikdpanscik Posts: 202Questions: 47Answers: 0

    Here is a solution I pulled together combining the info from this forum thread and another forum thread.

            editor.on('preOpen', function (e, mode, action) {
                if (editor.field('poType').val() == "child") {
                    editor.field('PoNumber').disable();
                } else {
                    editor.field('PoNumber').enable();
                }
            });
    
  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954
    edited April 2023

    The logic using PoType does not work (obviously).

    Take a look at the Submit data section of the dependent() docs. It explains data is more than just the row data. It has different objects rows and values that you can compare to. Maybe you will want to loop through either the rows object or the values object to determine if the filed should be disabled. Something like this:
    https://live.datatables.net/guwafemu/376/edit

    The Position field is disabled for the row with Ashton Cox.

    Kevin

Sign In or Register to comment.