How to verify not null and skip row if it is? - javascript

I am trying to compare 2 dates to be able to display the data in a table.
I am doing the following:
${this.dateToCompare.getTime()} > ${row.CreateDate.getTime()}
The problem is, CreateDate has a null value on some rows so the above is not working.
I have also tried this:
<div ng-if="${row.CreateDate!==null} && ${this.dateToCompare.getTime()} > ${row.CreateDate.getTime()}"
I need to check first if the row is null or not, but even when it is null, it is continuing after the && and when it hits the dateToCompare.getTime, it is throwing a syntax error because I am calling getTime on a null value.
Is it possible to tell ng-if to not run it if null and to skip that row?
What is the best way to be able to fix this problem and be able to run the getTime() on it?
Thank you

I had to create a function:
convToTime(date: Date) {
if (date !== null)
{
this.tDate = new Date(date.toString().substring(0,10));
return this.tDate.getTime();
}
}
and called it this way:
${this.convToTime(row.CreateDate)}
All working properly now and as needed.

Related

Remove element from DOM if a certain Json property is null or ""

I want to remove an element from my DOM if a JSON Property is null or an empty string. But i cannot make it work.
Here's my code.
function projectJsonUi(){
$.getJSON('/project.json',function(response){
let foo_jpg = response.ressources.foo_jpg;
let foo_pdf = response.ressources.foo_pdf;
if(foo_pdf == null && foo_jpg == null){
$("#myDiv").remove();
}
}
my JSON looks like this;
{"ressources":{
"foo_jpg": null,
"foo_pdf": null,
...
},...
}
i made a console.log() of foo_jpg i just cut it out of the code for readability, it returned null. I don't get any errors displayed. I also tried with empty string, same result. The request is valid for i have used its result in other operations successfully.
It would be nice, if someone could help me with my problem.
Well, currently you're only checking null, not an empty string. And considering that both null and "" are falsy values, you could just use the less verbose option of Boolean if conditions. Try using this instead:
if (!(foo_pdf && foo_jpg)) {
$("#myDiv").remove();
}
You should also make sure your jQuery code:
$("#myDiv").remove();
Actually links up to an element which I believe would look like this:
<div id="myDiv">Some content that should be hidden</div>
And finally make sure you have jQuery in your HTML page - use this code in your <head> tag:
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
And now your code should be working.

React .trim() is not a function

My application has a landing page with two components in two separate tabs.
The code from the first component that is causing the crash looks like this:
for (let key in linegraphdata) {
linegraphdata[key].price = Number(
linegraphdata[key].price.trim().slice(1)
);
linegraphdata[key].month = parseDate(linegraphdata[key].month);
}
When I load into my application initially it doesn't crash, loads the data from the first tab fine. I'll click into the second tab and when I eventually click back the whole application crashes and the log gives me this error:
Uncaught TypeError: linegraphdata[key].price.trim is not a function
It must have something to do with how React handles refreshing components once already rendered, could anyone help me figure it out please :)
You're setting what was a string to a number, and numbers don't have the trim() method on them. That's why it works the first time (when it's a string) and not the second time around:
array[key] = Number(array[key].trim());
So that code must be executing more than once.
linegraphdata[key].price is either null or not a string.
If there is a value, you can try using linegraphdata[key].price.toString().trim().slice(1)
You can check that price is a string with this ternary. If it's not a string it will set the value to -1
linegraphdata[key].price = Number(
typeof linegraphdata[key].price == 'string' ? linegraphdata[key].price.trim().slice(1) : -1
);

What to do if there is no data in localStorage?

I'm in a dilemma, I have a search engine which I keep the last results, everything perfect until there.
The problem is that I do not know what to do if I do not have items already saved, ie if it is the first time I search.
if(localStorage.getItem("searchResults") === null) {
// I do not know what to do here ...
}
else {
// Here the code is supposed to do what it has to do
}
Should not I do anything, should I save an empty string, or would I have to change the logic I'm working on?
What are your friends, what are you doing? Thank you
I would have a variable called "noResults", and set it to false, when search results are 0, or you can make it fetch results from server. It's all about context and logic
You could set it to N/A:
if(localStorage.getItem("searchResults") === null) {
localStorage.setItem("searchResults", "N/A");
}
else {
// Here the code is supposed to do what it has to do
}
Or use the following, and when you check if searchResults is null, last search result should be empty.
if(localStorage.getItem("searchResults") !== null) {
// Here the code is supposed to do what it has to do
}
Maybe you don't need an if statement. The if statement provides you with code blocks that can be executed for truthy or falsely expressions.
Instead, use a logical OR || to define a default value.
var results = localStorage.getItem('searchResults') || 'No results...';

cannot read shorthand javascript/query

i've spent ages trying to understand bootstrap's navigation bar, mainly by spending 4-5 days reading stackoverflow posts
& finally i think i've found an answer that helps!!!
trouble is, i can't understand the accompanying javascript/jquery code. i'm guessing its a shorthand version of js or something but just what it means i cannot decipher
basically, its the javascript code that appears on this jsfiddle page
$('.navbar').on('show', function () {
var actives = $(this).find('.collapse.in'),
hasData;
if (actives && actives.length) {
hasData = actives.data('collapse')
if (hasData && hasData.transitioning) return
actives.collapse('hide')
hasData || actives.data('collapse', null)
}
});
so, if anyone can explain to me what the code is doing on a line by line basis it'd be really cool
the first line i understand. its the weird-ass syntax in the next 6 lines that have me mystified
var actives = $(this).find('.collapse.in'),
hasData;
This creates two variables. One with elements picked from current scope that match the selector .collapse.in, and one empty variable.
if (actives && actives.length)
If actives exists and contains more than zero elements, do the following...
hasData = actives.data('collapse')
Retrieve arbitrary data stored under the key collapse. See https://api.jquery.com/jquery.data/ for more info.
if (hasData && hasData.transitioning) return
If hasData exists and hasData.transitioning is truthy, stop function execution.
actives.collapse('hide')
Call the collapse function on actives. This is not a native jQuery function, so you'll have to look up whatever plugin it comes from to make sense of the argument being passed in.
hasData || actives.data('collapse', null)
If hasData is truthy, skip this line. Otherwise, set the arbitrary data in actives variable to null.

Javascript | Adding value to String Returns an Undefined

I am new to Javascript so please forgive my ignorance.
I got a dynamic table that increases with one row each time the user enters a value.
I'm trying to get all values from the new created column to appear in a TD (id=scm) seperated with a dot.
The following works but returns an "UNDEFINED" at the start.
var serial = document.getElementById("serial");
var scm = document.getElementById("scm");
if (scm === ""){
scm.innerHTML = "start";}
else {
scm.innerHTML = scm.value += "." + serial.value }
All help is welcome!
I think at the start, the table might not be initialised so you're correct checking for scm === "" but you also need to check for undefined (since the table is empty when it loads, right?) so the logic will flow to the else clause and scm will be undefined!
Try adding something like this:
if (!scm || scm === ""){
This will check if scm has no value i.e. undefined, OR if it's an empty string. :)

Categories