django-jet icon indicating copy to clipboard operation
django-jet copied to clipboard

datetime picker does not work correctly when used in inlines (tabular or stacked)

Open carlosfvieira opened this issue 8 years ago • 26 comments

image

for the existing records, datetime picker works well, but if add another row, the datetime picker does not work correctly -> it shows Ok, but if clicked on the field or on the buttons, the datetime picker window does not show up.

I've tried to initialize datetime picker using livequery jquery plugin, but still does not work...

carlosfvieira avatar Jun 01 '16 10:06 carlosfvieira

Hey, any ideas? Thanks!

carlosfvieira avatar Jun 14 '16 23:06 carlosfvieira

Are any errors showing up in Chrome Dev Console?

JensAstrup avatar Jun 14 '16 23:06 JensAstrup

Hi @JensAstrup , no errors! It's simply silent... either clicking on date or time inputs or date or time buttons, nothing happens, and no error is shown!

Any idea of what's happening?

carlosfvieira avatar Jun 15 '16 08:06 carlosfvieira

I hadn't noticed this issue before, but just tried it myself and ran into the same problem. I'm going to do some digging - will let you know if I find anything.

JensAstrup avatar Jun 15 '16 12:06 JensAstrup

Thanks @JensAstrup !

carlosfvieira avatar Jun 15 '16 12:06 carlosfvieira

Well, I have an idea of where the problem is coming from - but it will take some more digging around to figure out the solution.

I believe the Django datepicker needs to be initialised after clicking 'Add Another ...' - the question is what function to call and in what file. The relevant javascript files are in django-jet/jet/static/admin/js/ if you want to take a look around. There may also be some clues in the default django admin files as they are using the same datepicker, but without the same issue.

JensAstrup avatar Jun 15 '16 13:06 JensAstrup

I've already tried that, using a solution like this (using jquery livequery plugin):

$(".datepicker").livequery(
        function(){ 
            // This function is called when a new object is found. 
            $(this).datepicker({ ...}});
        }, 
        function() { 
            // This function is called when an existing item is being removed. I don't think you need this one so just leave it as an empty function.
        }
); 

but, no luck :\

carlosfvieira avatar Jun 15 '16 13:06 carlosfvieira

Do you get any results just using $(".datepicker")? When I use that class I get nothing, may need a different selector?

JensAstrup avatar Jun 15 '16 13:06 JensAstrup

.datepicker here was just an example of a selector... what i do is, using the correct selector for date and time (vDateField and vTimeField), initialize datepicker and timepicker, like is being done e initDateWidget function, in main.js (or main.min.js)

still can't get it to work...i can get it enter the event when field is created (using the livequery plugin), but still can't make it to work...

carlosfvieira avatar Jun 15 '16 14:06 carlosfvieira

Hi! Any updates about this issue?

Thanks

fproldan avatar Dec 28 '16 17:12 fproldan

This issue is still present, and the only way to "fix" it is by adding extra = 1 to the inlines that use datepicker.

pabgn avatar Jan 20 '17 09:01 pabgn

Hey everyone,

This is the code I'm using to fix this issue.

On my ModelAdmin:

    class Media:
        js = ('/static/admin/js/fix_calendar.js',)

In fix_calendar.js:

function djangoDateTimeFormatToJs(format) {
        return format.toLowerCase().replace(/%\w/g, function(format) {
            format = format.replace(/%/,"");
            return format + format;
        });
}

django.jQuery(document).on('click', '.inline-navigation-item, .add-row', function(){
        jet.jQuery(".vDateField").removeClass('hasDatepicker').datepicker({
                showButtonPanel: true,
                nextText: '',
                prevText: '',
                dateFormat: djangoDateTimeFormatToJs(DATE_FORMAT)
            });
    });

pabgn avatar Jan 29 '17 12:01 pabgn

@pabgn hey, can you make a PR for fix this error? @f1nality Can you fix this problem?

SalahAdDin avatar Feb 25 '17 23:02 SalahAdDin

Please come to the django-jet Discord server so we can organize if you like:

  • https://discord.gg/3yr5yeH

Welcome! 😄

Ismael-VC avatar Aug 15 '17 15:08 Ismael-VC

@carlosfvieira @pabgn What's about the last version? Does it fixed this problem?

SalahAdDin avatar Aug 25 '17 18:08 SalahAdDin

@SalahAdDin , i did not test it on last version

carlosfvieira avatar Aug 28 '17 07:08 carlosfvieira

@carlosfvieira Did you try put these code in the javascript bundle?

SalahAdDin avatar Aug 28 '17 11:08 SalahAdDin

@SalahAdDin , nope :\

carlosfvieira avatar Aug 28 '17 13:08 carlosfvieira

@carlosfvieira Why? If you can't do it i can do it but i don't know how can i test it.

SalahAdDin avatar Aug 28 '17 16:08 SalahAdDin

@carlosfvieira Hi, how did you come up with this datetimepicker? What module is it?

denny-sam avatar Sep 11 '18 05:09 denny-sam

Any new solution is presented ?

AhmadEbeid avatar Oct 06 '20 11:10 AhmadEbeid

@AhmadEbeid THis package was deprecated.

SalahAdDin avatar Oct 06 '20 15:10 SalahAdDin

Hi, any solution so far? I've tried above mentioned JS change, but couldn't make it work.

khanchi97 avatar Dec 12 '22 18:12 khanchi97

Hi, any solution so far? I've tried above mentioned JS change, but couldn't make it work.

I think it won't be any solution here but you can try on django-jet 4 repo.

fgplastina avatar Jan 10 '23 14:01 fgplastina

Thanks, @fgplastina, but I forgot to update, we made it work using JS, which adds an event listener for click and initializes the timepicker ("timepicker" because it was only the time field in our case)

window.addEventListener("load", function () {
  document
    .querySelectorAll("a.add-handler.djn-add-handler")
    ?.forEach((element) => {
      element.addEventListener("click", function () {
        jQuery(".vTimeField").removeClass("hasTimepicker").timepicker();
      });
    });
}

khanchi97 avatar Jan 11 '23 07:01 khanchi97

@khanchi97 thanks for your reply, this may help a lot!!

fgplastina avatar Jan 12 '23 12:01 fgplastina