[Yii2] Kartik Select2 - intercept no result when user typing

Hi all, i have a Select2 Kartik Widget and i want that when the user insert a string that don’t find anything in the select, i intercept that moment and i want that appears a button “INSERT NEW ARTICLE”

Can someone give me any help?

Thanks’1000

this is my widget:


        echo Select2::widget([

            'name' => 'artsel',

            'data' => ArrayHelper::map(\app\models\Art::findBySql('SELECT DISTINCT descr as key, descr as value FROM Art ORDER BY key')->asArray()->all(), 'key', 'value'),

            'options' => [

                'placeholder' => 'Select Article ...',

            ],

            'pluginEvents' => [

                "change" => "function() {

                $.post( \"".Yii::$app->urlManager->createUrl('stock/artcheck')."\", {artsel:$(this).val()}, function( data ) {

                    $( \".view-art\" ).html( data );

                    $( \".view-art-spec\" ).empty();

                    });

                $(\"#stock-qta\").focus();

                }",

            ],

        ]);

At the moment, waiting a better solution, i do in this way adding a new javascript:




// execute this function after other user-event

function postpone( fun )

{

    window.setTimeout(fun,0);

}




$(document).keypress(function(e) {

        postpone(function(){

             if ($('.select2-results__option').text() == '*NO RESULT*') {

                *MY ACTION*;

             }

        });




});



[color="#006400"]/* moved to Extensions */[/color]

Though there are many ways you can do this… one direct simple way is to use the tags feature of the plugin. Check the select2 widget tagging demo. You can add values on the fly that are not originally part of the dropdown list (this is true for both multiple select as well as a single select). You can then write your own javascript ajax function on CHANGE event of the input to populate these in a db table.