Compare most recent values from multiple BehaviorSubjects - rxjs

Say I have this:
isMatchedCountLessThanTotalCountMessage(){
// I want to implement this
// "returns" a string asynchronously
}
getMatchedEventsCount() {
return this.dcs.matchCount.asObservable();
}
getTotalEventsCount() {
return this.dcs.totalCount.asObservable();
}
matchedCount and totalCount are like so:
public matchCount = new BehaviorSubject<number>(0);
public totalCount = new BehaviorSubject<number>(0);
these Observables fire integers as values change. Anytime a value is fired from either one, I want to compare the two most recent values from both, how do I do that?
What I want to do is return a boolean from the method
so I can display in the HTML:
<div>{{(isMatchedCountLessThanTotalCountMessage() | async)}}</div>
I think Observable.zip might do the trick:
isMatchedCountLessThanTotalCountMessage(){
return Observable.zip(
this.getMatchedEventsCount(),
this.getTotalEventsCount()
)
.subscribe(function(v){
const intA = v[0];
const intB = v[1];
if(intA > intB)
// but I don't know how to send a message the HTML from here
});
}

You can easily use .map() function to transform the data you want:
isMatchedCountLessThanTotalCountMessage() {
return Observable.combineLatest(
this.getMatchedEventsCount(),
this.getTotalEventsCount(),
)
.map(([intA, intB]) => {
return intA > intB ? '(results ARE filtered)' : '(results are not filtered)'
})
}

This works, although we might be able to use something other than Observable.zip.
isMatchedCountLessThanTotalCount() {
return Observable.create(obs => {
return Observable.zip(
this.getMatchedEventsCount(),
this.getTotalEventsCount()
)
.subscribe(v => {
if ((v[1] - v[0]) > 0) {
obs.next('(results ARE filtered)')
}
else {
obs.next('(results are not filtered)');
}
});
});
}
and there is actually a simpler way to do that using what's called a "projection function":
isMatchedCountLessThanTotalCount() {
return Observable.combineLatest(
this.getMatchedEventsCount(),
this.getTotalEventsCount(),
function (one, two) {
if ((two - one) > 0) {
return '(results ARE filtered)'
}
return '(results are not filtered)';
}
)
}
Observable.combineLatest() is similar to Observable.zip() but will fire upon the first new value, it doesn't wait for new values to come from all observables.

Related

How to display and hide columns based on the two dropdown selection using ag grid

Basically, I have two dropdowns a and b.
Based on the combination of these two dropdowns Ineed to hide / show colums using ag grid.
Eg: if I choose 'xyz' in dropdown a and '123' in dropdown b, 2 columns wiil be displayed. Similarly if choose dropdown 'ghj' and '456' in dropdown b, some ther 3 columns will be selected and the first 2 columns will be no longer be visible / available.
I can use if else conditionsbut I need to check for all the possible combinations. Is there an easy way to do so?
dropdown a
onReportingType(e) {
// console.log(e);
this.reportData = e;
this.reportSelArr.push(this.reportData);
console.log(this.reportSelArr);
}
dropdown b
onDataPoint(e) {
console.log(e);
this.dataPointData = e;
this.dataPointSelArr.push(this.dataPointData);
console.log(this.dataPointSelArr);
this.addRatingCol();
}
Condition applied for now
addRatingCol() {
// console.log(this.reportSelArr);
// console.log(this.dataPointSelArr);
for (let i = 0; i < this.reportSelArr.length; i++) {
for (let j = 0; j < this.dataPointSelArr.length; j++) {
if (this.reportSelArr[i].reportingTypeName === 'Outstanding') {
if (this.dataPointSelArr[j].dataPointName === 'Rating') {
this.gridColumnApi.setColumnsVisible(['newRatingName', 'newRatingReleaseDate'], true);
return true;
} else if (this.dataPointSelArr[j].dataPointName === 'Rating Outlook') {
this.gridColumnApi.setColumnsVisible(['newOutlookName', 'newOutlookDate', 'outlookEndDate'], true);
} else if (this.dataPointSelArr[j].dataPointName === 'Rating Watch') {
this.gridColumnApi.setColumnsVisible(['newRatingWatchName', 'newRatingWatchDate', 'ratingwatchEndDate'], true);
}
}
} // end of the for loop
}
if (this.addCol === true && this.addReport === true){
this.gridColumnApi.setColumnsVisible(['newRatingName', 'newRatingReleaseDate'], true);
} else {
this.gridColumnApi.setColumnsVisible(['newRatingName', 'newRatingReleaseDate'], false);
}
}
If you are using ES6..
Instead of the 2 for loops i would prefer using filter for the first loop and finding whether it has the reporting type as outstanding.
const outstanding = this.reportSelArr.filter(elem => elem.reportingTypeName === 'Outstanding')
i would then use the return value of the filter to determine whether or not to run the second for loop which can be through a map.
if (outstanding) {
this.dataPointSelArr.map(elem => {
const { dataPointName } = elem
switch(dataPointName) {
case 'Rating': {
//statements;
break;
}
case 'Rating Outlook': {
//statements;
break;
}
case 'Rating Watch':{
//statements;
break;
}
default: {
//statements;
break;
}
}
})
}

How to check if the user doesn't select any item in the list?

I have a list view that list the activities. I have a way to get the selected values. What I don't know is how can I check if the user doesn't select any items from the list. How can I do this?
This is how I populate my list this will return at least 5-20 activities:
public void Get_Activities()
{
try
{
var db = DependencyService.Get<ISQLiteDB>();
var conn = db.GetConnection();
var getActivity = conn.QueryAsync<ActivityTable>("SELECT * FROM tblActivity WHERE Deleted != '1' ORDER BY ActivityDescription");
var resultCount = getActivity.Result.Count;
if (resultCount > 0)
{
var result = getActivity.Result;
lstActivity.ItemsSource = result;
lstActivity.IsVisible = true;
}
else
{
lstActivity.IsVisible = false;
}
}
catch (Exception ex)
{
//Crashes.TrackError(ex);
}
}
And this is how I get the values of the selected items on my list:
foreach(var x in result)
{
if (x.Selected)
{
// do something with the selected items
}
}
My question is like this
if(list.selecteditemcount == 0){
DisplayAlert("Error","Please select atleast 1 item","Ok");
}
In xamarin projects you can use Linq (using System.Linq). With Linq it's really easy to filter your list like that:
if(!list.Any(x => x.Selected == true))
{
// DisplayAlert...
}
This basically checks if any of your items has the value Selected='true'
Or without Linq you can do something like this:
if(list.FindAll(x => x.Selected).Count() == 0)
{
//DisplayAlert...
}
Use this
if (result.Any(x => x.Selected))
{
}
else
{
await DisplayAlert("Application Error", "Please choose at least one(1) activity", "Ok");
}

collections.sort compare selectItem

i have a Problem and i hope you can help me,
i have a list of SelectItem to ordern
the inital order is:
geschieden
ledig
unbekannt
verheiratet
verwitwet
and i will the list like:
ledig
verheiratet
geschieden
verwitwet
unbekannt
i have the method implementiert but i don t have the right order:
public List getFamilienstandSelectItems()
{
List getFamilienstandSelectItems =TK_Familienstand_DS_Values.getInstance().getSelectItems();
Collections.sort(getFamilienstandSelectItems , new Comparator<SelectItem>()
{
public int compare(SelectItem s1, SelectItem s2)
{
if (s1.getLabel()=="ledig")
{
return 0;}
else if (s1.getLabel()=="verheiratet" )
{ return 0;}
else if (s2.getLabel()=="geschieden" )
{ return 1;}
else if (s2.getLabel()=="unbekannt" )
{ return -1;}
else if (s2.getLabel()=="verwitwet " )
{ return 0;}
else return 1;
} });
return getFamilienstandSelectItems;
}
and the result of this method:
ledig
verheiratet
geschieden
unbekannt
verwitwet
something is missing??
thank you
The compare method should tell us, of the two items that I'm comparing right now, which one should come first? Negative numbers means that the second item should come first, 0 means that they are equal and positive numbers means that the first item should come first.
So give each item type a number and create a function
public int getPriority(SelectItem item) {
// Give higher values to the items that should come first
}
and then do something like this in your compare method
public int compare(SelectItem s1, SelectItem s2) {
return Integer.compare(getPriority(s1),getPriority(s2));
}

LINQ ToList() causes all records to be the last one off a coroutine

I seem to have an misunderstanding because the following code works correctly if I don't append the ToList() command:
IEnumerable<ProcessorInfo> query = (
from n in InfoGet(EMachineInfoDepth.LogicalProcessor)
select n
)
.ToList();
InfoGet looks like this:
internal static IEnumerable<ProcessorInfo> InfoGet(EMachineInfoDepth depth)
{
ProcessorInfo result = new ProcessorInfo();
// loop through all workgroups
foreach (Workgroup wg in workgroups_S)
{
result.Workgroup = wg;
if (depth >= EMachineInfoDepth.NUMANode)
{
// loop through all NUMANodes
foreach (NUMANode node in wg.NUMANodes)
{
result.NUMANode = node;
if (depth >= EMachineInfoDepth.CPU)
{
// loop through all CPUs
foreach (CPU cpu in node.CPUs)
{
result.CPU = cpu;
if (depth >= EMachineInfoDepth.Core)
{
// loop through all Cores
foreach (Core core in cpu.Cores)
{
result.Core = core;
if (depth >= EMachineInfoDepth.LogicalProcessor)
{
// loop through all LogicalProcessors
foreach (LogicalProcessor lp in core.LogicalProcessors)
{
result.LogicalProc = lp;
yield return result;
}
}
else
{
yield return result;
}
}
}
else
{
yield return result;
}
}
}
else
{
yield return result;
}
}
}
else
{
yield return result;
}
}
}
With ToList() I get the correct count, but all records equal the final element in the sequence. While I get that this could be a variable scope error in my complex coroutine, as in all iterations see the final value, why does the code work without ToList()?
My question is: what am I misunderstanding?
Problem is, you're returning reference to the same variable all the time:
ProcessorInfo result = new ProcessorInfo();
That's the only place you're actually creating new ProcessorInfo object. You only change it's properties values later, but return still the same object.
You should consider adding copy constructor into your ProcessorInfo() class, and replace every yield return result; call with yield return new ProcessorInfo(result);. That would be the easiest way to make it work.
Update
It could look like it works e.g. when you've saved some variable state somewhere during loop:
foreach(var item in query)
{
itemsList.Add(item);
propertyList.Add(item.IntProperty);
}
After that call itemsList will contain incorrect data, while propertyList will be just fine.

The method 'OrderBy' must be called before the method 'Skip' Exception

I was trying to implement the jQgrid using MvcjQgrid and i got this exception.
System.NotSupportedException was unhandled by user code
Message=The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
Though OrdeyBy is used before Skip method why it is generating the exception? How can it be solved?
I encountered the exception in the controller:
public ActionResult GridDataBasic(GridSettings gridSettings)
{
var jobdescription = sm.GetJobDescription(gridSettings);
var totalJobDescription = sm.CountJobDescription(gridSettings);
var jsonData = new
{
total = totalJobDescription / gridSettings.PageSize + 1,
page = gridSettings.PageIndex,
records = totalJobDescription,
rows = (
from j in jobdescription
select new
{
id = j.JobDescriptionID,
cell = new[]
{
j.JobDescriptionID.ToString(),
j.JobTitle,
j.JobType.JobTypeName,
j.JobPriority.JobPriorityName,
j.JobType.Rate.ToString(),
j.CreationDate.ToShortDateString(),
j.JobDeadline.ToShortDateString(),
}
}).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
GetJobDescription Method and CountJobDescription Method
public int CountJobDescription(GridSettings gridSettings)
{
var jobdescription = _dataContext.JobDescriptions.AsQueryable();
if (gridSettings.IsSearch)
{
jobdescription = gridSettings.Where.rules.Aggregate(jobdescription, FilterJobDescription);
}
return jobdescription.Count();
}
public IQueryable<JobDescription> GetJobDescription(GridSettings gridSettings)
{
var jobdescription = orderJobDescription(_dataContext.JobDescriptions.AsQueryable(), gridSettings.SortColumn, gridSettings.SortOrder);
if (gridSettings.IsSearch)
{
jobdescription = gridSettings.Where.rules.Aggregate(jobdescription, FilterJobDescription);
}
return jobdescription.Skip((gridSettings.PageIndex - 1) * gridSettings.PageSize).Take(gridSettings.PageSize);
}
And Finally FilterJobDescription and OrderJobDescription
private static IQueryable<JobDescription> FilterJobDescription(IQueryable<JobDescription> jobdescriptions, Rule rule)
{
if (rule.field == "JobDescriptionID")
{
int result;
if (!int.TryParse(rule.data, out result))
return jobdescriptions;
return jobdescriptions.Where(j => j.JobDescriptionID == Convert.ToInt32(rule.data));
}
// Similar Statements
return jobdescriptions;
}
private IQueryable<JobDescription> orderJobDescription(IQueryable<JobDescription> jobdescriptions, string sortColumn, string sortOrder)
{
if (sortColumn == "JobDescriptionID")
return (sortOrder == "desc") ? jobdescriptions.OrderByDescending(j => j.JobDescriptionID) : jobdescriptions.OrderBy(j => j.JobDescriptionID);
return jobdescriptions;
}
The exception means that you always need a sorted input if you apply Skip, also in the case that the user doesn't click on a column to sort by. I could imagine that no sort column is specified when you open the grid view for the first time before the user can even click on a column header. To catch this case I would suggest to define some default sorting that you want when no other sorting criterion is given, for example:
switch (sortColumn)
{
case "JobDescriptionID":
return (sortOrder == "desc")
? jobdescriptions.OrderByDescending(j => j.JobDescriptionID)
: jobdescriptions.OrderBy(j => j.JobDescriptionID);
case "JobDescriptionTitle":
return (sortOrder == "desc")
? jobdescriptions.OrderByDescending(j => j.JobDescriptionTitle)
: jobdescriptions.OrderBy(j => j.JobDescriptionTitle);
// etc.
default:
return jobdescriptions.OrderBy(j => j.JobDescriptionID);
}
Edit
About your follow-up problems according to your comment: You cannot use ToString() in a LINQ to Entities query. And the next problem would be that you cannot create a string array in a query. I would suggest to load the data from the DB with their native types and then convert afterwards to strings (and to the string array) in memory:
rows = (from j in jobdescription
select new
{
JobDescriptionID = j.JobDescriptionID,
JobTitle = j.JobTitle,
JobTypeName = j.JobType.JobTypeName,
JobPriorityName = j.JobPriority.JobPriorityName,
Rate = j.JobType.Rate,
CreationDate = j.CreationDate,
JobDeadline = j.JobDeadline
})
.AsEnumerable() // DB query runs here, the rest is in memory
.Select(a => new
{
id = a.JobDescriptionID,
cell = new[]
{
a.JobDescriptionID.ToString(),
a.JobTitle,
a.JobTypeName,
a.JobPriorityName,
a.Rate.ToString(),
a.CreationDate.ToShortDateString(),
a.JobDeadline.ToShortDateString()
}
})
.ToArray()
I had the same type of problem after sorting using some code from Adam Anderson that accepted a generic sort string in OrderBy.
After getting this excpetion, i did lots of research and found that very clever fix:
var query = SelectOrders(companyNo, sortExpression);
return Queryable.Skip(query, iStartRow).Take(iPageSize).ToList();
Hope that helps !
SP

Resources