Tk combobox update box with new list - combobox

I have two comboboxes. If the user picks one entry from the first one, the second one should update so that the user only can pick certain things.
I populate the first box:
ttk::combobox .dsm.nb.f1.entr_Box1 -textvariable GUI_BoxOne -values [list {first entr} {second entr}] -state readonly
Then I use bind to catch any changes:
bind .dsm.nb.f1.entr_Box1 <<ComboboxSelected>> { ::UpdateCombobox }
If anything changes I repopulate the second box:
proc ::UpdateCombobox { } {
switch $::GUI_BoxOne {
{first entr} {
set ::GUI_BoxTwo {one new value}
}
{second entr} {
set ::GUI_BoxTwo [list {first new value} {second new value} ]
}
}
}
This works fine for the first case of the switch, I get one entry in box two I can select, but I do not manage to populate the box by several new values (lines). "first new value" and "second new value" end up as one entry in the box. Logical somehow, it's one list, but how can i update all the values of a combobox at once ?

Use .dsm.nb.f1.entr_Box2 configure -values:
proc ::UpdateCombobox {} {
.dsm.nb.f1.entr_Box2 configure -values [dict get {
{first entr} {
{one new value}
}
{second entr} {
{first new value}
{second new value}
}
} $::GUI_BoxOne]
}
Of course you can also use switch, but I usually prefer dicts for that.
The -textvariable just contains the current value, if you set it the current value is set.

Related

SwiftUI, IF inside ForEach loop to display different text

I am having a hard time displaying certain text in a ForEach loop in SwiftUI.
I have an array that I iterate over with ForEach. The problem is, I need to have certain text displayed for whether or not a specific date is selected in a calendar.
For example:
ForEach(reminders) { reminder in
if(Calendar.current.isDate(reminder.beginDate, equalTo: selectedPassedDate) {
HStack {
Text("The Text for the specific day")
} else {
Text("The text for the unspecific day")
}
}
}
When the array has more than 2 elements, the text for the non-matching day is being displayed multiple times (since it is inside the foreach). This makes sense, but I am trying to figure out a way to only display ONE of the "unspecified day" text instead of many.
I have tried adding a simple bool to display the other text, but I cannot change the state of it without a button or something.. I need to do this totally programmatically. I've also moved the else statement outside of the loop but again the secondary text is being displayed in the current day selection.
What can I do to solve this issue? If I can programmatically set the bool while conforming to SwiftUIs view then I can solve this, but I'm not sure how.
you could try something like this:
class FirstTime: ObservableObject {
var value = true
}
struct MyView: View {
...
#State var firstTime = FirstTime()
var body: some View {
VStack {
ForEach(reminders) { reminder in
HStack {
if (Calendar.current.isDate(reminder.beginDate, equalTo: selectedPassedDate) {
Text("The Text for the specific day")
} else {
if firstTime.value {
showTextOnce()
}
}
}
}
}
}
func showTextOnce() -> some View {
firstTime.value = false
return Text("The text for the unspecific day")
}
}

Bind comma separated list to checkboxes in Angular 9

In my reactive form I have an array that I bind the checkbox list to using following syntax:
structure of array is {id:number, name:string}
TS
ngOnInit(): void {
this.initFCForm();
this.addCheckboxes();
}
initFCForm(): void {
this.fcForm = this.formBuilder.group({
frequency : new FormControl('', [Validators.required]),
rules: new FormArray([])
});
}
get rulesFormArray() {
return this.fcForm.controls.rules as FormArray;
}
private addCheckboxes() {
this.businessrules.forEach(() => this.rulesFormArray.push(new FormControl(false)));
}
HTML
<label class="col-md-7" formArrayName="rules"
*ngFor="let rule of rulesFormArray.controls; let i = index">
<input type="checkbox" [formControlName]="i">
{{businessrules[i].name}}
</label>
Now I need to populate it when the page loads to have the first and the third option selected. I have tried the following code but it only select first two options.
TS
this.fc.rules.patchValue([1,3])
When you do this.fc.rules.patchValue([1,3]), it will set value 1 to first control of the rules FormArray and 3 to the second control of the rules FormArray but nothing to third.
Checkbox formcontrol expects a boolean value (true or false) and here, when setting 1 and 3 using patch, values are automatically 'converted' to true because these are truthy values.
If you want to have the first and third value populated, try this :
this.fc.rules.patchValue([true,null, true])

Multiple Select not working, only giving last clicked value

I implemented a multiple select dropdown from react-bootstrap documentation.
It does not let me do multiple select and only gets the last clicked option. I have state variable set to array. What else am I missing? App is created with create-react-app.
I have state set to array inside the class constructor. Binding of event handler also done in the constructor.
Next, I'm showing my event handler followed by form group with onChange and value set to state. (note I have a drop-down above this which is working fine.)
I then pass this value to a few classes before it's parsed to JSON. The last pastes are those classes. I have removed other parameters so easier to read, any ideas, feel free to ask for more info.
this.state = {
codeCoverage: [],
}
this.handleCodeCoverageChange = this.handleCodeCoverageChange.bind(this);
//Event handlers below
handleCodeCoverageChange(event){
this.setState({
codeCoverage: event.target.value
})
}
<Form.Group>
<Form.Label>Please choose your desired code coverage software(s)</Form.Label>
<Form.Control as="select" value={this.state.codeCoverage} onChange={this.handleCodeCoverageChange} multiple>
<option value="">--Please choose an option--</option>
<option value="cobertura">Cobertura</option>
<option value="sonarcube">Sonarcube</option>
</Form.Control>
</Form.Group>
var configurator = new Configurator(this.state.codeCoverage)
class Configurator
{
constructor(
code_coverage)
{
this.pipeline = new Pipeline(code_coverage)
}
}
class Pipeline
{
constructor(code_coverage)
{
this.analysisAndSecurity = new AnalysisAndSecurity(code_coverage)
}
class AnalysisAndSecurity{
parameter
constructor(code_coverage)
{
this.code_coverage = code_coverage
}
}
In your handleChange function you assign state.codeCoverage the value of the selected element instead of adding it to the array of selected element. This is why when you select another element it deletes the old value. I would recommend logging e.target.value and this.state.codeCoverage to better understand. As for the solution:
Since you are using multiple select it expects an array as value instead of a single value. So you need to change two things in your handleChange method.
First you need to add your element to existing values and not replace them.
You need to handle when a selected element is clicked again and needs to become unselected.
You can do both these tasks as shown below:
handleChange = e => {
const { codeCoverage } = this.state;
// Find the value selected the codeCoverage array
const index = codeCoverage.indexOf(e.target.value);
// If the value is not found then add it to the array
if (index === -1) codeCoverage.push(e.target.value);
// If value found then remove the value to unselect
else codeCoverage.splice(index, 1);
// Set the state so that the component reloads with the new value
this.setState({ codeCoverage: [...codeCoverage] });
};

How to update item in array using react?

Could you please tell me how to update item in array using react? I make a dynamic list using add button. In generated Item I have two button update and delete.
On click of update button I change the text of add button to update and fill the selected value in input field. Now when I click update button I want to update the selected item.
Here is my code
https://plnkr.co/edit/bpSGPLLoDZcofV4DYxPe?p=preview
addName() {
if (this.state.username !== '') {
if (this.state.btnText === 'add') {
this.props.add(this.state.username)
} else if (this.state.btnText === 'update') {
this.props.updateItem(this.state.username)
}
this.setState({
username: '',
btnText: 'add'
})
}
}
delete(item) {
this.props.deleteItem(item)
}
update(item){
this.setState({
username : item,
btnText:'update'
})
}
You need to pass the updated item name and the indexof the item to the update case in the reducer. Then just use splice to update the new value
Updated the plunkr. Check it here
case 'UPDATE_ITEM':
let newList = state.slice()
newList.splice(action.payload.index, 1, action.payload.item)
return newList;

Setting values to each comboBox item

populate combobox code:
ComboBox getCategoryComboBox = new ComboBox();
searchOptionForm.add(getCategoryComboBox);
getCategoryComboBox.setUIID("TextField");
getCategoryComboBox.addItem("Choose Category");
for (Map<String, Object> entry : alacc.responseCategory) {
String categoryName = (String) entry.get("name");
String categoryId = (String) entry.get("id");//how to set this to combobox item
getCategoryComboBox.addItem(categoryName);
}
categoryId is taken from for loop above, how to set it in each combobox items? I need to get the categoryId of each selected combobox item, how can i get this?
You have several ways to do this.
One way is to just do:
getCategoryComboBox.addItem(entry);
Which would provide you the full entry on getSelectedItem() effectively solving that problem.
To make the name render properly though you would need to do this:
cb.setRenderer(new DefaultListCellRenderer<Object>() {
public Component getCellRendererComponent(Component list, Object model, Object value, int index, boolean isSelected) {
if(value instanceof Map) {
value = ((Map)value).get("name");
}
return super.getCellRendererComponent(list, model, value, index, isSelected);
}
});
Notice you will also need to define the theme constant otherPopupRendererBool to false for this to work properly.

Resources