How to preserve Vaadin ComboBox selection when setting DataProvider? - combobox

Vaadin 12.0.3
I have a helper method that returns ListDataProvider for Days of the Month. When User changes Month, I recall the helper method and reset DataProvider to reflect the new Day range.
filterDay.setDataProvider(IndexViewHelper.getCalendarDaysDataProvider(year, month));
The problem is that setting DataProvider on ComboBox removes currently selected option. To prevent this, I need to do something like this:
var currentValue = filterDay.getValue();
filterDay.setDataProvider(IndexViewHelper.getCalendarDaysDataProvider(year, month));
if(currentValue exists in the new list) filterDay.setValue(currentValue);
Is there a better way to do this? Basically, I would hope that setDataProvider() would not reset currently selected option if that option exists in the newly supplied list.
I know this can get very complex, especially with paging results. I just wanted to ask this on the forum in case there are better ways to deal with the issue.

Related

ExtJS synchronize record between clients

I'm trying to do something fun: I'd like to send the record changes from one client to another and have the second client updated to show these changes. Basically collaborative viewing. The second client is disabled from making changes, he can only watch.
Simple fields like strings, numbers, checkboxes, etc. are easy, that worked right away.
The references are the problem. If I have a combo that uses another model as it's source, I am unable to update it on the second client.
I tried setting just the id, then the entire referenced object, I tried various set options, but simply no dice.
The record does change, I see that the data is updated, I was even able to manually modify the _ reference attributes, but the UI keeps showing the old values for those fields.
Is there a way to send the record from one client to another and have the other client just take over all values and display them in the UI? (it would be better to send just the changes, but I'd be very happy if I could get it to work with the entire record)
EDIT: I'm using SailsJS with socket.io, so the p2p connection is not the issue.
I'm getting the data like this:
var data = record.getData(true);
broadcastRecord(data);
And on the other side I tried:
record.set(data);
A code example for the receiving side would be appreciated, if anyone has an ide how to solve this...
I think your problem is related to associations and comboboxes.
Let's say you have a model User with a field group that references model Group, and that you have a User form with a Group combobox.
In the receiver client, you are probably getting only the group id. record.set(data) updates the bound combobox calling setValue(groupId).
This setValue will try to find the corresponding record inside its store, but it won't ask server-side for that record. Instead, it will create a new record with the passed id (showing an empty combobox).
If possibile, you can set remoteFilter:false to the store and queryMode:'local' on the combobox and preload all the data from that store.
Otherwise, I think you'll have to override the combobox setValue method to get the record remotely.

Sorting and Filtering a collection in a Paged ListBox

I have a database with 10,000 items, to which you can add and remove while the app is running.
I have a ListBox that displays at most 100 items, and supports paging.
You can filter and sort on the 10,000 items, which needs to be immediately reflected in the listbox.
I have a button that randomly selects an item as long as it passes the filters.
What is the best set of collections/views to use for this kind of operation?
So far, my first step will be to create an ObservableCollection of ALL items in the database which we will call MainOC.
Then create a List of all items that match the filter by parsing MainOC which we will call FilteredList.
Then create a ListCollectionView based on the above List that holds the first 100 items.
CONS:
You have to recreate the ListCollectionView every time a sort operation is applied.
You have to recreate the ListCollectionView every time you page.
You have to recreate the ListCollectionView every time a filter is changed.
You have to recreate the ListCollectionView every time an item is added or removed to MainOC.
Is there a better approach that I am missing?
For example, I see that you can apply filters to a ListCollectionView. Should I populate my ListCollectionView with all 10,000 items? But then how can I limit how many items my ListBox is displaying?
Should I be doing my filtering and sorting directly against the database? I could build FilteredList directly off the database and create my ListCollectionView based off that, but this still has all the cons listed above.
Looking for any input you can provide!
This is a problem which is easily solved using DynamicData . Dynamic data is based on rx so if you are not familiar with the wonderful Rx I suggest you start learning it. There is quite a bit of a learning curve but but the rewards are huge.
Anyway back to my answer, the starting point of dynamic data is to get some data into a cache which is constructed with a key as follows
var myCache = new SourceCache<MyObject, MyId>(myobject=>myobject.Id)
Obviously being a cache there are methods to add, update and remove so I will not show those here.
Dynamic data provides a load of extensions and some controllers to dynamically interrogate the data. For paging we need a few elements to solve this problem
//this is an extension of observable collection optimised for dynamic data
var collection = new ObservableCollectionExtended<MyObject>();
//these controllers enable dynamically changing filter, sort and page
var pageController = new PageController();
var filterController = new FilterController<T>();
var sortController = new SortController<T>();
Create a stream of data using these controllers and bind the result to the collection like this.
var mySubscription = myCache.Connect()
.Filter(filterController)
.Sort(sortController)
.Page(pageController)
.ObserveOnDispatcher() //ensure we are on the UI thread
.Bind(collection)
.Subscribe() //nothing happens until we subscribe.
At any time you can change the parameters of the controllers to filter, sort, page and bind the data like follows
//to change page
pageController.Change(new PageRequest(1,100));
//to change filter
filterController.Change(myobject=> //return a predicate);
//to change sort
sortController .Change( //return an IComparable<>);
And as if by magic the observable collection will self-maintain when any of the controller parameters change or when any of the data changes.
The only thing you now have to consider is the code you need for loading the database data into the cache.
In the near future I will create a working example of this functionality.
For more info on dynamic data see
Dynamic data on Github
Wpf demo app

SmartGWT mobile Record with SwitchItem

I am moderately new to SmartGWT and am trying to figure out how to have a RecordList where each record has the ability to have a SwitchItem attached to it. So basically create a list of selectable items, but those items also will have a SwitchItem on them to enable/disable some behavior.
Any thoughts on how I can accomplish this?
Looking at Record and RecordList it appears that Record extends HashMap and does nothing fancy like what I am wanting...but i wondered if it was possible to set the "description" property with perhaps some html string that creates the SwitchItem...is this way off? And if so...how is the best way to do this?
Use setShowRecordComponents() and createRecordComponent(). Documentation for how these work can be found in the core SmartGWT docs:
http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/widgets/grid/ListGrid.html#setShowRecordComponents%28java.lang.Boolean%29
There's no support for "recycle" mode yet. A sample is being created.

Prevent Backbone.js model from validating when first added to collection

Is there a way to suppress model validation in Backbone.js when a new model is first created?
In my app, I have a collection with an arbitrary number of models, which are represented as list items. The user can click a button on each item, which inserts a new empty item beneath the current item. The empty item is failing validation, obviously, because I don't want an empty item being saved later.
There's no way for me to know what sensible defaults might be when I'm creating the new item, so prepopulating the new model with valid data doesn't seem like an option.
Any suggestions?
Update: While working on a tangentially related problem, I realized that I was using Backbone.js version 0.9.0. When this version was released, other people had the same problem I was having, and they complained in this issue on GitHub.
Jeremy modified validation in 0.9.1 to fix this. Adding a (temporarily) empty model to a collection is a valid real-world usecase. You could handle the new, empty model in the view, but if you're managing a list of items like I am, that forces you to have a collection of item views (including the empty one) in addition to your collection of must-be-valid models. That's a really clunky workaround for an otherwise straightforward scenario. Glad this got fixed.
You're not supposed to add invalid models :)
Digging a bit in Backbone source code (0.9.1 at least) showed that the mechanism can be circumvented by passing options to your add method:
var Mod=Backbone.Model.extend({
validate: function(attrs,opts) {
if (opts.init) return;
return "invalid";
}
});
var Col=Backbone.Collection.extend({
model:Mod
});
var c=new Col();
c.add({},{init:true});
console.log(c.length);
A Fiddle: http://jsfiddle.net/jZeYB/
Warning : it may break things down the line.
Do you need to add the model to the collection right away? I presume that validation fails because you add it to the collection immediately.
Instead, when the button is pressed you could just create the view and blank model. When the model validates you add it to the collection. You would need a submit button/mechanism on the new row to add it to the collection (which invokes validation automatically).

How to get a value from a combobox in a form after the field is populated by the database

I have a formPanel with two of the form items as comboboxes with their stores populated by the database. The value from comboBoxA needs to be used to get the value for comboBoxB however comboBoxA.getValue() (as well as getRawValue()) are returning undefined.
storeA.load();
var comboBoxA = Ext.getCmp(comboBoxAID);
storeB.baseParams.UserID = comboBoxA.getValue();
storeB.load();
As noted in the docs, store loading is asynchronous, so you have to do your additional processing within the appropriate callback:
storeA.on('load', function(){
var comboBoxA = Ext.getCmp(comboBoxAID);
storeB.baseParams.UserID = comboBoxA.getValue();
storeB.load();
});
storeA.load();
Loading a ComboBoxes store does not actually select a value. Try making a selection first (or loading a record into the form, etc). It sounds like your trying to link the 2 combos. If thats the case, search around for a tutorial, there are few out there. This should get you started, Linked Combos.
You might want to try this. It might be exactly what you are looking for. It also has a demo on the same page. The page is in german but the demo is predictable and the code is in english so test this.

Resources