Sign in Join

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

Mailing lists / pas2js / 2022-10

How to change a div (or othe widget) content

12 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,

I have a div in my html and I want to change its content. For consistency, I'd like to use a TDivWidget, but I could find no way to modify the content: I cannot manipulate the TDivWidget itself and creating a TLabelWidget and assigning the TDivWidget as its parent does nothing.

If I use a TTagWidget instead, I can use TextContent (however if I set it to '' it will keep the previous content).

Currently I'm using a TJSHTMLDivElement and assign its innerHTML.

There are other cases where I had to use an html element instead of a widget since I could find no way to manipulate the content of the widget.

Is it so by design or am I doing something wrong?

Bye

Luca
Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 9 lines
Hello, I have a div in my html and I want to change its content. For consistency, I'd like to use a TDivWidget, but I could find no way to modify the content: I cannot manipulate the TDivWidget itself and creating a TLabelWidget and assigning the TDivWidget as its parent does nothing. If I use a TTagWidget instead, I can use TextContent (however if I set it to '' it will keep the previous content).

I have changed this.

I have also added TextMode as a property so you can choose between InnerText and InnerHTML

Show quoted text 7 lines
Currently I'm using a TJSHTMLDivElement and assign its innerHTML. There are other cases where I had to use an html element instead of a widget since I could find no way to manipulate the content of the widget. Is it so by design or am I doing something wrong?

Your use of DivHTML (or any other TTagWidget) is correct, but I needed to extend the functionality.

If you feel functionality is missing, don't hesitate to say so, and we'll discuss what can be done.

I've used these widgets in multiple projects, but use cases may vary.

You may want to look at the TSimpleTemplateWidget and TTemplateWidgets if you need to set innerHTML a lot, I usually use that.

Michael.

Luca Olivetti
to Michael Van Canneyt

El 3/10/22 a les 17:35, Michael Van Canneyt via Pas2js ha escrit:

Show quoted text 8 lines
> If I use a TTagWidget instead, I can use TextContent (however if I set > it to '' it will keep the previous content). I have changed this. I have also added TextMode as a property so you can choose between InnerText and InnerHTML

Thank you, I saw the commit but...

Show quoted text 14 lines
> > Currently I'm using a TJSHTMLDivElement and assign its innerHTML. > > There are other cases where I had to use an html element instead of a > widget since I could find no way to manipulate the content of the widget. > > Is it so by design or am I doing something wrong? Your use of DivHTML (or any other TTagWidget) is correct, but I needed to extend the functionality. If you feel functionality is missing, don't hesitate to say so, and we'll discuss what can be done.

...I still see no way to change the content of a TDivWidget. Mind me, I don't need more functionality (or better, I don't know what I need, this is my first project using pas2js), I just need to learn how to best use what's already there.
I can mix and match widgets and elements, but I don't know if that's good practice.
I also instinctively prefer to use the widget most close to the tag (I just changed a bunch TTagWidget to the real widget type, i.e. TDivWidget and TLabelWidget) but maybe that's not good practice either.

Show quoted text 5 lines
I've used these widgets in multiple projects, but use cases may vary. You may want to look at the TSimpleTemplateWidget and TTemplateWidgets if you need to set innerHTML a lot, I usually use that.

I'll check those examples, thank you.

Luca
Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 29 lines
El 3/10/22 a les 17:35, Michael Van Canneyt via Pas2js ha escrit: >> If I use a TTagWidget instead, I can use TextContent (however if I set it >> to '' it will keep the previous content). > > I have changed this. > > I have also added TextMode as a property so you can choose between > InnerText > and InnerHTML Thank you, I saw the commit but... > >> >> Currently I'm using a TJSHTMLDivElement and assign its innerHTML. >> >> There are other cases where I had to use an html element instead of a >> widget since I could find no way to manipulate the content of the widget. >> >> Is it so by design or am I doing something wrong? > > Your use of DivHTML (or any other TTagWidget) is correct, but I needed to > extend the functionality. > > If you feel functionality is missing, don't hesitate to say so, and we'll > discuss what can be done. ...I still see no way to change the content of a TDivWidget.

Sorry, I forgot to publish these properties, TDivWidget descends from TCustomTagWidget, not from TTagWidget, my bad.

Fixed.

Michael.

Luca Olivetti
to Michael Van Canneyt: I have changed this. I have also added TextMode as a property so you can choose between…

El 3/10/22 a les 17:35, Michael Van Canneyt via Pas2js ha escrit:

Show quoted text 6 lines
I have changed this. I have also added TextMode as a property so you can choose between InnerText and InnerHTML

Unfortunately you left out the "if FTextContent''" which broke my page (it empties a div hence later it doesn't find the element that were there). This fixes it for me

--- a/packages/webwidget/htmlwidgets.pp
+++ b/packages/webwidget/htmlwidgets.pp
@@ -3195,10 +3195,11 @@ end;
  procedure TCustomTagWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  begin
    inherited ApplyWidgetSettings(aElement);
-  if TextMode=tmText then
-    aElement.InnerText:=TextContent
-  else
-    aElement.InnerHTML:=TextContent
+  if FTextContent'' then
+    if TextMode=tmText then
+      aElement.InnerText:=TextContent
+    else
+      aElement.InnerHTML:=TextContent
  end;

Bye

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 12 lines
El 3/10/22 a les 17:35, Michael Van Canneyt via Pas2js ha escrit: > > I have changed this. > > I have also added TextMode as a property so you can choose between > InnerText > and InnerHTML Unfortunately you left out the "if FTextContent''" which broke my page (it empties a div hence later it doesn't find the element that were there).

I understood from your original mail that you wanted that particular part gone ?

I reinstated it.

Michael.

Luca Olivetti
to Michael Van Canneyt

El 4/10/22 a les 9:52, Michael Van Canneyt ha escrit:

Show quoted text 20 lines
On Tue, 4 Oct 2022, Luca Olivetti via Pas2js wrote: > El 3/10/22 a les 17:35, Michael Van Canneyt via Pas2js ha escrit: > >> >> I have changed this. >> >> I have also added TextMode as a property so you can choose between >> InnerText >> and InnerHTML > > > Unfortunately you left out the "if FTextContent''" which broke my > page (it empties a div hence later it doesn't find the element that > were there). I understood from your original mail that you wanted that particular part gone ?

I must have expressed myself poorly, I just wanted a way to modify the content, i.e. what you did in a later commit

https://gitlab.com/freepascal.org/fpc/pas2js/-/commit/ad8c36e807de50be099dd0bf16e346fd21b0edc9

I reinstated it.

Thank you.

Bye

Luca
Luca Olivetti
to Luca Olivetti

El 4/10/22 a les 9:59, Luca Olivetti via Pas2js ha escrit:

Show quoted text 32 lines
El 4/10/22 a les 9:52, Michael Van Canneyt ha escrit: > > > On Tue, 4 Oct 2022, Luca Olivetti via Pas2js wrote: > >> El 3/10/22 a les 17:35, Michael Van Canneyt via Pas2js ha escrit: >> >>> >>> I have changed this. >>> >>> I have also added TextMode as a property so you can choose between >>> InnerText >>> and InnerHTML >> >> >> Unfortunately you left out the "if FTextContent''" which broke my >> page (it empties a div hence later it doesn't find the element that >> were there). > > I understood from your original mail that you wanted that particular > part gone ? I must have expressed myself poorly, I just wanted a way to modify the content, i.e. what you did in a later commit https://gitlab.com/freepascal.org/fpc/pas2js/-/commit/ad8c36e807de50be099dd0bf16e346fd21b0edc9 > > I reinstated it. Thank you.

Aagh, now I see where the confusion came from: in one message I told that, if I assign and empty content, the element would keep the old one, so you removed the check, but then when I create the widget it would empty the content that it got from the html.
I guess I can't have my cake and eat it too, unless you could differentiate the two cases (i.e. initial assignment, when you just got the element from the html, and subsequent assignments).

Bye

Luca
Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 38 lines
El 4/10/22 a les 9:59, Luca Olivetti via Pas2js ha escrit: > El 4/10/22 a les 9:52, Michael Van Canneyt ha escrit: >> >> >> On Tue, 4 Oct 2022, Luca Olivetti via Pas2js wrote: >> >>> El 3/10/22 a les 17:35, Michael Van Canneyt via Pas2js ha escrit: >>> >>>> >>>> I have changed this. >>>> >>>> I have also added TextMode as a property so you can choose between >>>> InnerText >>>> and InnerHTML >>> >>> >>> Unfortunately you left out the "if FTextContent''" which broke my page >>> (it empties a div hence later it doesn't find the element that were >>> there). >> >> I understood from your original mail that you wanted that particular part >> gone ? > > I must have expressed myself poorly, I just wanted a way to modify the > content, i.e. what you did in a later commit > > https://gitlab.com/freepascal.org/fpc/pas2js/-/commit/ad8c36e807de50be099dd0bf16e346fd21b0edc9 > >> >> I reinstated it. > > Thank you. Aagh, now I see where the confusion came from: in one message I told that, if I assign and empty content, the element would keep the old one, so you removed the check, but then when I create the widget it would empty the content that it got from the html.

Yes, that's the consequence :)

I guess I can't have my cake and eat it too, unless you could differentiate the two cases (i.e. initial assignment, when you just got the element from the html, and subsequent assignments).

I've long been thinking to create a 'ClearContent' method, maybe this is a good occasion. It could actually set the HTML innertext/html, and for this widget then also set the textcontent to empty.

But first I'll think about how to solve the detection of the initial setting, probably there is a way.

Michael.

Luca Olivetti
to Michael Van Canneyt

El 5/10/22 a les 8:53, Michael Van Canneyt ha escrit:

Show quoted text 6 lines
> Aagh, now I see where the confusion came from: in one message I told > that, if I assign and empty content, the element would keep the old > one, so you removed the check, but then when I create the widget it > would empty the content that it got from the html. Yes, that's the consequence :)

The law of unintended consequences :-D

Show quoted text 11 lines
> I guess I can't have my cake and eat it too, unless you could > differentiate the two cases (i.e. initial assignment, when you just > got the element from the html, and subsequent assignments). I've long been thinking to create a 'ClearContent' method, maybe this is a good occasion. It could actually set the HTML innertext/html, and for this widget then also set the textcontent to empty. But first I'll think about how to solve the detection of the initial setting, probably there is a way.

It's not a big issue, now I just use a single space when I want to clear the previous content.

function EnsureNotEmpty(const s:string):string;
begin
  if s='' then
    result:=' '
  else
    result:=s
end;

Bye

Luca
Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 24 lines
El 5/10/22 a les 8:53, Michael Van Canneyt ha escrit: >> Aagh, now I see where the confusion came from: in one message I told that, >> if I assign and empty content, the element would keep the old one, so you >> removed the check, but then when I create the widget it would empty the >> content that it got from the html. > > Yes, that's the consequence :) The law of unintended consequences :-D > >> I guess I can't have my cake and eat it too, unless you could >> differentiate the two cases (i.e. initial assignment, when you just got >> the element from the html, and subsequent assignments). > > I've long been thinking to create a 'ClearContent' method, maybe this is a > good occasion. It could actually set the HTML innertext/html, and for this > widget then also set the textcontent to empty. > > But first I'll think about how to solve the detection of the initial > setting, probably there is a way. It's not a big issue, now I just use a single space when I want to clear the previous content.

I introduced ClearContents, and now setting an empty text should work if the widget is already rendered.

Michael.

Luca Olivetti
to Michael Van Canneyt

El 5/10/22 a les 11:05, Michael Van Canneyt via Pas2js ha escrit:

I introduced ClearContents, and now setting an empty text should work if the widget is already rendered.

Great! I removed my workaround and everything works. Thank you.

Bye

Luca