Using Conditional Statement within a css class (Rails/View) - css

In Rails/Haml
I know this works
%i.class{:class => (name ? "arrow-up" : "arrow-down")}
I also know that I can make a helper function
%i.class{:class => (getArrowClass name)}
def getArrowClass value
if value == 1
return 'arrow-up'
elsif value == 0
return 'arrow-down'
else
return ''
end
end #getArrowClass
For some reason, whenever I do the helper way it messes up my view (tables don't stick to their places), so now my question is how can I do the if elsif else in the class using the first method.
something like that
%i.class{:class => (if name then "arrow-up" elsif "arrow-down" else "")}

Please try with
%i.class{:class => ((name == 1) ? "classup" : (name == 0) ? "classdown" : "")}

Related

Trying to do a Case statement in Powerbuilder

Just trying to do a simple case function in powerbuild.
I have a table
client_notes
A!, B!, C!
case(client_notes when '%A!%' then 'Cash1' else "Cash2")
It compiles OK but when I run it, it says Cash2.
Shouldn't it say Cash1?
What's the context here? Is this in PowerScript or in a datawindow expression, or in the SQL source of the datawindow?
And what version/build of PB are you using?
What it should "say" [sic] all depends on the value of "client_notes" at runtime. It will only return the string 'Cash1' when the value of client_notes is exactly equal to the string '%A!%'.
What set of data are you running this against? Show some sample data.
-Paul Horan-
I have never seen a "LIKE" expression used in the expression painter but it might be possible. I gave it a quick try and wasn't able to. I would agree with what Matt said, except your expression doesn't have the important "LIKE" keyword so it is matching exactly on your string and of course not matching so always getting Cash2.
I've wanted to use > or < in CASE statements before and had problems too, the WHEN seems to be pretty 'dumb' and not able to handle anything other than a value.
This works not as elegant as a case statement though.
if ( client_notes like '%A!%' , 'Cash1',
if ( client_notes like '%B!%' , 'Cash2',
if ( client_notes like '%C!%' , 'Cash3',
'Cash?' ) ) )
What programmer doesn't like a challenge? None of these work but you probably get the thought process I was going through.
case ( ('%' + client_notes + '%') when (like 'A1') then 'Cash1' else 'Cash2' )
case ( '%A1%' when (like 'A1') then 'Cash1' else 'Cash2' )
case ( ( '%bbb%' like 'A1' ) when true then 'Yah' else 'Nah' )
No cigar on CASE...
You could use a global-function, but then again you could write a database function too and it doesn't really solve what you wanted to do. I recall wanting to use like inside a CASE statement more than once and I don't recall getting it to work. I can also recall trying to do something like case variable_a when '4:20' then 'Celebrate' when > '4:20' then 'Too late' else 'Later'
This compiles but do not think it would work due to the comparison variable needing to be different.
case( if ( client_notes like '%' + client_notes + '%', client_notes , 'X')
when 'A1' then 'Cash1'
when 'A2' then 'Cash2'
else 'use nested if instead')
Well, if you wanted to try it as a datawindow computed field, you could use the "brute force" method.
CASE (client_notes
WHEN 'A!' THEN 'Cash'
WHEN 'A!, B!' THEN 'Cash, Check'
WHEN 'A!, B!, C!' THEN 'Cash, Check, Money Order'
WHEN 'A!, B!, C!, D!' THEN 'Cash, Check, Money Order, Card'
WHEN 'B!' Then 'Check'
...
Else '')
In the RetrieveRow event, it might look like this:
string ls_result = ''
string ls_client_notes
If row > 0 then
ls_client_notes = this.getItemString( row, 'client_notes')
If pos( ls_client_notes, 'A!' ) > 0 then ls_result = 'Cash'
If pos( ls_client_notes, 'B!' ) > 0 then
If len( ls_result ) > 0 then
ls_result += ', Check'
Else
ls_result = 'Check'
End if
End if
If pos( ls_client_notes, 'C!' ) > 0 then
If len( ls_result ) > 0 then
ls_result += ', Money Order'
Else
ls_result = 'Money Order'
End if
End if
If pos( ls_client_notes, 'D!' ) > 0 then
If len( ls_result ) > 0 then
ls_result += ', Card'
Else
ls_result = 'Card'
End if
End if
this.setItem( row, 'client_notes', ls_result ) // if you want it back in the same column
End if
-Paul-

asp.net MVC 4 webgrid with an IF statement

I have a column that needs to check a field of the object, when the field equals 2 then another has to multiply with -1.
The problem is I do not know the syntax to do this inside the creation of my gridview. Could someone give an example how this works?
#(invoice.dc.Equals(2)?String.Format("{0:0.00}", invoice.totv * -1): String.Format("{0:0.00}", invoice.totv))
This code sample i have to accomplish inside the creation of the gridview. So if field: dc equals 2 show invoice.totv * -1 else show invoice.totv.
This is what i have tried:
grid.Column("", "PDF", format:
(item) => if(#item.dc == 2)
{
String.Format("{0:0.00}", #item.totv * -1) ;
}
else
{
String.Format("{0:0.00}", #item.totv) ;
})
How about
grid.Column("", "PDF", (item) => String.Format("{0:0.00}", item.dc == 2 ? item.totv * -1 : item.totv));

Duplicate values in multiple textbox

How to validate whether the text in multiple textboxes are unique from each other.
It looks like that in asp.net but its not a valid syntax
bool hasNoDuplicate = (txtEmergencyName1.Text.Trim() <> txtEmergencyName2.Text.Trim() <> txtEmergencyName3.Text.Trim <> txtEmergencyName4.Text.Trim);
I am looking for an efficient appraoch, kind of lambda expression or inbuilt in asp.net
Since you're asking for lambda, here's a linq approach.
var allTxt = new[] { txtEmergencyName1, txtEmergencyName2, txtEmergencyName3, txtEmergencyName4 };
var allText = allTxt.Select((txt, i) => new { Text = txt.Text.Trim(), Pos = i + 1 }).ToList();
bool hasNoDuplicate = !allText.Any(t => allText.Skip(t.Pos).Any(t2 => t.Text == t2.Text));
Put all relevant TextBoxes in a collection like an array and use Enumerable.Any. By skipping all before the current textbox you avoid checking the TextBoxes twice.
If all relevant TextBoxes are in a container control like a Panel, you could also use Enumerable.OfType to find them:
IEnumerable<TextBox> allTxt = this.EmergencyPanel.Controls.OfType<TextBox>();
Side-note: it's premature optimization anyway to look for the most performant way to validate some controls. This is nothing what you are doing constantly and there are never millions of controls. Instead you should look for the shortest or most readable approach.
You can use and && or or || operator accordingly
bool isDuplicate=(txtEmergencyName1.Text.Trim() == txtEmergencyName2.Text.Trim()
&& txtEmergencyName2.Text.Trim() == txtEmergencyName3.Text.Trim);
it will set true or false in the isDuplicate variable.
Edit 1
bool isDuplicate=(txtEmergencyName1.Text.Trim() == txtEmergencyName2.Text.Trim()
&& txtEmergencyName2.Text.Trim() == txtEmergencyName3.Text.Trim
&& txtEmergencyName1.Text.Trim() == txtEmergencyName3.Text.Trim
);
You could also do something like
var test = new TextBox();
var AlltBox = new List<TextBox>() { new TextBox() };
for(int i=1; i == 5;i++)
AlltBox.Add((TextBox)this.FindName("txtEmergencyName"+i));
bool exist = AlltBox.Any(tb => ((tb.Text == test.Text)&& tb.Name != test.Name));
but i don't know about the performance

Does ternary operator can only do one kind of condition?

ternary make code concise and readable, I'm curious about how to change the following if condition to ternary operator:
var1 = if(true){'a'};
I try the following
var1 = true? 'a': ;
since it require nothing to do with false condition, I leave blank after :, but apparently it gives me a error.
Is there a way to do this?
--------update---------
The intention of using the above example is that I want to simplify the problem, however it made everyone more confuse, so I post my original code:
if($_SERVER['REQUEST_METHOD'] == 'GET'){ $sub_count = 0; }
$sub_count = $_SERVER['REQUEST_METHOD'] == 'GET'? 0 : ;
how to change the if condition to ternary ?
$sub_count = null;
$sub_count = $_SERVER['REQUEST_METHOD'] == 'GET'? 0 : null;
// To check:
if(!isset($sub_count))
{
// Do something because $_SERVER['REQUEST_METHOD'] != 'GET'
} else {
if($sub_count===0)
{
// REQUEST METHOD IS GET
}
}

LINQ sorting, doesn't work

I have a LINQ query like this:
from i in _db.Items.OfType<Medium>()
from m in i.Modules
from p in m.Pages
where i != null && i.Type == 1 && i.Published == true && p.PageId == 2
select p
I use the query like this because I have strongly typed view (ASP.NET MVC).
I need to have items sorted by the i.Sort property. orderby i.Sort and i.OrderBy(it => it.Sort) doesn't work. How can I fix this?
When sorting with Linq you usually give OrderBy a property, and eventually an IComparer, not a sorting function. For example:
class Person {
public int Age {get; set;}
}
public static void Main() {
var ps = new List<Person>();
ps.Add(new Person{Age = 1});
ps.Add(new Person{Age = 5});
ps.Add(new Person{Age = 3});
var sorted = ps.OrderBy(p => p.Age);
foreach(p in sorted) {
Console.WriteLine(p.Age);
}
}
Here Linq will know how to correctly sort integers.
Without giving more context (such as what exactly is i.Sort, what is its purpose, what do you want to do with it), it would be difficult to be more specific to your problem.
However, I'm pretty sure you are misunderstanding OrderBy: you should give it a lambda expression that identifies a property of the objects contained in your sequence, and then Linq will sort your sequence according to the usual order of the type of that property (or according to another order you define for that type, by using IComparer).
Let's say your Pages include page-numbers among their properties. Let's pretend this property is called "pagenumber". You would then add the following 'orderby' line between the 'where' and 'select' lines.
// (snip...)
where i != null && i.Type == 1 && i.Published == true && p.PageId == 2
orderby p.pagenumber
select p
Or maybe you don't have page numbers, but only page titles. You would do nearly the same thing:
where i != null && i.Type == 1 && i.Published == true && p.PageId == 2
orderby p.title
select p
Just from reading your code, I can't tell what criteria should be used for sorting. You need some kind of ordered element, an id number, a page number, or some text can be alphabetized.
from i in _db.Items.OfType<Medium>().OrderBy(x => x.Sort)
...

Resources