Conditional colorizing in WebViewer table.html

Ref. https://puravidaapps.com/table.php

How can I add conditional font color formatting to the presented table.html, depending on the table cell value?

If table cell value = OK => font color = green
if table cell value = NOK => font color = red

Could not find a suitable example by google

You must have a different “google” to me…

Google conditional formatting for html table

You need to include the following code inside the html file

<script>
document.querySelectorAll('td').forEach(e => {
 if (e.textContent == "OK") {
    e.style.color = "green";
 }
  else if (e.textContent == "NOK") {
    e.style.color = "red";
 }
});
</script>
1 Like

Thanks Red_Panda,

I got the idea, but I did not get that working.

In the original table.html, there is the following script section of code which adds rows dynamically into table:
–clip–
function addRow(dom, tag) {
for(j=0;j<rowArray.length;j++){
var el = doc.createElement(tag);
el.innerHTML = rowArray[j];
tr.appendChild(el);
dom.appendChild(tr);
document.getElementById(tag).style.color = “#ff0000”;
}
}

–clap–

Now, I tried to integrate your suggestion into that as follows:

–clip–

function addRow(dom, tag) {
for(j=0;j<rowArray.length;j++){
var el = doc.createElement(tag);
el.innerHTML = rowArray[j];
tr.appendChild(el);
dom.appendChild(tr);
document.getElementById(tag).style.color = “#ff0000”;

	document.querySelectorAll('td').forEach(e => {
	if (e.textContent == "OK") {
		e.style.color = "green";
	}
	else if (e.textContent == "NOK") {
		e.style.color = "red";
		}
	else {
		e.style.color = "#ff0000";
		}
	});
	
  }
}

–clap–

…but I get only empty WebViewer.
All parenthesis should match, at least in Notepad++ editor they do.
As I do not really know any html syntax, I am not sure what is the issue.
Should I distinguish these sentences and use either one of “document.getElementById(tag)” OR “document.querySelectorAll(‘td’).forEach” ?

Another try, did not succeed either:

–clip–

function addRow(dom, tag) {
for(j=0;j<rowArray.length;j++) {
var el = doc.createElement(tag);
el.innerHTML = rowArray[j];
tr.appendChild(el);
dom.appendChild(tr);
document.getElementById(tag).style.color = “#ff0000”;

	if (document.getElementById(tag).textContent == "CLOSED") {
		document.getElementById(tag).style.color = "green";
	}
	else if (document.getElementById(tag).textContent == "NEW") {
		document.getElementById(tag).style.color = "red";
		}
	else if (document.getElementById(tag).textContent == "ONGOING") {
		document.getElementById(tag).style.color = "yellow";
		}
	else {
		document.getElementById(tag).style.color = "#ff0000";
		}
	}
}

–clap–

Original table.html is here:
https://puravidaapps.com/table.php

  • Tommi S.

I tested with this, and it worked fine:

–clip–

    <tbody>
      <tr>
        <td>Alvin</td>
        <td>Eclair</td>
        <td>$0.87</td>
      </tr>
      <tr>
        <td>Alan</td>
        <td>Jellybean</td>
        <td>$3.76</td>
      </tr>
      <tr>
        <td>Jonathan</td>
        <td>Lollipop</td>
        <td>$7.00</td>
      </tr>
    </tbody>
  </table>

–clap–

However, when I implemented the script section into table.html as follows, I get just an empty WebViewer. Without colors the result is however ok and works fine.

–clip–

function addRow(dom, tag) {
for(j=0;j<rowArray.length;j++) {
var el = doc.createElement(tag);
el.innerHTML = rowArray[j];
tr.appendChild(el);
dom.appendChild(tr);
document.getElementById(tag).style.color = “#ff0000”;
}
}

document.querySelectorAll('td').forEach(e => {
	if (e.textContent == "CLOSED") {
		e.style.color = "green";
	}
	else if (e.textContent == "OPEN") {
		e.style.color = "red";
		}
	else {
		e.style.color = "black";
		}
	});

–clap–

  • Tommi S.
Name Item Name Item Price

Strange. Code should work, see here: https://jsfiddle.net/fLmr39ut/
Try

document.getElementsByTagName("TD").forEach(function(e) {
 if (e.textContent == "OK") {
    e.style.color = "green";
 }
  else if (e.textContent == "NOK") {
    e.style.color = "red";
 }
});

Maybe the webviewer doesn’t know ES

What do you mean by that?

I created a simple test app wil WebViewer and a button.
Blocks including only:
kuva

And table_test.html:
–clip–

    <tbody>
      <tr>
        <td>Alvin</td>
        <td>Eclair</td>
        <td>$0.87</td>
      </tr>
      <tr>
        <td>Alan</td>
        <td>Jellybean</td>
        <td>$3.76</td>
      </tr>
      <tr>
        <td>Jonathan</td>
        <td>Lollipop</td>
        <td>$7.00</td>
      </tr>
    </tbody>
  </table>
document.querySelectorAll('td').forEach(e => { if (e.textContent == "Alvin") { e.style.color = "green"; } else if (e.textContent == "Alan") { e.style.color = "red"; } else { e.style.color = "black"; } }); --clap-- And now Alvin resulted as green and Alan resulted as red in WebViewer.
Name Item Name Item Price

So I am still wondering the issue which is distinguishing this test app and the actual app with html code

I mean EcmaScript arrow functions and Array.forEach(). Has anybody tried that?

Put @Red_Panda 's additional script at the end of all the other scripting in the file, then it should work (using Taifun’s table.html example)

function addRow(dom, tag) {
  for(j=0;j<rowArray.length;j++){
    var el = doc.createElement(tag);
    el.innerHTML = rowArray[j];
    tr.appendChild(el);
    dom.appendChild(tr);
  }
}

//colorize
document.querySelectorAll('td').forEach(e => {
 if (e.textContent == "TX") {
e.style.color = "green";
 }
  else if (e.textContent == "Austin") {
e.style.color = "red";
 }
});

  </script>
</body>
</html>

image

3 Likes

YES!!

Now it is working finally!

Great thanks for all of you that gave me support :slight_smile:

  • Tommi S.

Further development:
Can anyone provide an example, howto tweak the table.html so that

  1. the dynamically created row becomes a button?
  2. by pushing that “dynamic” button, an AppInventor activity can be started by clicking the button like below
    kuva

Thanks,

  • Tommi S.

Using the html from here:

https://puravidaapps.com/table.php

If you click on a cell that row index will be returned to the webviewstring. Is that enough ?

ECMAScript support would depend on at least one of the following things:

I’d recommend finding out what versions of Android your target audience typically uses and tailoring your JavaScript to only use APIs supported by those versions. Check out caniuse.com for more info.

Hi,

yes do have that table.html working in my app.
But really? If I click on the table view (=WebViewer), it returns that row index inside the app?
Cannot be quite like that, because there is no such activity for Webviewer on blocks side.
So I have now misunderstood something…?

  • Tommi S.

Look in the html:

tr.addEventListener ("click", function () {
        // return picked row (remove td tag, use delimiter and remove last character, http://stackoverflow.com/a/1990274)
        //var picked = this.innerHTML.replace(/<td>/g, "").replace(/<\/td>/g, delimiter).replace(/(\s+)?.$/, '');
        //window.AppInventor.setWebViewString(picked);

        // return index (add 1 because first row is the header row)
        //window.document.title = this.rowIndex + 1;
        window.AppInventor.setWebViewString(this.rowIndex + 1);
      });

The functionality may be commented out (//), so you will have to uncomment for the webviewstring to be set

Is this how it should go?
I tried this with emulator but the table row did not indicate in any way that it became “selected”…

–clip–
tr.addEventListener (“click”, function () {
// return picked row (remove td tag, use delimiter and remove last character, http://stackoverflow.com/a/1990274)
var picked = this.innerHTML.replace(/

/g, “”).replace(/</td>/g, delimiter).replace(/(\s+)?.$/, ‘’);
window.AppInventor.setWebViewString(picked);
    // return index (add 1 because first row is the header row)
    //window.document.title = this.rowIndex + 1;
    window.AppInventor.setWebViewString(this.rowIndex + 1);
  });

–clap–

Sorry, not concentrating, it should work just as I posted it and in the file with this line:

window.AppInventor.setWebViewString(this.rowIndex + 1);

You will need to use the when webviewstring Changed event in the app to return the value of the row selected, then use this with the list you sent to the html file

Thx, I got it now :slight_smile:

However I noticed that row index only is not enough.
How can I refer to the row items?

  • Tommi