I am doing below code in a vue.js application. I would like to do this in a programmatic way to reduce the code size.
display_input(valu) {
if(valu == 'editName') {
this.editName = true
this.editAddress = false
this.editTelephone = false
this.editEmail = false
}
if(valu == 'editAddress') {
this.editName = false
this.editAddress = true
this.editTelephone = false
this.editEmail = false
}
if(valu == 'editTelephone') {
this.editName = false
this.editAddress = false
this.editTelephone = true
this.editEmail = false
}
if(valu == 'editEmail') {
this.editName = false
this.editAddress = false
this.editTelephone = false
this.editEmail = true
}
},
Use the following, it can all be compressed into:
display_input(valu) {
this.editName = valu == 'editName'
this.editAddress = valu == 'editAddress'
this.editTelephone = valu == 'editTelephone'
this.editEmail = valu == 'editEmail'
}
This is the shortest way that I could thought, with the bonus that if you add any attribute you won't need to change it.
display_input(valu) {
Object.keys(this).forEach(key => {
this[valu] = valu == key
})
}
Related
I got ConcurrentModificationException when trying initialize a ArrayList<CheckBox> as blow:
late init var checkBoxArrayList = ArrayList<CheckBox>()
val repeatDays: List<Int> = getRepeatDays() // List of the order of days of the week starts from 1 (monday)
checkBoxArrayList = arrayListOf(
checkBoxMon.apply { this.isChecked = false
this.setOnCheckedChangeListener(this#RepeatOptionsFragment)},
checkBoxTue.apply { this.isChecked = false
this.setOnCheckedChangeListener(this#RepeatOptionsFragment)},
checkBoxWed.apply { this.isChecked = false
this.setOnCheckedChangeListener(this#RepeatOptionsFragment)},
checkBoxThu.apply { this.isChecked = false
this.setOnCheckedChangeListener(this#RepeatOptionsFragment)},
checkBoxFri.apply { this.isChecked = false
this.setOnCheckedChangeListener(this#RepeatOptionsFragment)},
checkBoxSat.apply { this.isChecked = false
this.setOnCheckedChangeListener(this#RepeatOptionsFragment)},
checkBoxSun.apply { this.isChecked = false
this.setOnCheckedChangeListener(this#RepeatOptionsFragment)}
)
repeatDays?.forEach { day ->
// The checkBoxArrayList starts from index 0 (for monday), while the DayOfWeek enum assigns monday to 1.
checkBoxArrayList[day - 1].isChecked = true
}
But when I change the code as below, this issue is gone. I don't understand how the setOnCheckedChangeListener() impact and cause the error?. Could you give me an explanation.
checkBoxArrayList = arrayListOf(
checkBoxMon.apply { this.isChecked = false },
checkBoxTue.apply { this.isChecked = false },
checkBoxWed.apply { this.isChecked = false },
checkBoxThu.apply { this.isChecked = false },
checkBoxFri.apply { this.isChecked = false },
checkBoxSat.apply { this.isChecked = false },
checkBoxSun.apply { this.isChecked = false }
)
repeatDays?.forEach { day ->
checkBoxArrayList[day - 1].isChecked = true
}
checkBoxArrayList.forEach {
it.setOnCheckedChangeListener(this)
}
AnimatedVisibility with initiallyVisible deprecated. How to achieve this animation with new api? Thanks in advance.
#Composable
fun DefaultTransition(content: #Composable () -> Unit) {
AnimatedVisibility(
visible = true,
enter = slideInVertically(
initialOffsetY = { 50 }
) + fadeIn(initialAlpha = 0.3f),
exit = slideOutVertically() + fadeOut(),
content = content,
initiallyVisible = false
)
}
I found the answer.
val visibleState = remember { MutableTransitionState(false) }
visibleState.targetState = true
AnimatedVisibility(visibleState,
enter = slideInVertically(
initialOffsetY = { 50 }
) + fadeIn(initialAlpha = 0.3f),
exit = slideOutVertically() + fadeOut() {
content()
}
this is my declaration of state :
state = {
shouldShow1: Boolean = false,
shouldShow2: Boolean = false,
shouldShow3: Boolean = false,
shouldShow4: Boolean = false,
shouldShow5: Boolean = false,
shouldShow6: Boolean = false,
shouldShow7: Boolean = false,
shouldShow8: Boolean = false,
shouldShow9: Boolean = false,
};
then i change the value of one state each time the user click on button
if (this.state.shouldShow1 == false) {
this.state.shouldShow1 = await true;
this.state.shouldShow2 = false;
this.state.shouldShow3 = false;
this.state.shouldShow4 = false;
this.state.shouldShow5 = false;
this.state.shouldShow6 = false;
this.state.shouldShow7 = false;
this.state.shouldShow8 = false;
this.state.shouldShow9 = false;
.............
and i have a code like this in the render
return(
{this.state.shouldShow1 == true ? Show View A : Show View B }
{this.state.shouldShow2 == true ? Show View A : Show View B }
)
Bernhard Josephus answered resolved my problem the solution is :
i should have used this.setState({ shouldShow1: true, shouldShow2: true, .... })
instead of just affecting values.
I am trying to give a conditional statement inside AsEnumerable select list. I have the following AsEnumerable select . Here I am trying to give the condition If IsRoleClocking = true then IsClockingEnabled should be true else the value of IsClockingEnabled must be depends the value of (Convert.ToInt32(row["IsClockingEnabled"]) . Please help to amend the existing code to consider IsRoleClocking also.
bool IsRoleClocking = true;
attendanceEntry.attendanceLogList = dt.AsEnumerable()
.Select(row =>
{
var p = new AttendanceLogModel
{
IsFlexibleDayOff = (Convert.ToInt32(row["IsFlexibleDayOff"]) == 1) ? true : false,
IsClockingEnabled = (Convert.ToInt32(row["IsClockingEnabled"]) == 1 ) ? true : false,
IsProtected = (Convert.ToInt32(row["IsProtected"]) == 1) ? true : false,
};
return p;
})
.ToList<AttendanceLogModel>();
return attendanceEntry;
}
I hope I got your question correctly :)
Does this help?
IsClockingEnabled = IsRoleClocking ? true :
(Convert.ToInt32(row["IsClockingEnabled"]) == 1 ) ? true : false;
update
Just noticed, it can be done much simpler:
IsClockingEnabled = IsRoleClocking || Convert.ToInt32(row["IsClockingEnabled"]) == 1;
I know I have to return some sort of value in my code so it does not keep crashing with this thread error, but I can't figure it out. Please help! Here is the code- `
#IBAction func decideBttn(_ sender: Any) {
// if there is data in more than one Label randomly pick 1 out of 2
if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false
{
var topics = [valueLbl1.text!, valueLbl2.text!]
pickTopic = Int(arc4random_uniform(UInt32(topics.count)))
topics.remove(at: pickTopic)
resultLbl.text = "\(topics[pickTopic])" //Where fatal error occurs
valueLbl1.text = ""
valueLbl2.text = ""
valueLbl3.text = ""
return
}
// if all 3 Labels are used button will randomly pick 1 out of 3
else if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false && valueLbl3.text?.isEmpty == false
{
var topics = [ valueLbl1.text!, valueLbl2.text!, valueLbl3.text!]
pickTopic = Int(arc4random_uniform(UInt32(topics.count)))
topics.remove(at: pickTopic)
resultLbl.text = "\(topics[pickTopic])"
// resetting variable value
valueLbl1.text = ""
valueLbl2.text = ""
valueLbl3.text = ""
return
}