I'm very new to dart, so don't judge me. :))
I just started to write a simple ToDo App in Dart. I want to add a button to the item, so i can delete it from the list. I add the button successfully, but don't get the click event working.
I know why the code isn't working, but don't know, what the best solution would be to solve this.
Some improvements would be awesome.
Thanks in advance
Ron
my little sexy dartpad
You need to register the onClick listen to remove the current item on every button. Here's a working version of your code.
import 'dart:html';
InputElement toDoInput;
UListElement toDoList;
void main() {
toDoInput = querySelector('#to-do-input');
toDoList = querySelector('#to-do-list');
toDoInput.onChange.listen(addToDoItem);
}
// Add item to list
void addToDoItem(Event e) {
final toDoItem = new LIElement();
toDoItem.text = toDoInput.value;
final deleteItemButton = new ButtonElement()
..text = 'Delete'
..onClick.listen((_) => toDoItem.remove());
toDoItem.children.add(deleteItemButton);
toDoList.children.add(toDoItem);
toDoInput.value = '';
}
Related
I am using https://vaadin.com/directory/component/file-download-wrapper
with below code
exportButton = new Button("export"));
FileDownloadWrapper buttonWrapper = new FileDownloadWrapper(
new StreamResource(genericGridView.getExportFileCaption(), () -> getExportStream()));
buttonWrapper.wrapComponent(exportButton);
buttonLayout.add(exportButton); //Button Layout is an Horizontal Layout which is added finally in the
main Layout.
and this part of code is written at the loading time of my view. But the method getExportStream method not getting executed(Tried to Put debug in eclipse but it never executed) neither on view load nor in button click event.
Please can you help in this implementation, whether I am doing something wrong here?
You need to add the FileDownloadWrapper to the layout instead of the Button. Otherwise, the FileDownloadWrapper does nothing.
For me the code using FileDownloadWrapper looks correct. So I tend to think that the problem is more related to how you generate the data. I copy here one code snippet from my test apps that works. The CSV export here uses StatefulBeanToCsv and StatefulBeanToCsvBuilder from com.opencsv.opencsv library.
private void export(Grid<Person> grid, TextArea result) {
Set<Person> selection = grid.asMultiSelect().getValue();
Stream<Person> persons = persons = selection.stream();
StringWriter output = new StringWriter();
StatefulBeanToCsv<Person> writer = new StatefulBeanToCsvBuilder<Person>(
output).build();
try {
writer.write(persons);
} catch (CsvDataTypeMismatchException
| CsvRequiredFieldEmptyException e) {
output.write("An error occured during writing: " + e.getMessage());
}
result.setValue(output.toString());
StreamResource resource = new StreamResource("export.csv",
() -> new ByteArrayInputStream(output.toString().getBytes()));
FileDownloadWrapper download = new FileDownloadWrapper(resource);
Button button = new Button("Click to download");
download.wrapComponent(button);
add(download);
}
I'm currently working on a JavaFX project.I'm using Autcomplete TextField of ControlFx .Each time i add new rows in database table, it should to update Autocomplete ,i did this but my problem is showing double Context-Menu ,we can say double autocompletes because i call method that create autocomplete each adding of new elements in table.
When i click a tab editBill i call this method :
public void showEditBill() {
if (!BillPane.getTabs().contains(EditBillTab)) {
BillPane.getTabs().add(EditBillTab);
}
SingleSelectionModel<Tab> selectionModel = BillPane.getSelectionModel();
selectionModel.select(EditBillTab);
/*it should remove the old autocomplete from textfield*/
pushBills(); //Call for cheking new items
}
pushBills method () :
public void pushBills() {
ArrayList list = new ArrayList<>();
bills = new BillHeaderDao().FindAll();
for (int i = 0; i < bills.size(); i++) {
list.add(bills.get(i).getIdClient());
}
//How can i remove the old bind before bind again
autoCompletionBinding = TextFields.bindAutoCompletion(SearchBill, SuggestionProvider.create(list));
}
How i can remove the old autocomplete and bind new automplete?
Just in any case if you need to keep instance of AutoCompletionTextFieldBinding object, thus avoiding use of:
autoCompleteBinding = TextFields.bindingAutoCompletion(TextField,List);
, which will change the instance, we could go a little bit deeper and use this:
// let's suppose initially we have this possible values:
Set<String> autoCompletions = new HashSet<>(Arrays.asList("A", "B", "C"));
SuggestionProvider<String> provider = SuggestionProvider.create(autoCompletions);
new AutoCompletionTextFieldBinding<>(textField, provider);
// and after some times, possible autoCompletions values has changed and now we have:
Set<String> filteredAutoCompletions = new HashSet<>(Arrays.asList("A", "B"));
provider.clearSuggestions();
provider.addPossibleSuggestions(filteredAutoCompletions);
So, through SuggestionProvider, we have "updated" auto completion values.
To avoid doubling of suggestions menu, don't use again (for the 2nd time):
TextFields.bindAutoCompletion(..)
In order to provide updates to the auto-complete suggestion list, retain a reference to the SuggestionProvider and update the suggestion provider instead:
TextField textField = new TextField();
SuggestionProvider suggestionProvider = SuggestionProvider.create(new ArrayList());
new AutoCompletionTextFieldBinding<>(textField, suggestionProvider);
When you want to update the suggestion list:
List<String> newSuggestions = new ArrayList();
//(add entries to list)
suggestionProvider.clearSuggestions();
suggestionProvider.addPossibleSuggestions(newSuggestions);
This will do the trick:
Instead of: TextFields.bindAutoCompletion(textField, list);
, try this:
List<String> strings = new ArrayList<>();
Then create binding between your textField with the list through:
new AutoCompletionTextFieldBinding<>(textField, SuggestionProvider.create(strings));
So any changes, including removing, from the list, will be reflected in the autoCompletion of the textField;
And you will have dynamic filtering of suggestions, showed in pop-up, when user enter some text in textField;
I had the same problem some time ago I try to do as #MaxKing mentions, but it didnt work. I managed to give it a soluciĆ³n even though I don't think it's the right way.
// Dispose the old binding and recreate a new binding
autoCompleteBinding.dispose();
autoCompleteBinding = TextFields.bindingAutoCompletion(TextField,List);
try this:
public void pushBills() {
ArrayList list = new ArrayList<>();
bills = new BillHeaderDao().FindAll();
for (int i = 0; i < bills.size(); i++) {
list.add(bills.get(i).getIdClient());
}
autoCompletionBinding.dispose();
autoCompletionBinding = TextFields.bindAutoCompletion(SearchBill, SuggestionProvider.create(list));
}
Vaadin's comboBoxes were designed to show captions and items are line by line (you can see them in here). If I want to see them in a same line , what is the solution ?
Now I am trying to get as below ....
HorizontalLayout hlMain = new HorizontalLayout();
hlMain.addComponent(new Label("Gender:"));
final ComboBox gender = new ComboBox("" , genderList);
hlMain.addComponent(gender);
But I know above code is too ugly , So I am trying to use with CSS . I would like to know has there easy way to get it with Vaadin ? Any suggestions will be great help for me.
If you are willing to dedicate a layout to this, use a FormLayout:
// run with: spring run vaadin.groovy
#Grapes([
#Grab('org.vaadin.spring:spring-boot-vaadin:0.0.5.RELEASE'),
#Grab('com.vaadin:vaadin-server:7.4.4'),
#Grab('com.vaadin:vaadin-client-compiled:7.4.4'),
#Grab('com.vaadin:vaadin-themes:7.4.4'),
])
import org.vaadin.spring.annotation.VaadinUI
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.*
#VaadinUI
class MyUI extends UI {
protected void init(VaadinRequest request) {
setContent(new FormLayout( // XXX
new ComboBox("Gender:").with{ addItem("M"); addItem("F"); it }
))
}
}
Your current code would work, if you would not put the empty caption on the ComboBox (it makes Vaadin think, that there is something to show and render an empty line, break, and then the combobox). Yet FormLayout is the superior solution.
// run with: spring run vaadin.groovy
#Grapes([
#Grab('org.vaadin.spring:spring-boot-vaadin:0.0.5.RELEASE'),
#Grab('com.vaadin:vaadin-server:7.4.4'),
#Grab('com.vaadin:vaadin-client-compiled:7.4.4'),
#Grab('com.vaadin:vaadin-themes:7.4.4'),
])
import org.vaadin.spring.annotation.VaadinUI
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.*
#VaadinUI
class MyUI extends UI {
protected void init(VaadinRequest request) {
setContent(new HorizontalLayout(
new Label("Gender"),
new ComboBox().with{ addItem("M"); addItem("F"); it } // no label!
))
}
}
You can use Vaadin FormLayout component wherever you required to put caption before input fields.
Example:
// A FormLayout used outside the context of a Form
FormLayout fl = new FormLayout();
// Make the FormLayout shrink to its contents
fl.setSizeUndefined();
TextField tf = new TextField("A Field");
fl.addComponent(tf);
// Mark the first field as required
tf.setRequired(true);
tf.setRequiredError("The Field may not be empty.");
TextField tf2 = new TextField("Another Field");
fl.addComponent(tf2);
In the following code f.onChildAdded is an Instance of '_BroadcastStream<Event>':
f.onChildAdded.forEach((e) {
print(e.snapshot.val());
//TODO: Turn this somehow into a list
});
The print outputs each item fine, and now I'd like to turn it into a list that's friendly with Polymer's repeat="{{item in items }}" magic.
How do I turn it into a list? List myList = new List(); and myList.add(e.snapshot.val()); isn't helping me there.
How do I pass that from my main() in my index.dart to my-element.dart?
FWIW, here's my project: https://github.com/DaveNotik/dartstack. Any other guidance as I get a handle on this would be great!
At first you should ensure that your main look like shown here
how to implement a main function in polymer apps
in your main:
List myList = new List();
f.onChildAdded.forEach((e) {
myList.add(e.snapshot.val());
});
(querySelector('your-element') as YourElement).myList = myList;
or when you don't want to create a new list each time:
The list field in you custom element should look like
#observable List myList = toObservable([]);
in your main:
List myList = (querySelector('your-element') as YourElement).myList;
f.onChildAdded.forEach((e) {
myList.add(e.snapshot.val());
});
Normally, if I want to start a new activity I can use
StartActivity(typeof(foo));
This is fine.
I can also set an intent
Intent i = new Intent(this, typeof(foo));
StartActivity(i);
Problem is this. I have Activity A. This fires off Activity B. However, I need to fire off Activity B after using PutExtra on an intent. If I do
Intent i = new Intent(this, typeof(ActivityB));
monodroid gets rightly annoyed as I'm defining a new Intent from within an Activity.
Is there a way to do this
(psuedocode)
[Activity]
public partial class A
{
protected override void OnCreate(Bundle savedInstance)
{
SetContentView(Resource.Layout.layout);
Button btnClick = FindViewById<Button>(Resource.Id.btnClicky);
btnClick.Click += new EventHandler(button_click);
}
private void button_Click(object s, EventArgs e)
{
Intent i = new Intent(this, typeof(B)); // <- gets annoyed
i.PutExtra("foo", 1);
i.PutExtra("bar", true);
StartActivity(i);
}
}
Any help here would be appreciated.
PFJ
Looks like I needed to add a using which wasn't needed elsewhere.
Thanks for the answers and sorry for the delay in getting back :)
Promise that in future, I'll make sure things are clearer.