How to filter by Date - google-app-maker

I have a dropdown that have 3 values (Status Values): All, Active, Inactive and a Table with results.
The dropdown will filter the table results by the status, this status depends on a field that is from Date value.
Active: End_date is null. Returns the results that do not have a end_date
Inactive: end_sate is not null. Returns the results that have a end_date
All: will not query by nothing. Returns all results

First things first you need to create a parameter in your query script datasource:
Once you have the parameter, you can bind it to dropdown's value and add the following code to the onValueChange event
app.datasources.MyTableDatasource.load();
Your query script code will look similar to this:
var status = query.parameters.Status;
switch(status) {
case 'Active':
query.filters.end_date._equals = null;
break;
case 'Inactive':
query.filters.end_date._notEquals = null;
}
// we keep original query to preserve its paging and sorting settings
// and we do not set any additional filters in case selected status
// is not Active or Inactive
return query.run();

Related

How to create an advanced search/filter using Google App Maker's Query Script?

I'm making an app with an advanced search feature which can help users filter data from dropdowns and textboxes (Dropdown to choose column and clause, Textbox for entering search parameter) like this one:
Advanced Search page sample:
I tried to bind the Column's name dropdown to #datasource.query.parameters.Parameter and changed the Query part of the datasource like this:
Datasource's Query Script and Parameters:
However, I keep getting errors like:
Parameter 'Column' is used in 'where' clause but not defined in property 'parameters'
Could you please tell me how can I resolve this problem?
You literally have to construct your 'Where' clause in this case and then set the parameter of the where clause equal to your parameters. So lets say your first set of parameters is Column1: Name, Query1: contains, Parameter1: John, then your datasource needs to have the following parameters Column1, Query1, and Parameter1 and your bindings on your dropdown1, dropdown2, and textbox1 should be:
#datasource.query.parameters.Column1
#datasource.query.parameters.Query1
#datasource.query.parameters.Parameter1
respectively.
Then your query script needs to be as follows:
if (query.parameters.Field1 === null || query.parameters.Query1 === null) {
throw new app.ManagedError('Cannot complete query without Parameters!');
}
switch (app.metadata.models.MaintenanceManagement.fields[query.parameters.Field1].type) {
case 'Number':
query.parameters.Parameter1 = Number(query.parameters.Parameter1);
break;
case 'Date':
query.parameters.Parameter1 = new Date(query.parameters.Parameter1);
break;
case 'Boolean':
if (query.parameters.Parameter1 === 'True' || query.parameters.Parameter1 === 'true') {
query.parameters.Parameter1 = true;
} else {
query.parameters.Parameter1 = false;
}
break;
default:
query.parameters.Parameter1 = query.parameters.Parameter1;
}
query.where = query.parameters.Column1 + " " + query.parameters.Query1 + "? :Parameter1";
return query.run();
So your where statement essentially becomes a string that reads 'Name contains? :Parameter1' (i.e. John) that then becomes your query. Hope this makes sense, feel free to ask follow up questions.

Meteor: how to find record's position in the collection

I'm working on a cursor-based pagination and I need to find record's position in the collection by its _id. Is it possible to do so, using mongo query only (or several queries), and not fetching all the records and counting because that is inefficient in case a collection contains millions of records as well as using createdBy field, because few records can be created at the same time.
The _ids are strings so I cannot just do something like
n = myCollection.find({ _id: { $lte : 12345}}).count() ;
By default, the Meteor .find() query sorts records in their natural order. This is not based on _id, but rather the position of the records on disk.
Instead, you need to explicitly define the sort parameter if you want records to be sorted by _id. Thus your query should look something like this:
// pageNo = the page number
// pageSize = number of records per page
records = myCollection.find({}, {
sort: { '_id' : 1 }, // Sort by _id in ascending order
limit: pageSize // Only return pageSize elements
skip: pageSize*pageNo // Skip the elements fetched on previous pages.
});

checkbox update to mongo is sometime work and sometime won't work

My code sometime work and some time won't work
Mongo didn't update sometimes...
But his code is work perfact
What am i do wrong?
Here is my code
http://checkboxploblem.meteor.com
https://github.com/codepawn/practice_makes_perfect/tree/master/checkbox_mongo
origin is
https://github.com/EventedMind/class-build-a-multi-page-app-with-iron-meteor-6737880d
Your problem lies in your event within the 'home.coffee' file:
Template.Home.events
'click [name=isDone]': (e, tmpl) ->
id = #_id
isDone = tmpl.find('input[name=isDone]').checked
Todos.update {_id: id},
$set:
isDone: isDone
You are assigning the first checkbox value to the variable isDone. Thus, if you check the first input box, that is what gets applied to every subsequent task.
You can get around this by retrieving the value of the checkbox from the event object. ie. e.currentTarget.checked
Another alternative is use a unique id for each checkbox and retrieve the value using that id

Angularfire v2: is there a way to get a collection beginning with a specified index number?

For instance:
$scope.items = $firebase(new Firebase("https://****.firebaseio.com").startAt(100).limit(100));
Starting at the 100th item in the Firebase and ending at 200? I know I can use a skip filter but that still seems to load the first 100 items, correct me if I'm wrong.
You are on the right track. You attach the startAt().limit() code to the Firebase ref and then pass that into $firebase as you have above.
However, the startAt method does not take a numeric offset, but instead a priority and optionally a record id (key).
So where you have put startAt(100), attempting to start after record 100, you would instead need to use the record ID, or prioritize the records in groups of 100.
For background, here's a simple paginator you can check out and steal ideas from. The heart of the example is in nextPage, where it calls startAt using the previous record id like so:
var lastKey = null; // page 0
this.ref.startAt(null, lastKey)
.limit(this.limit + (lastKey? 1 : 0))
.once('value', /* callback to process data goes here */));
UPDATE
Another useful note here is that the records returned by angularFire contain their unique id as $id, which can be useful for determining the id of the last iterated item.

change boolean value

Hi I am working on a Hotel Reservation Project in ASP.NET.
I want to make a filter so that I can search the room available by date (for example between 1 June and 5 June) and after that reserve that room .How can I do to change status of that room to "not available" for that period,so that somebody who want to reserve that room between 15 may and 30 may can do it.Status of the room is boolean value in my database.
try something like this
before displaying data on UI, you have to manipulate your room table everytime
var objrum = rum.toList().asQueryable(); // fetches all the rooms
var objreservation = reservation.toList().asQueryable(); // fetches all reservation
foreach(var res in objreservation)
{
foreach(var rum in objrum)
{
if(res.roomid == rum.rumid)
{
if(res.checkindate >= currentcheckindate && res.checkoutdate <= currentcheckoutdate)
{
rum.status = false // set to unavailable
}
else
rum.status = true // set to available
}
}
}
than fetch the data from room table for displaying on your UI
The 'Status' value changes and is based upon multiple things so you shouldnt really store it against your room table.
What you need to do in this case is write a query that looks at a date range and then returns the status based on your reservations table. So a user selects a date range and the query gets executed and returns a list of all the days with the status of available or booked.
Imagine if you did this
SELECT * FROM Reservation res LEFT JOIN Room ON res.Room_ID = Room.RoomId
Your room table has a status of Available so it will always return available for every single row. If you had this status as unavailable it would return unavailable for every row, even when the room was actually available.

Resources