I have a checkbox that I want to verify the checkbox (same names with different values) and to enable/disable if value is an E and it is checked.
function DisableETP() {
var kk = document.getElementsByName('RQR')
for (var i = 0; i < kk.length i++) {
if (kk[i].value == 'E'
and kk[i].checked == true) {
document.getElementById('cp2').disabled = true
document.getElementById('cp2').value = ""
document.getElementById('rtp2').disabled = true
document.getElementById('rtp2').value = ""
document.getElementById('qty2').disabled = true
document.getElementById('qty2').value = ""
document.getElementById('cp3').disabled = true
document.getElementById('cp3').value = ""
document.getElementById('rtp3').disabled = true
document.getElementById('rtp3').value = ""
document.getElementById('qty3').disabled = true
document.getElementById('qty3').value = ""
} else {
document.getElementById('cp2').disabled = false
document.getElementById('cp2').value = ""
document.getElementById('rtp2').disabled = false
document.getElementById('rtp2').value = ""
document.getElementById('qty2').disabled = false
document.getElementById('qty2').value = ""
document.getElementById('cp3').disabled = false
document.getElementById('cp3').value = ""
document.getElementById('rtp3').disabled = false
document.getElementById('rtp3').value = ""
document.getElementById('qty3').disabled = false
document.getElementById('qty3').value = ""
}
}
Do I need to add ; to the lines or...?
Thank you.
In this particular case you don't strictly need semicolons, no.
However your first if statement is invalid because and is not correct javascript syntax, it should be &&.
if (kk[i].value == 'E' && kk[i].checked == true)
You should end each line in javascript with a ;
You also cannot use 'and' as a keyword in an if statement, you have to use && instead.
Its also best to check a boolean value with === instead of ==
Related
I need to change the output of a method a bit: Execute the function, and if it's a empty string, then convert it to a "1". How can i write this short on just one line?
var = some_really_long_method(foo)
var = "1" if var == ""
I tried below, but that does call the method twice, right?
var = some_really_long_method(foo) == "" ? "1" : some_really_long_method(foo)
You could use Object#then:
def some_really_long_method
p 'called'
p res = ["", "10"].sample
res
end
var = some_really_long_method.then { |m| m == "" ? '1' : m }
You can check for yourself that the method is called once.
Newlines are optional in Ruby, they can always be replaced with either an expression separator (;), a keyword (e.g. then, do), or sometimes just whitespace.
Therefore, every program, no matter how complex, can always be written in one line, just by removing the linebreaks:
var = some_really_long_method(foo); var = "1" if var == ""
var = "1" if (var = some_really_long_method(foo)) == ""
var plays = Dictionary<Int,Int>()
var done = false
var aiDeciding = false
if (!plays[sender.tag] && !aiDeciding && !done){
self.setImageForSpot(sender.tag, player:1)
this line says Optional type'Bool' cannot be used as a boolean; test
for '!nil=' instead. I did what it said, but it made it even worse.
checkForWin()
aiTurn()
}
when you check plays[sender.tag] this could return an Int or nil. It is not a bool. So try:
var plays = Dictionary<Int,Int>()
var done = false
var aiDeciding = false
if ((plays[sender.tag] == nil) && !aiDeciding && !done){
self.setImageForSpot(sender.tag, player:1)
}
Here is your working if statement:
if (plays[sender.tag] != nil) && !aiDeciding && !done{
}
In your code you did !plays[sender.tag] which doesn't return bool but it returns Int values because you declared a dictionary which have Int keys and values.
so you need to check if that is nil or not and if it isn't nil you can proceed.
I have a EXGrid in a form and want to add button to a cell that either say "Edit" and "Remove", or Have icons for the two. Here is the code I have so far.
With grdGrade
.BeginUpdate
'.DataSource = objCo.DAL.Connection.Execute(sql)
.DefaultItemHeight = 21
.TreeColumnIndex = -1
.MarkSearchColumn = False
.FullRowSelect = exItemSel
.DrawGridLines = exAllLines
.SortOnClick = exNoSort
.BackColorHeader = grdGrade.BackColor
.Appearance = Flat
.BackColorAlternate = Me.BackColor
.SelBackColor = RGB(224, 224, 224)
.SelForeColor = vbBlack
.UseTabKey = True
.Font = Me.Font
.EndUpdate
End With
Set col = grdGrade.Columns.Add("Actions")
col.AllowDragging = False
col.Width = grdGrade.Width * 0.05
col.Editor.AddButton "Edit", , 0
col.Editor.AddButton "Remove", , 0
col.Editor.EditType = ButtonType
Thanks so much for all your help.
I finally got it. When loading the grid I used:
Me.grdGrade.Items.CellValue(h, "Actions") = " Edit "
Me.grdGrade.Items.CellHasButton(h, "Actions") = True
s = Me.grdGrade.Items.SplitCell(h, "Actions")
Me.grdGrade.Items.CellValue(0, s) = " Remove "
Me.grdGrade.Items.CellHasButton(0, s) = True
and it works like a charm
I need to build a where clause at runtime but I need to do an OR with the where clause. Is this possible?
Here is my code. Basically "filter" is a enum Bitwise, son hence filter could be equal to more than 1 of the following. Hence I need to build up the where clause.
If I execute the WHEREs separately then imagine if I do the Untested first, and it returns 0 records that means I can't execute a where on the Tested because its now 0 records.
I will put some pseudo-code below:
string myWhere = "";
if ((filter & Filters.Tested) == Filters.Tested)
{
if (myWhere != "" ) myWhere =myWhere + "||";
myWhere = myWhere " Status == "Tested";
}
if ((filter & Filters.Untested) == Filters.Untested)
{
if (myWhere != "" ) myWhere =myWhere + "||";
myWhere = myWhere " Status == "Untested";
}
if ((filter & Filters.Failed) == Filters.Failed)
{
if (myWhere != "" ) myWhere =myWhere + "||";
myWhere = myWhere " Status == "Failed";
}
// dataApplications = a List of items that include Tested,Failed and Untested.
// dataApplication.Where ( myWhere) --- Confused here!
Is this possible?
I don't want to include lots of "IFs" because there are lots of combinations i.e. no filter, filter= tested Only, filter = Untested and Tested ... and lots more.
If you have this:
IEnumerable<MyType> res = from p in myquery select p;
You can define a
var conditions = new List<Func<MyType, bool>>();
conditions.Add(p => p.PropertyOne == 1);
conditions.Add(p => p.PropertyTwo == 2);
res = res.Where(p => conditions.Any(q => q(p)));
And now the trick to make Lists of Funcs of anonymous objects (and you can easily change it to "extract" the type of anonymous objects)
static List<Func<T, bool>> MakeList<T>(IEnumerable<T> elements)
{
return new List<Func<T, bool>>();
}
You call it by passing the result of a LINQ query. So
var res = from p in elements select new { Id = p.Id, Val = p.Value };
var conditions = MakeList(res);
var statusTexts = new List<string>(); // Add desired status texts
dataApplication.Where(item =>
statusTexts.Any(status => item.Status == status))
Use HashSet<> for statuses, then .Contains will be O(1) instead of usual O(n) for List<>:
var statuses = new HashSet<string>() {"a", "b", "c"};
var list = new[] {
new { Id = 1, status = "a"},
new { Id = 2, status = "b"},
new { Id = 3, status = "z"}
};
var filtered = list.Where(l => statuses.Contains(s => l.status == s));
I want to generate dynamic query to check manage the where clause with number of parameters available...if some parameter is null i don't want to include it in the where clause
var test = from p in _db.test
where if(str1 != null){p.test == str} else i dnt wanna check p.test
I have around 14 parameters for the where clause
need help,
thanks
You can do it in steps:
// set up the "main query"
var test = from p in _db.test select _db.test;
// if str1 is not null, add a where-condition
if(str1 != null)
{
test = test.Where(p => p.test == str);
}
In addition to #Fredrik's answer, you can also use the short-circuit rules when evaluating boolean expressions like so:
var test = from p in _db.test
where str1 == null || p.test == str1;
Edit If you have lots of strings to test, (str1, str2, etc...) then you can use the following, which will be translated to an SQL IN clause:
var strings = new List<string>();
if (str1 != null) strings.Add(str1);
if (str2 != null) strings.Add(str2);
if (str3 != null) strings.Add(str3);
...
var test = from p in _db.test
where strings.Contains(p.test);
It's even easier if your strings are already in a collection (which, if you've got 14 of them, I assume they would be...)
Consider param1 and param2 are the parameters. Your query should be as under:
string param1 = "Value1";
string param2 = "Value2";
var q = from bal in context.FxBalanceDetails
where (string.IsNullOrEmpty(param1) || bal.Column1 == param1)
&& (string.IsNullOrEmpty(param2) || bal.Column2 == param2)
select bal;
This will ensure that the where clause gets applied for the particular parameter only when it is not null.
var test =
from p in _db.test
where p.str1 != null ? p.str1 : ""
select p;
Do you check the strings against the same Field of the entity?
If so you can write something like:
var strings = new[] { "foo", "bar", "ok", "", null };
var query = dataContext.YourTable.AsQueryable();
query = strings.Where(s => !string.IsNullOrEmpty(s))
.ToList()
.Aggregate(query, (q, s) => q.Where(e => e.YourField == s));
EDIT:
The previous solution is overcomplicated:
var strings = new[] { "foo", "bar", "ok", "", null }.Where(s => !string.IsNullOrEmpty(s))
.ToList();
var query = dataContext.YourTable.Where(e => strings.Contains(e.YourField));