ng-show based on dynamic conditions from database - javascript

We are working on some dynamic form where user can create questions in the admin panel and put conditions to show them based on other questions values.
ng-show="((UserCode==10003 && Name=='Ankur') ||(StateId=='NY'))"
this works fine show/hide question based on other questions value, But the conditions are dynamic and created runtime based on the users selected values in database.. how can we add ng-show conditions dynamic ?
I tried this
$scope.Condition = "((UserCode==10003 && Name=='Ankur') ||(StateId=='NY'))";
and ng-show="{{Condition}}" but this didn't worked.
UserId, Name & StateId are other questions on the page.
Please let me know if i missed some info.
thanks

Try with method:
ng-show="condition()"
$scope.condition = function() {
/* You calculate your condition here */
var condition = ... ;
return condition;
}

You know this is a string and not a condition that will return true or false.. ?
$scope.Condition = "((UserCode==10003 && Name=='Ankur') ||(StateId=='NY'))";
This should be correct
$scope.Condition = (UserCode==10003 && Name=='Ankur') ||(StateId=='NY');

$scope.Condition = "((UserCode==10003 && Name=='Ankur') ||(StateId=='NY'))";
this will never work because it is not a function or condition that returns a true or false,
try with thist
$scope.condition = function(_usercode, _name, _state_id){
return (UserCode == _usercode && Name == _name) || (StateId == _state_id);
}
and in html ng-show="condition(10003,'Ankur','NY')"

Related

transfer date into input field from one asp form into another using session state

I need to transfer date from one asp form to another based on some checks.
In the source page I do the check and send as following:
if (txtFinalized.Text == "" && rblPhysicalrecords.SelectedValue=="1" && ddlResponsibility.SelectedIndex == 5)
{
//String s = Request.QueryString["finlalisationDate"] = txtFinalized.Text;
Session["finlalisationDate"] = txtFinalized.Text;
}
Then I try to read the value in the target form but so far I can't get the resut inserted into the input field as I need.
txtFinalised.Text = (string)(Session["finlalisationDate"]);
Do I need to write a method in javascript to fetch the result and insert it to the field and if yes how do I do that?
Your condition block has a flaw, it says txtFinalized must be empty to set a value to your session variable.
For learning and and understand session you could write your code like this...
//remove txtFinalized from condition
if (rblPhysicalrecords.SelectedValue=="1" && ddlResponsibility.SelectedIndex == 5)
{
//check if textFinalized NOT is null or empty
if (!String.IsNullOrEmpty)
{
Session["finlalisationDate"] = txtFinalized.Text;
}
//if textFinalized is empty set session to a value just to see some text
else
{
Session["finlalisationDate"] = "n/a";
}
}
Now when you load your second form you will always see something in your textFinalized textbox and from what you see you know if the user made some input in the first form.
You can modify your condition block like below
if (!String.IsNullOrEmpty(txtFinalized.Text) && rblPhysicalrecords.SelectedValue=="1" && ddlResponsibility.SelectedIndex == 5)
{
//String s = Request.QueryString["finlalisationDate"] = txtFinalized.Text;
Session["finlalisationDate"] = txtFinalized.Text;
}

Append class with if statement

I just created a variable for a specific site of mine but I want to append a class entitled "partner-text" if I'm actually on that site. If I'm on a different site then don't append it. How can I do that?
use(function() {
var inPartnerPath = currentPage.getPath().indexOf("partners_Skate_Daily");
// to check if the site is under partners_Skate_Daily folder if yes then it should return true
var isPartner = (inPartnerPath != -1) ? 'true' : 'false';
return {
isPartner: isPartner
};
});
'true' and 'false' are strings. You want to use the boolean values true/false.
You don't even need the ?: here, != already returns you a boolean.
var isPartner = inPartnerPath != -1;

logic error in javascript code

I am trying to write some logic that will check for the existence of a specific record in two tables.
If the row exists in either table, I want to return TRUE.
For table A, when the record is added to the table, the id field looks like "123".
For table B, the same record would have the id of "a123". However, the value i have to search for the record is "row_123".
This is what my code looks like right now :
var checkForDuplicates = function(row_id) {
return !!($('#existing_members').find('#' + row_id.replace('row_','')).length || $('#selected_users').find('#' + row_id.replace('row_','a').length) );
};
I want the function to return true if the record exists in either table.
However, this statement returns true in cases when it should be false.
What I've tried so Far
I've been playing around in the console to make sure that my logic is correct:
!!(1 || 0) //returns true
!!(0 || 0) //returns false
!!(0 || 1) //returns true
I'm also currently reviewing the replace statements to make sure the find() is being supplied the right strings.
But a second pair of eyes to confirm that my logic is correct would be appreciated.
Thanks
EDIT 1
The solution, using Max's suggestion would be:
var checkForDuplicates = function(row_id) {
var parts = row_id.split('_');
var tableB = '#a'+ parts[1];
var tableA = '#' + parts[1];
return !!($('#existing_members').find(tableA).length || $('#selected_users').find(tableB).length);
}
However, as Ankit points out, I just had a typo in my original code. So this would be my final answer / solution:
var checkForDuplicates(row_id) {
return !!( $('#existing_members').find('#' + row_id.replace('row_', '')).length || $('#selected_users').find('#' + row_id.replace('row_','a')).length);
}
Your code has a typo at the end of return statement
...'a').length)); //it returns object which always evaluates to true
it should be
...'a')).length);
DEMO
var checkforduplicates = function(row_id){
//row_id looks like "row_123"
return !!($('#tableA').find('#' + row_id.replace('row_','')).length || $('#tableB').find('#' + row_id.replace('row_','a')).length );
}
alert(checkforduplicates("row_123"));
<table id=tableA><tr><td id="123">123 ID</td></tr></table>
<table id=tableA><tr><td id="a13">a13 ID</td></tr></table>
Corrected few issues to make the code more efficient:
var checkforduplicates = function(row_id) {
var id = row_id.split('_')[1]; // [ 'row', '123']
return $('#'+id).length || $('#a'+id).length;
}
No need for !! as operator || produces boolean result (true or
false)
Used $('#'+id) as more efficient jQuery selector
Removed unnecessary find(..) call
Eliminated unnecessary parenthesis (which had an issue)
I want the function to return true if the record exists in either table.
var checkForDuplicates = function(row_id) {
row_id = row_id.substring(4); // 'row_123' -> '123'
var table_A_row_id = row_id;
var table_A_row_exists = $('#tableA').find('#' + table_A_row_id).length > 0;
var table_B_row_id = 'a' + row_id;
var table_B_row_exists = $('#tableB').find('#' + table_B_row_id).length > 0;
return table_A_row_exists || table_B_row_exists;
};
of course it is returning the opposite of the things you want, cause you are using !!.
! is used to negotiate the return value of the specific function/variable e.g.:
if(!var_x == false)
this example only works if var_x is true.
So please be aware to avoid !! ;)
Please use a single ! instead!

checking values before submitting form

Before I submit a form I want to check the values in the input.
Here I'm checking if a value is NOt equal to .5 or 1. or not a empty string.
form.onsubmit = function(e) {
var ftimes = document.getElementsByClassName("add_timebox");
var fflag = 0;
for(i=0;i< ftimes.length;i++) {
var value1 = ftimes[i].value;
console.log(value1);
if ( value1 !==.5 ||value1 !== 1 || (!IsStringEmpty(value1)) ){
fflag = 1;
console.log('inside');
}
}
if(fflag==1) {
alert('enter again' );
return false;
}
I have made many changes to the IF statement to try to get it correct.
But it is still going in the loop even when I know if shouldn't.
For example when i submit the form and i have one input value equal .22
then it should only give me 1 'inside' but in keeps repeating:
inside
.22
(empty string)
inside
....
You do not show how you are implementing your IsStringEmpty method, but if you are using something like this, then any number is also a non-empty string, so your if statement will always run.
function IsStringEmpty(str) {
return (!str || 0 === str.length);
}
So you need to change your ORs with ANDs, or it will never check the number conditions.
You can check if the value is not an empty string and is different from 0.5 and 1. Then your condition should be like this.
if (!IsStringEmpty(value1) && value1 !== 0.5 && value1 !== 1)
But, you are getting the value from a form, so it will be a string. Therefore, you are comparing strings and you need this.
if (!IsStringEmpty(value1) && value1 !== ".5" && value1 !== "1")
Although you will probably want to compare floats, in which case you need this.
if (!IsStringEmpty(value1) && parseFloat(value1) !== .5 && parseFloat(value1) !== 1))
So basically, when you enter 1, .5 or and empty string in all of the form fields, you skip the inside block. But if you have any other value in any of the fields, then the flag will be set to 1. If that is not what you meant, please update your question to be more specific.
Please check Plunker here.
Hope this helps.
you have to add a break; statment in your if condition once the if condition is satisfied.
if ( value1 !==.5 ||value1 !== 1 || (!IsStringEmpty(value1)) ){
fflag = 1;
console.log('inside');
break;
}

Checking textbox if it's empty in Javascript

I wrote a simple html file with a textbox and submit button, and it generates and document.write's a response dependant on what you submit. I want to have it generate a response saying to enter content if the box is empty. The textbox's id is chatinput, so I have the following the code in the beginning
var chatinput_box=document.getElementById('chatinput');
var chatinput=chatinput_box.value;
Then a have a conditional, although I can't get it to work correctly; I've tried
if(chatinput==""){}
if(chatinput.length=0){}
if(chatinput=null){}
and others but none have worked correctly. Does anyone have another idea?
It should be this:
var chatinput = document.getElementById("chatinput").value;
if (chatinput == "" || chatinput.length == 0 || chatinput == null)
{
// Invalid... Box is empty
}
Or shorthanded:
if (!document.getElementById("chatinput").value)
{
// Invalid... Box is empty
}
The = assigns a value whereas == checks whether the values are equal.
Just offering an alternative, not trying to steal thunder ...
Create an isEmpty function to reuse on a variety of items.
function isEmpty(val){
return ((val !== '') && (val !== undefined) && (val.length > 0) && (val !== null));
}
Then you can apply it to whatever element you want:
if(!isEmpty(chatinput)){
// hooray its got a value!
}
Not exactly original, its the concept stolen from PHP, but it comes in handy a lot.

Categories

Resources