[]
If your Firestore collections contain a lot of documents, you may want to limit what your application shows your users based on what they're interested in. Narrowing down and getting only the data that you need more user-friendly, it will also make it more efficient and less expensive (cloud services are not always free).
There are three simple ways to limit the amount of data loaded into a Collection object:
The three options above can be combined and used simultaniously.
For example, the code below loads a collection with customers. Only a few selected fields are retrieved, and only six customers are loaded at a time. To use the Collection object, you'll need to import the Collection class along with the Firestore class:
import { Firestore, Collection } from '@mescius/wijmo.cloud';
// load the Customers collection
let customers = new Collection(fsNWind, 'Customers', {
sortDescriptions: ['CustomerID'],
fields: ['CustomerID', 'CompanyName', 'ContactName', 'City', 'Country'],
pageOnServer: true,
pageSize: 6 // load only six customers per page
});
We will allow users to filter the data by country. We will use a ComboBox to allow users to select the country they are interested in:
let cmbCountries = new ComboBox('#cmbCountries', {
placeholder: '(Select a Country)',
itemsSource: 'Brazil,France,Germany,UK,USA'.split(','),
isRequired: false,
text: '',
textChanged: (s, e) => {
customers.where('Country', '==', s.text);
}
});
When the user selects a country, the ComboBox fires the textChanged event and the code updates the queryFilter property to show all customers in the selected country.
To show the data, we will use a CollectionViewPager and a FlexGrid:
let fsCustomerPager = new CollectionViewNavigator('#fsCustomerPager', {
cv: customers,
byPage: true,
headerFormat: 'Page {current:n0}'
});
let fsCustomerGrid = new FlexGrid('#fsCustomerGrid', {
showMarquee: true,
formatItem: addRowHeaderIndex,
autoGenerateColumns: false,
columns: customers.fields.map(fld => {
return { binding: fld }
}),
itemsSource: customers
});