Changing CSS in dynamically generated HTML - html

How can I get access and change CSS like the font color in completely dynamically generated HTML out of XML, if I cannot use an ID as I do not know in advance how many of the same HTML-tags will be generated and which of two different (red/green) colors it will need? (I am new here and very new in JS)
Data will be generated and collected on the server. I get them back and sort them in a "fieldset" and further more in a "collapsible". Data "var a, b and c" come together in one line (fieldset) and should be in a specific font color. So for "var d" there will be a "xx" or "yy" coming back from XML for this font color and be saved in "var d" and therefore the font color should be "red" or "green".
Part of my script in short:
function addPart(currentIndex,currentPart)
{
var a = $(currentPart).find("a").text();
var b = $(currentPart).find("b").text();
var c = $(currentPart).find("c").text();
var d = $(currentPart).find("d").text();
if(d === "xx") {
$("div.productColor").css({"color":"green"});
} else {
$("div.productColor").css({"color":"red"});
};
$("#shoppingTableDiv").append (
"<div data-role='collapsible'>"
+ "<h3>"
+ "<div class='productColor'>"
+ "<fieldset class='ui-grid-b'>"
+ "<div class='ui-block-a'>"
+ a
+ "</div>"
+ "<div class='ui-block-b'>"
+ b
+ "</div>"
+ "<div class='ui-block-c'>"
+ c
+ "</div>"
+ "</fieldset>"
+ "</div>"
+ "</h3>"
+ "</div>");
$('#shoppingTableDiv').collapsibleset('refresh');
}

I didn't fully understand how you want to color your results, but I think you can use your fieldset classes.
For example:
.ui-grid-b div { color: red; }
.ui-grid-c div { color: green; }
If you don't know how many fieldsets you're going to have and you can't use the fieldset classes you showed in your code example, you might find the nth-child selector useful.
Maybe something like this:
fieldset:nth-child(2n) div { color: red; }
fieldset:nth-child(2n+1) div { color: green; }

Related

Aggrid assign css to cell

I have a column with value like this 'A-E'. I want to apply css for each alphabet like this
Padding: 5px;
background-color: red;
I have tried cellrenderer but it does not seems to work. I'm working on angular cli 6
I got the result after several attempts please see the result
What I did is in the colDefs of the column where css was to be applied I added an extra property as below:
cellRenderer: function(params) {
params.value = 'A-B';
const v1 = params.value.substring(0, 1);
const v2 = params.value.substring(2);
return '<span style="background-color:#e585a2;padding:5px;border:1px solid black;">' + v1 + '</span>' +
' <span style="background-color:#42c985;padding:5px;border:1px solid black;">' + v2 + '</span>';
}
Note: I manipulated the params.value because data like that will be coming from my api later after modifications.

Why is my font color not being applied?

If I set the font like so on a label:
<label id="lblrptgenprogress" class="invisible redfont">Report generation list is being constructed...</label>
...it works (I see the text in red after I remove the "invisible" class).
However, if I try to set the color like so in another label:
<label id="testsettingproduceusage" class="midnightbluefont"></label>
...with no text in the label to begin with, but adding it dynamically later, it doesn't work - the dynamically added text remains the default black. Why? How can I get it to respect the assigned class?
I give the label some text in an AJAX call this way:
. . .
success: function (returneddata) {
var nextgendate = returneddata.testsettings.NextGenDate;
var nextfromdate = returneddata.testsettings.NextFromDate;
var nexttodate = returneddata.testsettings.NextToDate;
var verbiage = 'If you save the current configuration, the Produce Usage report would next be sent on ' +
nextgendate +
' and emailed to ' +
addressees +
'; the report would cover data from ' +
nextfromdate +
' to ' +
nexttodate;
$("#testsettingproduceusage").append(verbiage);
document.body.style.cursor = 'pointer';
},
. . .
The CSS is:
.redfont {
color: red;
}
.midnightbluefont {
color: midnightblue;
}
UPDATE
Specificity it is, as can be seen by the comments:
.midnightbluefont {
/*color: midnightblue;*/ <= doesnt' work
/*color: #191970;*/ <= doesn't work
/*color: #191970 !important;*/ <= works
color: midnightblue !important; <= works
}
The brute force way to work it is either this:
.midnightbluefont {
color: midnightblue !important;
}
...or this:
.midnightbluefont {
color: #191970 !important;
}

HTML Table or CSS? (Layout Issue)

I am struggling to understand how I can achieve the look I want with HTML tables/CSS while also having an Accordion, I am getting data from multiple SharePoint lists and displaying it to the page in a JqueryUI Accordion however because it's essentially just a data dump I need to be able to add headers so the users know what data is what. I have created a JS Fiddle of how I tried to do it, with the issues explained below:
http://jsfiddle.net/7uv3m1fy/
The look I am trying to achieve is a set of headers across the top for the parent dataset, with the data from that set being the clickable part of the Accordion. These need the data to line up under the headers. This is pretty much the entire issue I have, they seem to be using only the first of the HTML table on subsequent rows when I want the information to fill all of the columns (preferably with a way to match where the information goes to under it's header)
I honestly don't know enough about CSS or how I could achieve this using DIV's while still being able to get information to line up.
The biggest issue I have is that because the data populating these tables is called from SharePoint lists/views, the number of columns is completely dynamic so I cannot hardcode column width/numbers.
Any suggestion of how I could do this using CSS or HTML or Both would be greatly appreciated.
below is the code I am currently using to generate this (C# as it's for a SharePoint WebPart)
try
{
if (parentItems.Count > 0)
{
newHTML = newHTML + "<Table style='table-layout:fixed' width='100%'><tr>";
foreach (string fieldName in viewFields)
{
newHTML = newHTML + "<td> " + parentList.Fields.GetFieldByInternalName(fieldName).Title + "</td>";
}
newHTML = newHTML + "</tr>";
}
}
catch
{
newHTML = newHTML + "No Results Found";
}
foreach (SPListItem parentitem in parentItems)
{
newHTML = newHTML + "<tr class='yes' width='100%'><td> Parent:</td><td>" + parentitem.ID.ToString() + "</td></tr><tr width='100%'><td>";
foreach (SPListItem childitem in childItems)
{
if (childitem[RelatedID].ToString() == parentitem.ID.ToString())
{
newHTML = newHTML + "<div>" + childitem.ID.ToString() + "</div>";
}
}
newHTML = newHTML + "</td></tr>";
}
newHTML = newHTML + "</table>";
}
And in the webpart I am adding the following to call it:
<div id="accordion">
<%= this.newHTML %>
</div>
I hope I have given enough information, although the code above doesn't really relate to the question it will show you how I am trying to dynamically output the HTML to achieve my goal.
Happy to provide more info if you need it.
Regards,

H1 Tag Variable

I have a variable in my append line "key" and "value" but I do not know how to keep span format for the key value. To have the blue color be on the key variable.
for key,value of data
$('#data-results').append "<br>" + "<li>" + """<span style="color: #0000CD;"> key </span>""" + ": " + value
Results on Browser
key: 0004a32eb300
What it should be:
user: 0004a32eb300
^ user being blue
Thank you in advance
http://coffeescriptcookbook.com/chapters/strings/interpolation
$('#data-results').append "<br>" + "<li>" + """<span style="color: #0000CD;"> #{key} </span>""" + ": " + value
You should either use a template, or build up your HTML using jQuery. Also, don't use inline styles, add CSS like this:
li span.key-from-data {
color: #0000CD;
}
Then with CoffeeScript as follows:
for own key, value of data
$li = $ '<li>',
text: ": #{value}"
$span = $ '<span>',
class: "key-from-data"
text: key
$li.prepend $span
$('#data-results').append $li
See it working here.

Displaying div tag in a textbox

I am saving the below div tag into SQL DB:
String StrDiv = "<div STYLE=" + "''"+"font-family: " + lblPreviewPane.Font.Name + "; font-size: " + lblPreviewPane.Font.Size + "; color: " + ColorPicker1.Color + ""+"''" + ">" + lblPreviewPane.Text + "</div>";
The below query is used to update the message:
String sqlUpdate = "update iweb_messages set message='" + StrDiv +
"'where site_id ='" + mSiteID + "' and page_id ='" + ddlSitePage.SelectedValue + "'";
I retrieve the div from DB and display it in a textbox and a label.
In the label, the div tag is displayed as: messagetoday21
But in the textbox, the div tag is displayed as:
<div STYLE='font-family: Times New Roman; font-size: 18pt; color: #CC3399'>today21</div>
I need to display the text alone in the textbox also(same as displayed in the Label).
Kindly help me with some suggestions.
I use the below code to display the text from the DB in the textbx and label:
String sqlMsg = "select message from iweb_messages where site_id='" + mSiteID + "' and page_id='" + ddlSitePage.SelectedValue + "'";
DataView dvMsg = dbLib.GetDateView(sqlMsg);
if (dvMsg.Table.Rows.Count > 0)
{
txtOutputMessage.Text = dvMsg.Table.Rows[0]["message"].ToString();
lblPreviewPane.Text = dvMsg.Table.Rows[0]["message"].ToString();
}
I assume your "textbox" is a textarea? If so, the behavior you're seeing is expected, because you're generating HTML that looks like this:
<textarea><div STYLE='font-family.....></div></textarea>
The content of a TEXTAREA is displayed without HTML rendering. Whereas the label (assuming it's a span or another div) will render the HTML.
So, as a quick fix, you need to save lblPreviewPane.Text to your database in addition to the full <div>; then display the value of lblPreviewPane.Text in your text box instead of the <div>.
But I would also question the wisdom of storing the full <div> in the database. If the font attributes need to be determined ahead of time, then I think you would be better off storing those attribute values in the table, then building the formatted <div> when it is being displayed.