I've 5 cells in a table. If the value are empty for those cells i need to disable them.
I can do something like this for each cells which work's.
function cellOne(params) {
if (params.value === null || params.value === undefined) {
return false
} else {
return true;
}
}
"CellOne": { disabled:cellOne }
is there any other way to check null value of each cell and add disable property instead of creating multiple function for each cells. Please help
You don't need to go for typescript code.You can do it in the template itself.
<your-cell [disabled]="!params.value"></your-cell>
Your function would return the same thing as :
function cellOne(params) {
return params.value != null
}
which you can easily inline instead of having a separate function for that.
To check for params that might be null or undefined too, you can use :
return params && params.value != null
Related
I'm trying to check if a key exists in the localStorage in JavaScript. I've searched for an answer online and I've found that the method getItem will return null if the key does not exist.
This is the code I've been using:
if (localStorage.getItem('token') !== null) {
return <div className="inform-text">Placeholder</div>
}
else {
return <div className="inform-text">Sign In</div>
}
But it always returns the Placeholder div, meaning it's always true. When I run console.log(localStorage.getItem('token')), it doesn't return null but instead a blank space like: ''. I could of course change the null to '' in the if statement but I'm curious as to why this is happening.
I have module where I need filter the data, base on date value and via seaching the words. Right now I experience problem. by the way I using laravel for the backend and React js for front end. but I will give you the scenario happen right now.
1. When I start fire searching the data this is working properly.
2. When I Click the date filter the data is filtering base on the date value.
3. This is the problem, When I try to change the searching text value in the textbox the data is not filtering anymore, how to solve that problem. my first thinking to solve it is to make the date filter null but I really don't know how to do it. but to make more understandable I will show you guys my code.
Code Sample:
let { loadReceiveChecks, receiveCheckResponse, keyWords, searchWord } = this.props
let keyStatus = keyWords.toLowerCase();
if(keyStatus) {
// this is for date range
return Object.keys(receive_date).some(
(key) =>
typeof receive_date[key] === "string" &&
receive_date[key].toLowerCase().includes(keyStatus)
);
}else if(searchWord) {
// this is for search text input box
return Object.keys(item).some(
(key) =>
typeof item[key] === "string" &&
item[key].toLowerCase().includes(searchWord)
);
}else {
// return all the list data.
return Object.keys(item).some(
(key) =>
typeof item[key] === "string" &&
item[key].toLowerCase().includes(searchWord)
);
}
From the limited information I have, I conclude that your best option is to set the state to null when the date range was triggered. It could look like this:
let {
loadReceiveChecks,
receiveCheckResponse,
keyWords,
searchWord,
updateParentState, // function which contains this.setState()
} = this.props;
let keyStatus = keyWords.toLowerCase();
if (keyStatus) {
updateParentState(null); // set parent state to null
// this is for date range
return Object.keys(receive_date).some(
key =>
typeof receive_date[key] === "string" &&
receive_date[key].toLowerCase().includes(keyStatus)
);
} else if (searchWord) {
// this is for search text input box
return Object.keys(item).some(
key =>
typeof item[key] === "string" &&
item[key].toLowerCase().includes(searchWord)
);
} else {
// return all the list data.
return Object.keys(item).some(
key =>
typeof item[key] === "string" &&
item[key].toLowerCase().includes(searchWord)
);
}
I have two condition of my same JSON data:
{
"selectionId":124,
"selectionDate":"2070-01-01",
"selectionAudit":null,
"letter":[
{
"letterNo":13,
"letterId":1,
"inout":"out",
"inoutNo":"12332466544",
"inoutDate":"2070-01-01",
"letterIssuedSubBy":null,
"letterFile":null,
"representativeName":null,
"representativeNameEng":null,
"selectionId":124,
"assessmentNo":null,
"entryBy":"user98",
"rStatus":"I",
"others":null,
"signatary":"qwerrt",
"letterBox":null,
"imageFile":null,
"imageTitle":null,
"reminderYesNo":"N"
}
]
}
Same JSON with letter array empty structure :
{
"selectionId":124,
"selectionDate":"2070-01-01",
"selectionAudit":null,
"letter":[]
}
All these values are stored in
var trDataSecondTable; . I tried to compare if the letter is empty or not by using if condition:
if(trDataSecondTable.letter != null) {
console.log("asasfasdfdsfdsfds");
$("#inoutDate").val(trDataSecondTable.letter[0].inoutDate);
$("#inoutNo").val(trDataSecondTable.letter[0].inoutNo);
$("#signatary").val(trDataSecondTable.letter[0].signatary);
}
else
{
console.log("entered in else part");
}
Though "letter":[] is empty it is not entering into else part. While comparing i also tried trDataSecondTable.letter.length != 0 but also it is not entering into else part.
Your condition should check for both null and the length:
if (trDataSecondTable.letter != null && trDataSecondTable.letter.length > 0)
Is is important to check for null before accessing the length property as it guarantees you won't try to access the length property on null, which would raise an exception. This is important since data is rarely reliable.
Because of the && operator, if the first check fails, the second check won't be evaluated.
Actually, an even safer way would be to check if the value is truthy, as this will work for null and undefined:
if (trDataSecondTable.letter && trDataSecondTable.letter.length > 0)
You could check the length of the array.
if (trDataSecondTable.letter.length) {
// letter has some elements
} else {
// no elements
}
I think this condition is enough
if(trDataSecondTable.letter.length)
I am using datatables and currently stuck in changing a row to another color if value = INACTIVE, already tried many things but it has really weird error, my codes are :
"createdRow": function (row, data, dataIndex) {
if (data[9] = "INACTIVE") {
$(row).addClass("yellow");
} else {
$(row).addClass("white");
}
}
This code change all color row, but i want only change value INACTIVE
Thanks for the help!
You have a typo in your code.
In your if statement use == instead of =.
"createdRow": function (row, data, dataIndex) {
if (data[9] == "INACTIVE") {
$(row).addClass("yellow");
} else {
$(row).addClass("white");
}
}
In the condition, you are assigning the value "INACTIVE" to the data[9] instead of comparing the value. Subsequently, the condition only checks whether data[9] has some value, which is true, and class .yellow is always added.
So the condition should be like this if (data[9] == "INACTIVE") or rather if (data[9] === "INACTIVE") to perform check without type conversion.
In your if statement you are using a single '=' which is used for assignment. You should use double '=' to compare if the value is the same and triple '=' to compare if the value and the data types are the same.
You are also only checking index 9 of data. In your function you seem to also be passing in the index, you should instead change your code to something like this.
if ( data[ dataIndex ] === "INACTIVE" )
I have an array of objects that represent form fields. I want to make sure that none of the elements in the array have their initial values. As in all fields need to be updated. The initial value is below. I'd like a simple function that checks each element and ensures none of these properties have the default fields.
[
{
key: 0,
name: '',
typeLabel: '',
typeValue: 0,
value: '',
}
...
]
Looking for help on improving this
collection.every((obj) => {
if (obj.key === 0) return false;
if (obj.name === '') return false;
if (obj.typeLabel === '') return false;
if (obj.typeValue === 0) return false;
if (obj.value === '') return false;
return true;
});
EDIT: Actually just thought of this
collection.every((obj) => {
const values = Object.values(obj);
return values.includes(0, '');
});
To be honest I'm not sure how much more better you can make it, it looks pretty decent to me! Any changes I'd make would be subjective changes that don't actually matter. Curious what other people will answer with though, maybe I'm missing something :)
I mean you could do this and it's arguably less if/elses and uses a named function to explain what it is doing which is maybe nicer? But meh I don't think this matters all that much:
function inputValueHasBeenSet(inputObject) {
return !!inputObject.key &&
!!inputObject.name &&
!!inputObject.typeLabel &&
!!inputObject.typeValue &&
!!inputObject.value;
}
collection.every(inputValueHasBeenSet);