Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2023-08

bootstrap 5 modal: JQuery(...).modal is not a function

13 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

Hello,

since I need to show/hide a bootstrap 5 modal, I thought I could use the TBootstrapModal defined in bootstrapwidgets, however it gives the exception in the subject: "JQuery(...).modal is not a function. This comes from ApplyWidgetSettings:

JQuery(aElement).modal(...

since TBootstrap is defined as a class helper for TJQuery.

I'm not an expert in web technologies, but I guess this is due to the fact that libbootstrap is for bootstrap 4 and I'm using bootstrap 5.

For the time being I'm using a very minimal wrapper for the modal (since I only need to show/hide it from my application):

type
   TBootstrapModal = class external name 'bootstrap.Modal' (TJSObject)
   public
     constructor new(element: TJSObject{; options: JSValue});
     procedure hide;
     procedure show;
   end;

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 13 lines
Hello, since I need to show/hide a bootstrap 5 modal, I thought I could use the TBootstrapModal defined in bootstrapwidgets, however it gives the exception in the subject: "JQuery(...).modal is not a function. This comes from ApplyWidgetSettings: JQuery(aElement).modal(... since TBootstrap is defined as a class helper for TJQuery. I'm not an expert in web technologies, but I guess this is due to the fact that libbootstrap is for bootstrap 4 and I'm using bootstrap 5.

It also works with bootstrap 5, but you must simply include jquery manually.

But having a libbootstrap5 geared towards using bootstrap without jquery would indeed be a good idea.

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 11/8/23 a les 11:53, Michael Van Canneyt via Pas2js ha escrit:

Show quoted text 20 lines
On Fri, 11 Aug 2023, Luca Olivetti via Pas2js wrote: > Hello, > > since I need to show/hide a bootstrap 5 modal, I thought I could use > the TBootstrapModal defined in bootstrapwidgets, however it gives the > exception in the subject: "JQuery(...).modal is not a function. > This comes from ApplyWidgetSettings: > > JQuery(aElement).modal(... > > since TBootstrap is defined as a class helper for TJQuery. > > I'm not an expert in web technologies, but I guess this is due to the > fact that libbootstrap is for bootstrap 4 and I'm using bootstrap 5. It also works with bootstrap 5, but you must simply include jquery manually.

Do you mean I have to include some other unit (which?) or import jquery with a <script> tag (which I did)?

Bye

Lucs

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 25 lines
El 11/8/23 a les 11:53, Michael Van Canneyt via Pas2js ha escrit: > > > On Fri, 11 Aug 2023, Luca Olivetti via Pas2js wrote: > >> Hello, >> >> since I need to show/hide a bootstrap 5 modal, I thought I could use the >> TBootstrapModal defined in bootstrapwidgets, however it gives the >> exception in the subject: "JQuery(...).modal is not a function. >> This comes from ApplyWidgetSettings: >> >> JQuery(aElement).modal(... >> >> since TBootstrap is defined as a class helper for TJQuery. >> >> I'm not an expert in web technologies, but I guess this is due to the fact >> that libbootstrap is for bootstrap 4 and I'm using bootstrap 5. > > It also works with bootstrap 5, but you must simply include jquery > manually. Do you mean I have to include some other unit (which?) or import jquery with a <script> tag (which I did)?

The latter: Import jquery with a tag, make sure it is before the bootstrap js. (bootstrap 5 tries to detect the presence of jquery, so jquery must be first)

I use bootstrap 5 with the libbootstrap in several commercial projects, so I am quite sure it works :-)

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 11/8/23 a les 13:01, Michael Van Canneyt via Pas2js ha escrit:

Show quoted text 8 lines
The latter: Import jquery with a tag, make sure it is before the bootstrap js. (bootstrap 5 tries to detect the presence of jquery, so jquery must be first) I use bootstrap 5 with the libbootstrap in several commercial projects, so I am quite sure it works :-)

Well, I must be doing something wrong since it gives me that exception here. Maybe I should delay the rtl.run() ("Run RTL when all page resources are fully loaded")?

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 14 lines
El 11/8/23 a les 13:01, Michael Van Canneyt via Pas2js ha escrit: > > The latter: Import jquery with a tag, make sure it is before the bootstrap > js. > (bootstrap 5 tries to detect the presence of jquery, so jquery must be > first) > > I use bootstrap 5 with the libbootstrap in several commercial projects, so > I am quite sure it works :-) Well, I must be doing something wrong since it gives me that exception here. Maybe I should delay the rtl.run() ("Run RTL when all page resources are fully loaded")?

Maybe. It depends on where you set the script elements. If they are in the <head></head> then it should not be necessary.

Michael.

Reply by email

Luca Olivetti
to Luca Olivetti: El 11/8/23 a les 13:01, Michael Van Canneyt via Pas2js ha escrit: Well, I must be doing…

El 11/8/23 a les 13:17, Luca Olivetti via Pas2js ha escrit:

Show quoted text 14 lines
El 11/8/23 a les 13:01, Michael Van Canneyt via Pas2js ha escrit: > > The latter: Import jquery with a tag, make sure it is before the > bootstrap js. > (bootstrap 5 tries to detect the presence of jquery, so jquery must be > first) > > I use bootstrap 5 with the libbootstrap in several commercial > projects, so I am quite sure it works :-) Well, I must be doing something wrong since it gives me that exception here. Maybe I should delay the rtl.run() ("Run RTL when all page resources are fully loaded")?

That was it-

Bye

Luca

Reply by email

Luca Olivetti
to Michael Van Canneyt: Maybe. It depends on where you set the script elements. If they are in the <head></head>…

El 11/8/23 a les 13:32, Michael Van Canneyt ha escrit:

Show quoted text 21 lines
On Fri, 11 Aug 2023, Luca Olivetti via Pas2js wrote: > El 11/8/23 a les 13:01, Michael Van Canneyt via Pas2js ha escrit: > >> >> The latter: Import jquery with a tag, make sure it is before the >> bootstrap js. >> (bootstrap 5 tries to detect the presence of jquery, so jquery must >> be first) >> >> I use bootstrap 5 with the libbootstrap in several commercial >> projects, so I am quite sure it works :-) > > Well, I must be doing something wrong since it gives me that exception > here. Maybe I should delay the rtl.run() ("Run RTL when all page > resources are fully loaded")? Maybe. It depends on where you set the script elements. If they are in the <head></head> then it should not be necessary.

Yes, they are in <head> and apparently it is necessary. OTOH, even if it now works, I introduced a dependency on jquery that I don't strictly need.

Bye

Luca

Reply by email

Luca Olivetti
to Luca Olivetti

El 11/8/23 a les 13:37, Luca Olivetti via Pas2js ha escrit:

Show quoted text 26 lines
El 11/8/23 a les 13:32, Michael Van Canneyt ha escrit: > > > On Fri, 11 Aug 2023, Luca Olivetti via Pas2js wrote: > >> El 11/8/23 a les 13:01, Michael Van Canneyt via Pas2js ha escrit: >> >>> >>> The latter: Import jquery with a tag, make sure it is before the >>> bootstrap js. >>> (bootstrap 5 tries to detect the presence of jquery, so jquery must >>> be first) >>> >>> I use bootstrap 5 with the libbootstrap in several commercial >>> projects, so I am quite sure it works :-) >> >> Well, I must be doing something wrong since it gives me that >> exception here. Maybe I should delay the rtl.run() ("Run RTL when all >> page resources are fully loaded")? > > Maybe. It depends on where you set the script elements. If they are in > the <head></head> then it should not be necessary. Yes, they are in <head> and apparently it is necessary. OTOH, even if it now works, I introduced a dependency on jquery that I don't strictly need.

BTW, the "Backdrop" property is defined as boolean, however it has to be the text 'static' (according to the documentation even in bootstrap 4). I did this

--- a/packages/bootstrap/bootstrapwidgets.pp
+++ b/packages/bootstrap/bootstrapwidgets.pp
@@ -847,12 +847,19 @@ end;
  procedure TBootstrapModal.ApplyWidgetSettings(aElement: TJSHTMLElement);
  begin

- JQuery(aElement).modal(New([
- 'backdrop', BackDrop,
- 'keyboard', keyboard,
- 'focus',Focus,
- 'show',ShowOnRender
- ]));
+ if BackDrop then
+ JQuery(aElement).modal(New([
+ 'backdrop', 'static',
+ 'keyboard', keyboard,
+ 'focus',Focus,
+ 'show',ShowOnRender
+ ]))
+ else
+ JQuery(aElement).modal(New([
+ 'keyboard', keyboard,
+ 'focus',Focus,
+ 'show',ShowOnRender
+ ]));

    jQuery(aElement).on_('hidden.bs.modal', at BootstrapHide);
  end;

but I'm not sure if the backdrop property was intended to work the other way round.

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 31 lines
El 11/8/23 a les 13:37, Luca Olivetti via Pas2js ha escrit: > El 11/8/23 a les 13:32, Michael Van Canneyt ha escrit: >> >> >> On Fri, 11 Aug 2023, Luca Olivetti via Pas2js wrote: >> >>> El 11/8/23 a les 13:01, Michael Van Canneyt via Pas2js ha escrit: >>> >>>> >>>> The latter: Import jquery with a tag, make sure it is before the >>>> bootstrap js. >>>> (bootstrap 5 tries to detect the presence of jquery, so jquery must be >>>> first) >>>> >>>> I use bootstrap 5 with the libbootstrap in several commercial projects, >>>> so I am quite sure it works :-) >>> >>> Well, I must be doing something wrong since it gives me that exception >>> here. Maybe I should delay the rtl.run() ("Run RTL when all page >>> resources are fully loaded")? >> >> Maybe. It depends on where you set the script elements. If they are in the >> <head></head> then it should not be necessary. > > Yes, they are in <head> and apparently it is necessary. > OTOH, even if it now works, I introduced a dependency on jquery that I > don't strictly need. BTW, the "Backdrop" property is defined as boolean, however it has to be the text 'static' (according to the documentation even in bootstrap 4). I did this

If you look at the documentation:

https://getbootstrap.com/docs/5.0/components/modal/#options

In the table under 'options' you'll see it is a boolean or 'static'. I'll see about adding static.

Michael.

Reply by email

Michael Van Canneyt
to Luca Olivetti: El 11/8/23 a les 13:32, Michael Van Canneyt ha escrit: Yes, they are in <head> and…
Luca Olivetti wrote 26 lines
El 11/8/23 a les 13:32, Michael Van Canneyt ha escrit: > > > On Fri, 11 Aug 2023, Luca Olivetti via Pas2js wrote: > >> El 11/8/23 a les 13:01, Michael Van Canneyt via Pas2js ha escrit: >> >>> >>> The latter: Import jquery with a tag, make sure it is before the >>> bootstrap js. >>> (bootstrap 5 tries to detect the presence of jquery, so jquery must be >>> first) >>> >>> I use bootstrap 5 with the libbootstrap in several commercial projects, >>> so I am quite sure it works :-) >> >> Well, I must be doing something wrong since it gives me that exception >> here. Maybe I should delay the rtl.run() ("Run RTL when all page resources >> are fully loaded")? > > Maybe. It depends on where you set the script elements. If they are in the > <head></head> then it should not be necessary. Yes, they are in <head> and apparently it is necessary. OTOH, even if it now works, I introduced a dependency on jquery that I don't strictly need.

As I wrote, a version of the unit that targets bootstrap 5 without the dependency on jquery would be good. Unfortunately, this is not a trivial change.

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt: If you look at the documentation: https://getbootstrap.com/docs/5.0/components/modal/#opti…

El 11/8/23 a les 14:19, Michael Van Canneyt via Pas2js ha escrit:

I'll see about adding static.

What about:

--- a/packages/bootstrap/bootstrapwidgets.pp
+++ b/packages/bootstrap/bootstrapwidgets.pp
@@ -209,6 +209,7 @@ Type
    private
      FHideEl : TJSHTMLElement;
      FBackDrop: Boolean;
+    FStaticBackDrop: Boolean;
      FFocus: Boolean;
      FKeyBoard: Boolean;
      FOnHide: TOnModalHideEvent;
@@ -241,6 +242,7 @@ Type
    Published
      Property ShowOnRender: Boolean Read FShowOnRender Write FShowOnrender;
      Property BackDrop : Boolean Read FBackDrop Write FBackDrop;
+    Property StaticBackDrop : Boolean Read FStaticBackdrop Write
FStaticBackDrop;
      Property KeyBoard : Boolean Read FKeyBoard Write FKeyBoard;
      Property Focus : Boolean Read FFocus Write FFocus;
      Property Template : String Read FTemplate Write SetTemplate;
@@ -847,12 +849,20 @@ end;
  procedure TBootstrapModal.ApplyWidgetSettings(aElement: TJSHTMLElement);
  begin

- JQuery(aElement).modal(New([
- 'backdrop', BackDrop,
- 'keyboard', keyboard,
- 'focus',Focus,
- 'show',ShowOnRender
- ]));
+ if StaticBackDrop then
+ JQuery(aElement).modal(New([
+ 'backdrop', 'static',
+ 'keyboard', keyboard,
+ 'focus',Focus,
+ 'show',ShowOnRender
+ ]))
+ else
+ JQuery(aElement).modal(New([
+ 'backdrop', BackDrop,
+ 'keyboard', keyboard,
+ 'focus',Focus,
+ 'show',ShowOnRender
+ ]));

    jQuery(aElement).on_('hidden.bs.modal', at BootstrapHide);
  end;

Bye

Luca

Reply by email