How to disable Single Field only based the value of the Field

How to disable Single Field only based the value of the Field

ali.mujahidali.mujahid Posts: 8Questions: 2Answers: 0

I am using Datatable Editor with inline configuration and i have different fields. Now I want disable a specific field only based on the value. Field type is Select and i am using select2 plugin for it.
E.g. There a 3 Options in the Select and i want to disable editor for this field if value is "Unknown"
how can i do this. ? i have tried serveral options like PreOpen callback at that i am unable to get which field is currenly going to edit in inline mode of editor

Answers

  • rf1234rf1234 Posts: 3,112Questions: 91Answers: 429

    I think it shouldn't make a difference what editing mode you are using. Here is something I use frequently:

    editor
        .dependent('yourField', function ( val, data, callback ) {
            if ( val === "Unknown" ) {
                this.field('yourField').disable();
            } else {
                this.field('yourField').enable();
            }
            callback({});
        })
    
  • ali.mujahidali.mujahid Posts: 8Questions: 2Answers: 0

    Hi, @rf1234
    I am using select2 plugin for inline edit. this way it doesn't work. Field is still editable. it doesn't stop from editing.

  • allanallan Posts: 64,509Questions: 1Answers: 10,662 Site admin

    Can you post a link to a case case showing to issue so we can properly understand the problem and offer some help?

    Allan

  • ali.mujahidali.mujahid Posts: 8Questions: 2Answers: 0

    here is my Field config
    {
    "type": "select2",
    name: "fieldType",
    "opts": {
    "allowClear": false,
    minimumResultsForSearch: -1,
    dropdownParent: $('.content'),
    },
    options: [
    {label: 'Field1', value: 'Field1'},
    {label: 'Field2', value: 'Field2'}
    ]
    }
    fieldType value can 'Known' so in that case i don't want to edit that field. i am using inline edit style

  • allanallan Posts: 64,509Questions: 1Answers: 10,662 Site admin

    Are you using full row editing, or just individual cells? If individual cells, is the issue not that the field becomes disabled (or not), but rather than you don't want it to enter into the editing state at all? If so, how are you triggering inline editing - are you calling inline()? If that is the case, then you'd need a logic condition before that to check if the field should become editable or not.

    A link to a test case really would help.

    Allan

  • ali.mujahidali.mujahid Posts: 8Questions: 2Answers: 0

    Hi, @allan
    I am using Inline() Api for individual cell editing with Select2 Plugin configuration is
    {
    "type": "select2",
    name: "fieldType",
    "opts": {
    "allowClear": false,
    minimumResultsForSearch: -1,
    dropdownParent: $('.content'),
    },
    options: [
    {label: 'Field1', value: 'Field1'},
    {label: 'Field2', value: 'Field2'}
    ]
    }
    Now for this field there is third value 'Unknown' while table initialized for that case I just want to disable editing for this Field only incase if value is "Unknown" else it should editable with 2 options give. There are more fields with Inline configuration those should be editable.

  • allanallan Posts: 64,509Questions: 1Answers: 10,662 Site admin

    Can you show me how you are calling the inline() method please?

    As I say, a link to a test case would help (a lot).

    Allan

  • ali.mujahidali.mujahid Posts: 8Questions: 2Answers: 0

    :(
    Hi, @allan
    Can you please just guide me how to Stop opening Inline Editor for the Single field only by checking its name and Value!. Where i can check that Inline Editor should enable or not ?
    there is option in PreOpen(). but there i can't find which field it is going to edit !

  • rf1234rf1234 Posts: 3,112Questions: 91Answers: 429

    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    I have been trying to understand what you mean, unsuccessfully so. Please provide a test case. If you can't get the test case working please post it anyway. We might be able to understand your problem a little better - and hopefully help you!

  • allanallan Posts: 64,509Questions: 1Answers: 10,662 Site admin

    displayed() is the method to get displayed fields, but because you are wanting to check in preOpen, the field hasn't been displayed and thus the method will return an empty array.

    You will need to use a different approach, which I alluded to above when I asked for the code for how you are triggering editing. You haven't provided that, so I'm going to make a few assumptions - hopefully it will still apply to your use case.

    What you probably need to do is store which field it is that editing was being triggered on:

    let currentField;
    
    table.on('click', 'tbody td:not(:first-child)', function (e) {
      currentField = table.cell(this).dataSrc();  
      editor.inline(this);
    });
    

    That assumes that columns.data matches the field.name (if not, then the logic would need to be changed).

    Then you can use preOpen to check the value of the disabling field and also what currentField is and return false; if needed.

    It would be easier to simply wrap inline() in an if condition, but I don't think that would be sufficient, since the inline editing action is async if there is already a field being edited.

    Allan

Sign In or Register to comment.