Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

Hi @jcastroa87 , #1017

Closed AlonMoshe closed 2 months ago

AlonMoshe commented 3 months ago

Hi @jcastroa87 ,

I am working on my project and I followed your suggestion. However, the field that I need to change is a date time, so I need to wait for the user to fish selecting the date and time.

The problem I am having with:

crud.field("project_id")
    .onChange(function (field) {................

Is that it executes for every change the user makes. Which is not the behaviour that I need. I need the Ajax call to be done after the user has finished entering the date and time

Since in the documention it is written that it is possible to use plain JavaScript and JQuery, I am trying the following:

  1. In the setupCreateOperation I added :

    Widget::add([
            'type' =>'script',
            'content'=> 'assets/js/admin/forms/myfile.js',
        ]);
  2. In the JS file I have:

var formId=document.forms[0];
var startTimeElement=formId.querySelector('input[name="startTime"]');
     startTimeElement.addEventListener('blur',handleBlur);

function handleBlur(event){
 // Ajax call 
alert('This is a test')
}

Unfortunately, so far the "handleBlur" function doesn't even get called, I do not know why.

I will appreciate it if you have any suggestions/observations.

Once again, many thanks.

Alon.

Originally posted by @AlonMoshe in https://github.com/Laravel-Backpack/community-forum/discussions/1005#discussioncomment-9718327

jcastroa87 commented 3 months ago

Hello @AlonMoshe

I made a test. Is working in my side:

I use our demo:

Screenshot 2024-06-10 at 11 55 17 AM

Then add the script:

Screenshot 2024-06-10 at 11 56 14 AM

And there is the event:

Screenshot 2024-06-10 at 11 56 57 AM

Cheers.