How change editor`s row field value with function

How change editor`s row field value with function

VitalizVitaliz Posts: 71Questions: 7Answers: 1
edited October 2015 in Editor

Hi, Allan.
I'll tried create additional button in editor's form with my own function. I want change one field in the row which is currently edited. This field don't included in editor form.
I created button with label "Set state" for this task. But after how I add this button, not any buttons are displayed in datatable window.
How I can correct this situation?

editor.buttons( [
    {
        label: 'Save',
        className: 'primary',
        fn: function () {
            this.submit();
        }
    },
    {
        label: 'Set state',
        fn: function () {
        editor
            .edit( row, false );
            .set( 'obr', '1' );
            .submit();
    
        }
    },
    {
        label: 'Close',
        fn: function () {
            this.close();
        }
    }
] );

This question has accepted answers - jump to:

Answers

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1

    Next step I'd try: change "editor" variable to "this" - nothing changed.
    And next more:

    {
            label: Set state',
            fn: function () {
    
                this.edit( row, false ).set( 'obr', '1' ).submit();
                
            }
        },
    

    All buttons are displayed, but button "Set state" do nothing.

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited October 2015

    If I use string

    this.set( 'obr', '1' ).submit();
    

    in my function, value is changed, but in last line in datatable's window.
    So, the next question is:

    How I can set proper row index in this case? Can I use editor.modifier() method?

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    it might be easier to add the field to the Editor field list and set the type to 'hidden'. This keeps it off the form, but allows Editor to access the field.

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited October 2015

    Thanks for your advice, this is exactly what I need.
    Working code is:

    {
            label: 'Set state',
            fn: function () {
                this.field( 'obr').set( '1' );
                this.submit();
                
            }
        },
    

    But if I use one chain:

    this.field( 'obr').set( '1' ).submit();
    

    submit() don't working.

    Why I can't use chain methods in this case?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    The field() method will return a Field instance rather than an Editor instance which is why you can't chain the method like that. There is no Field.prototype.submit method.

    You'd need to use:

    this.field( 'obr').set( '1' );
    this.submit();
    

    Allan

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited October 2015

    Thanks, Allan. Now all clear for me. These two lines of code use two different this.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    No - they are identical - they are the Editor instance. For example the above is the same as:

    var editor = this;
    editor.field( 'obr').set( '1' );
    editor.submit();
    

    The key is what the functions return. field() will return a Field instance which is not an Editor instance!

    Allan

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited October 2015

    Thanks for replay, Allan. I'm sorry for the misunderstanding due to my poor English;(.
    I was referring to the fact that these lines are different/incompatible because of the it's returning value. That is exactly what you wrote. Thank you.

This discussion has been closed.