Class Not Updating when useing case in javascript - class

I don't know why the class does not be udpated in following script using the (case):
if (favorite !== null) {
switch (favorite) {
case 'cat':
document.getElementById("one").className = "favBlue";
//document.getElementById('one').className ='favRed';
//document.createAttribute('class','favRed')
break;
case 'dog':
document.getElementsByName('dog').className = 'favBlue';
break;
case 'gerbil':
document.getElementsByName('gerbil').className = 'favYellow';
break;
case 'gopher':
document.getElementsByName('gopher').className = 'favWhite';
break;
}
}
Please click on this link in order to see the complete script http://jsfiddle.net/gu8u6eoc/6/

Your case statement is working. But I think you should use document.getElementById(id) instead of document.getElementsByName(name) to change the class values. Also, you are applying the classnames to the checkboxes which won't change their colors. You should apply the classnames to the texts instead.
According to MDN, getElementsByName have different behavior per browser (e.g. sometimes it will work on elements with a similar id attribute to name). Also, getElementsByName() returns a NodeList instead of an Element object.
BTW, here is a simplified working JSFiddle. This contains both the usage of getElementById and getElementsByName.

Related

news category filtering issue in exended class Contao

I registered $GLOBALS['TL_HOOKS']['newsListFetchItems'][] hook in my news extended extension for sorting with my custom field (say 'rank'). But the pages which contain news filtering is not working . I am using the contao Version 4.5.8 . How can I solve the issue.
$t = \NewsModel::getTable();
$arrOptions = array();
switch ($objModule->news_sorting)
{
case 'sort_rankid_asc':
$arrOptions['order'] = "$t.rankid = 0, $t.rankid, $t.date DESC";
break;
case 'sort_random':
$arrOptions['order'] = "$t.date DESC";
break;
default:
$arrOptions['order'] = "$t.date DESC";
}
return \NewsModel::findPublishedByPids($newsArchives, $blnFeatured, $limit, $offset, $arrOptions);
News filtering with category is not working after I added the above code.How Can I fix the issue
The codefog/contao-news_categories extension in version 3.x also uses the newsListFetchItems hook to filter the news list items (see here). The hook will only work for one extension - which ever one returns something other than false first.
If you need both your custom sorting and filtering by a category, then you will need to implement the news category filtering yourself and you have to make sure that your hook is executed first.

Adding images to R object

I'm having a problem calling images that are sitting inside the res folder, and I assume I'm doing something wrong.
I've read several posts in SOF stating that all i need to do is to add all the images in a flat way to the res folder:
But I can't reach them in the code below (the code in under MapsActivity)
private BitmapDescriptor
getMarkerIcon(Job.jobTypes jobType){
switch ((jobType)) {
case LIFESTYLE:
return BitmapDescriptorFactory.fromResource(android.R.drawable.lifestyle); <-- states it cannot find the resource
case DELIVERY:
break;
case PRIVATELESSONS:
break;
case HANDYMAN:
break;
}
}
What I'm trying to achieve is a way of returning to another function the current image for the map marker based on the enum type.
Resource files cannot have space in their name. Remove spaces from the file names.
Load image from your resource:
return BitmapDescriptorFactory.fromResource(R.drawable.lifestyle);

Copy range with conditional formatting

I have a range with Conditional Formatting in an existing Excel file. I used EPPlus to copy that range to a new sheet, then I found the conditional formatting was missing.
Is there any way to copy range with conditional formatting using EPPlus?
I found a solution for this. I did not test it on all formattingRuleTypes. (Only needed 2 of them for the moment)
In my application i have 1 template row for each sheet.
var formatList = fromSheet.ConditionalFormatting.ToList();
foreach (var cf in formatList)
{
// sourceRow is the row containing the formatting
if (cf.Address.Start.Row == sourceRow )
{
IExcelConditionalFormattingRule rule = null;
switch (cf.Type)
{
case OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType.GreaterThan:
rule = dest.ConditionalFormatting.AddGreaterThan();
break;
case OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType.GreaterThanOrEqual:
rule = dest.ConditionalFormatting.AddGreaterThanOrEqual();
break;
case OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType.LessThan:
rule = dest.ConditionalFormatting.AddLessThan();
break;
case OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType.LessThanOrEqual:
rule = dest.ConditionalFormatting.AddLessThanOrEqual();
break;
default:
break;
}
rule.Style.Fill = cf.Style.Fill;
rule.Style.Border = cf.Style.Border;
rule.Style.Font = cf.Style.Font;
rule.Style.NumberFormat = cf.Style.NumberFormat;
// I have no clue why the Formula property is not included in the IExcelConditionalFormattingRule interface. So I needed to cast this.
((ExcelConditionalFormattingRule)rule).Formula = ((ExcelConditionalFormattingRule)cf).Formula;
((ExcelConditionalFormattingRule)rule).Formula2 = ((ExcelConditionalFormattingRule)cf).Formula2;
// Calculate the new address for the formatting. This will be different in your case
var adr = new ExcelAddress( dest.Start.Row , cf.Address.Start.Column -1 , dest.Start.Row, cf.Address.Start.Column -1 + cf.Address.Columns -1 );
rule.Address = adr;
I have no clue why the Formula property is not included in the IExcelConditionalFormattingRule interface. So I needed to cast this.
To add to the answer of Luc Wuyts (I can't comment yet due to limited reputation):
// I have no clue why the Formula property is not included in the IExcelConditionalFormattingRule interface. So I needed to cast this.
((ExcelConditionalFormattingRule)rule).Formula = ((ExcelConditionalFormattingRule)cf).Formula;
((ExcelConditionalFormattingRule)rule).Formula2 = ((ExcelConditionalFormattingRule)cf).Formula2;
Some conditional formatting do not have the Formula-options. This cast will work, but applying the Formula properties to conditional formatting options which do not require it will have unforeseen results. Eg. the ConditionalFormatting.AddContainsBlanks() does not require Formula properties, and adding them might mess up the conditional formatting. A better approach is to check the type, and add the formula's only when required.
I had a similar problem, the only way I found to inspect, change or delete a conditional format of a cell or range is looking at the openxml specs. The conditional format is stored in the worksheet, with the range under the attribute sqref. So you can edit that range or add a new.
For example:
DIM p As New ExcelPackage(New FileInfo(ExlReportPath), True)
Dim ws As ExcelWorksheet = p.Workbook.Worksheets(ExlSheetName)
'--Find Node "worksheet" (1 in my case) , Find all Child Nodes "conditionalFormatting" (5 to 11 in my test)
Print.Debug(ws.WorksheetXml.ChildNodes(1).ChildNodes(5).Name)
'--You get: conditionalFormatting
'--Now you can inspect the range:
Print.Debug(ws.WorksheetXml.ChildNodes(1).ChildNodes(5).Attributes("sqref").Value)
'--Will give you the cell address that this formatting applies to example: "D11:D15"
'--you can change delete or add new range if you want, below I add F11:F15
ws.WorksheetXml.ChildNodes(1).ChildNodes(5).Attributes("sqref").Value="D11:D15 F11:F15"
'--You can inspect the rule itself in the InnerXml also...
If you need more details of the markup, google Wouter van Vugt, "Open XML The markup explained". I found it useful and the full document was online (free).
If you find an easier way please post it.
Regards

Wordpress TinyMCE: Switch Views

In order to complete my Wordpress Plugin I want to make tinyMCE to switch between a custom Tag (Some|data|here) and a corresponding Image Display in the WYSIWYG-View.
The event should be triggered on load, safe, autosave, switch view etc. Threre are 4 different events defined, but none of them works as expected.
onBeforeSetContent
onGetContent
onPostProcess
onLoadContent
.
ed.onPostProcess.add(function(ed, o) {
if (o.set){
o.content = t._htmlToWysiwyg(o.content, url);
}
if (o.get){
o.content = t._wysiwygToHtml(o.content, t);
}
});
Anyon know the right way?
I do not know what you expect the 4 different events will do (?), but i can see some problems in your code.
1. The object o does not contain fields get and set - so o.get and o.set will never be true! Thus your code will never be called.
2. You are using the variable url, but this one is not defined here.
Working example: You may try to paste a string containing "a" into the editor. Use the following:
ed.onPostProcess.add(function(ed, o) {
//console.log('o:', o);
o.content = o.content.replace(/a/g, "A");
});
You should see that all lower 'a's get replaced by 'A's.

How can I use goto in a switch statement in Objective-C?

In my code I need to be able to jump (goto) a different case within the same switch statement. Is there a way to do this?
My code is something like this: (There is a lot of code I just left it all out)
switch (viewNumber) {
case 500:
// [...]
break;
case 501:
// [...]
break;
.
.
.
.
.
case 510:
// [...]
break;
default:
break;
}
Thank you for your time!
-Jeff
It's generally very bad practice to unconditionally jump like you're asking.
I think a more readable/maintainable solution would be to place the shared code in a method and have multiple cases call the method.
If you really want to, you can use goto to do something like:
switch(viewNumber) {
case 500:
// [...]
goto jumpLabel;
case 501:
// [...]
break;
case 502:
// [...]
jumpLabel:
// Code that 500 also will execute
break;
default:break;
}
Note: I only provided the code example above to answer your question. I now feel so dirty I might have to buy some Bad Code Offsets.
Instead of using goto, refactor your code so that the two (or more) cases that use common code instead call it in a common method.
Something like:
switch (value) {
case (firstValue):
// ...
break;
case (secondValue):
[self doSharedCodeForSecondAndThirdValues];
break;
case (thirdValue):
[self doSharedCodeForSecondAndThirdValues];
break;
default:
break;
}
// ...
- (void) doSharedCodeForSecondAndThirdValues {
// do stuff here that is common to second and third value cases
}
It wouldn't be the end of the world to use goto, though it is bad practice.
The practical reason for avoiding use of goto is that you have to search through your swtich-case tree to find that goto label.
If your switch logic changes, you'll have a messy situation on your hands.
If you pull out common code to its own method, the code is easier to read, debug and extend.
You should probably try rewrite your code, like a recursive call or just factor out common stuff and call a separate function. But as a fix and quick answer to your question you could put a label before your switch and goto it, like so
switchLabel:
switch(viewNumber) {
case 500: {
viewNumber = 501;
goto switchLabel;
}
}
Not sure of the Objective-C syntax here, but you could also try a variation thereof
int lastView = 0;
while (lastView != viewNumber)
switch(lastView = viewNumber) {
case 500: {
viewNumber = 501;
break;
}
}
which will keep on looping until the viewNumber doesn't change any more. This is still pretty much just a pretty-looking goto though.
And since we're doing gotos you could just goto into another case, as pointed out already. You could also do fancy stuff similar to Duff's device, by putting cases inside of other blocks. But that's just mad.. :)
[I'm making this answer community wiki because this doesn't actually answer the question per se]
As others have said, this is very bad style, and makes for unreadable code...
Alternatives:
Factor the common code into a separate function, and call that in 2 places.
Use fallthroughs, leave off the the break on a case and it falls through to the next one (remember, cases don't have to be in numerical order!)
if you only want part of a case to be done in the other case, protect it with an if:
as in
case 500:
.
.
.
case 501:
if(viewNumber == 501) {
.
.
.
}
.
.
.
break;