How to clear select menu before update

How to clear select menu before update

CryptographCryptograph Posts: 3Questions: 1Answers: 0
edited May 2018 in Free community support

Hello, I'm using refresh button for datatable editor like below codes

    {
        text: '<span class="fa fa-refresh"></span>',
        editor: editor,
        action: function ( e, dt, node, config ) {
            dt.ajax.reload();
            $.getJSON('ajaxurl.php', function (data) {

                $.each(data, function (index, element) {
                    categories_select_menu.push({
                        value: element.id,
                        label: element.kategori_ismi
                    });
                    //console.log(element.kategori_ismi);
                });
                editor.field( 'ust_kategori' ).val(''); //Not Working
                editor.field( 'ust_kategori' ).update( categories_select_menu );

            });
        }
    }

I update select menu options this command "editor.field( 'ust_kategori' ).update( categories_select_menu );" but options are repeating. How can I clear select menu options before update command

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Cryptograph ,

    You can use the clear() call - so replace

    editor.field( 'ust_kategori' ).val('');
    

    with

    editor.field( 'ust_kategori' ).clear();
    

    Cheers,

    Colin

  • CryptographCryptograph Posts: 3Questions: 1Answers: 0

    I try clear() function but not working. I get an error when I use clear()

    TypeError: editor.field(...).clear is not a function

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Sorry, reading the pages here myself. Instead of the update(), could you just set the value, which would erase the old value, with field().val() or field().set()?

  • CryptographCryptograph Posts: 3Questions: 1Answers: 0
    edited May 2018

    I noticed the problem. The problem does not arise from these commands "field.value() or field.set() or field.update()".

    I have defined before the categories_select_menu array variable. The same values ​​were repeated because each refresh operation added to the end of the array variable.

    I solved the problem by redefining the categories_select_menu array variable

                {
                    text: '<span class="fa fa-refresh"></span>',
                    editor: editor,
                    action: function ( e, dt, node, config ) {
                        dt.ajax.reload();
                        $.getJSON('{$ajax_option_url}', function (data) {
    
                            categories_select_menu = []; //I defined again array variable
                            $.each(data, function (index, element) {
                                categories_select_menu.push({
                                    value: element.id,
                                    label: element.kategori_ismi
                                });
                                //console.log(element.kategori_ismi);
                            });
                            editor.field( 'ust_kategori' ).update(categories_select_menu);
    
                        });
                    }
                }
    
This discussion has been closed.