Angular2 How to Handle null value in .find methode - angular2-forms

Here i wrote simple .find method its finding my data well but if that record is not available then its displying 1st Record
public _pagedItems : any;
someval(value){
if(value.length>=5){
this._pagedItems= this.allItems.find(e=>e.uniqueid = value);
this.pagedItems=[];
this.pagedItems.push(this._pagedItems);
}
if my uniquId is not avaible then its should display Nodata OR Null

Here is an example for handle null and undefined and return no data like
Your code is looking wrong. Your are code using (=) symbol. So change (==).
The difference between = and == (=) equal symbol is assigning value (==) symbol is comparing a value
For more proper example here,
public _pagedItems : any[]=[];
someval(value:string){
if(!this.isObjNull(value) && !this.isObjNull(this.allItems) && this.allItems.length>0){
if(value.length>=5){
if(this.allItems.find(e=>e.uniqueid ==value)){
//you need one data use find
this._pagedItems=this.allItems.find(e=>e.uniqueid == value);
//you need multiple data use filter
this._pagedItems = this.allItems.filter(e=>e.uniqueid == value);
}
//here Iam logged final variable
console.log("Final Paged Items",this._pagedItems);
}
}
}
//here is the method check null and undefined.
isObjNull(data){
If(data!=null && data!=undefined){
return true:
}else{
return false;
}
}
You can handle html part like this
<ng-container *ngIf="_pagedItems; else noData">
<ng-container *ngIf="_pagedItems.length>0; else noData">
<!-- do you logic here.like *ngFor -->
</ng-container>
</ng-container>
<ng-template #noData>
<p>No data found</p>
</ng-template>
That's all let try this once. If any error please let me know.
Note:- here Iam used sample variable please replace your actual variable.
For comparison related more thing visit here.
Difference between == and === in JavaScript

Related

DXL ignoring the error if an attribute doesn't exist in a module

I am writing some DXL for use as a DXL column that for each object in a module, looks at the in-links and returns the link name. Then if the link name starts with "verif", it will get the object text from an attribute "TestResultFloating" in the linked module and show it in the current module, in the DXL column.
The problem I will have when I use this on the whole database (currently I am just using a sandbox) is that some of the modules linked through the "verif" link module will not contain the "TestResultFloating" attribute. For these I would like to oppress the 'unknown Object attribute (TestResultFloating)' error and instead display something like N/A for that Object in the current module.
Below is my code that currently works as long as the "TestResultFloating" attribute is present in the linked module, but will throw the error if the attribute is not present.
ModName_ mSrc
Object o = current
Object nObject
Object oSrc, oDest
LinkRef lr = null
Link l = null
string linkname = ""
string attrbName = "TestResultFloating"
for mSrc in (obj <- "*") do {
if (!open(mSrc)) {
read(fullName(mSrc), true)
}
}
for l in (obj <- "*") do {
oSrc = source(l)
linkname = name(module(l))
string linkmodname = upper(linkname[0:4])
if(linkmodname == "VERIF") {
string objText = oSrc."TestResultFloating"
display(objText)
}
}
I tried one way of doing it which I got from the dxl reference manual which was to check whether the attribute exists and then do the operation. This is what I added but it doesn't seem to work, I still get the same error "unknown Object attribute (TestResultFloating)"
What I tried is shown below:
if(linkmodname == "VERIF") {
if(exists attribute "TestResultFloating"){
string objText = oSrc."TestResultFloating"
display(objText)
}
else {
display("N/A")
}
}
Please also note that i'm very new to DOORS and DXL so if I am doing something drastically wrong or I am asking a simple question please forgive me.
There is a utility function called string probeAttr_(Object o, string attrName) that can be used for getting an attribute value if you are not sure whether the attribute is readable or whether it even exists.
This function and a lot of similar functions tailored for different circumstances can be found in the file "c:\Program Files\IBM\Rational\DOORS\9.6\lib\dxl\utils\attrutil.inc"

Free Text Entry in Angular Material mdAutoComplete

I want my angular material autocomplete to be a list of suggestions but not requirements. However I'm not sure how to implement as their is no clear example from the Angular Material docs.
In the example below my model is $ctrl.item.category
Clearly the example below is wrong, as my model is linked to md-selected-item, but this only works if I select an item. I want the user to be able to free enter the text if the item is not in the list. Basically how autocomplete already works in most browsers.
I see plenty of questions on how to disable this, but they are not trying to disable so much as clean up the left over text when an item is not selected. In these cases when an item is not selected then the model value is null, but text is left in the input.
I want the text left int he input to be the model value if the person does not select (or a match is not made).
md-autocomplete(
md-floating-label="Category Name"
flex="50"
md-input-name="category"
md-selected-item="$ctrl.item.category"
md-search-text="catSearch"
md-items="category in $ctrl.categories"
md-item-text="category"
md-min-length="0"
md-select-on-match=""
md-match-case-insensitive=""
required=""
)
md-item-template
span(md-highlight-text="catSearch" md-highlight-flags="^i") {{category}}
My options ($ctrl.categories) is an array of strings ['Food','Liqour'] and I wan the user to be able to use one of those or free enter Tables as their choice.
In this case you should link md-search-text to your model.
If you want to implement fuzzy search you have to write the filter method yourself. Look at this example:
template:
<md-autocomplete
md-items="item in $ctrl.itemsFilter()"
md-item-text="item.label"
md-search-text="$ctrl.query"
md-selected-item="$ctrl.selected"
>
<md-item-template>
<span md-highlight-text="$ctrl.query">{{item.label}}</span>
</md-item-template>
<md-not-found>
No item matching "{{$ctrl.query}}" were found.
</md-not-found>
<div ng-messages="$ctrl.myValidator($ctrl.query)">
<div ng-message="short">Min 2 characters</div>
<div ng-message="required">Required value</div>
</div>
</md-autocomplete>
controller:
var items = [ ... ];
ctrl.itemsFilter = function itemsFilter() {
return ctrl.query ? filterMyItems(ctrl.query) : items;
};
ctrl.myValidator = function (value) {
return {
short: value && value.length < 2,
required : value && value.length < 1,
};
};
then you just need to add filterMyItems method to filter your items
To improve the answer of #masitko, I have implemented the filter in a way, that it adds the query to the filtered list. So it becomes selectable and a valid option. So it's possible to make the autocomplete a suggestion box.
I'm using ES6 in my projects. But it should be easily adaptable to ES5 code.
myFilter() {
if (!this.query) return this.items;
const
query = this.query.toLowerCase(),
// filter items where the query is a substing
filtered = this.items.filter(item => {
if (!item) return false;
return item.toLowerCase().includes(query);
});
// add search query to filtered list, to make it selectable
// (only if no exact match).
if (filtered.length !== 1 || filtered[0].toLowerCase() !== query) {
filtered.push(this.query);
}
return filtered;
}

How can I check string value in razor view?

I have a model, and I want to show dome data, if table_name == "something". All data are different, that is why I need to check. but when I write ==, my View does not understand. It returns only else clause data.
Please help me, how can I check the string value in view?
#foreach (var item in Model)
{
if (item.table_name == "tPerson")
{ #item.user_group_name: <strong>#item.user_full_name</strong> | #item.date;
}
else if (item.table_name == "tBook")
{ Book<strong>#item.book_name</strong> | #item.date}
else
{#item.table_name <strong>#item.name</strong> | #item.date}
}
Check out the "item.table_name" has the back Space or front space

Using a query in an Adapter for listview

I have a listview that I fill from an Adapter. My original code the data was being returned from a table, but now I need to get the code from a query with a join so the examples I used will no longer work and I haven't been able to find out how to use a query for this. I'm using an ORMrepository.
In my ORMrepository I have this function
public IList<Coe> GetmyCoe()
{
using (var database = new SQLiteConnection(_helper.WritableDatabase.Path))
{
string query = "SELECT Coe.Id, Adult.LName + ', ' + Adult.MName AS Name, Coe.Createdt FROM Adult INNER JOIN Coe ON Adult.CoeMID = Coe.Id";
return database.Query<Coe>(query);
}
}
which actually returns the data I want.
then in my Activity page I have this.
_list = FindViewById<ListView>(Resource.Id.List);
FindViewById<ListView>(Resource.Id.List).ItemClick += new System.EventHandler<ItemEventArgs>(CoeList_ItemClick);
var Coe = ((OmsisMobileApplication)Application).OmsisRepository.GetmyCoe();
_list.Adapter = new CoeListAdapter(this, Coe);
My Adapter page is where I have the problem, I know it is set up to to looking at a table which I'm not doing anymore. But I don't know how to change it for what I'm passing into it now. Current CoeListAdapter is:
public class CoeListAdapter : BaseAdapter
{
private IEnumerable<Coe> _Coe;
private Activity _context;
public CoeListAdapter(Activity context, IEnumerable<Coe> Coe)
{
_context = context;
_Coe = Coe;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = (convertView
?? _context.LayoutInflater.Inflate(
Resource.Layout.CoeListItem, parent, false)
) as LinearLayout;
var Coe = _Coe.ElementAt(position);
view.FindViewById<TextView>(Resource.Id.CoeMID).Text = Coe.Id.ToString();
//view.FindViewById<TextView>(Resource.Id.GrdnMaleName).Text = Coe.Name;
view.FindViewById<TextView>(Resource.Id.CreateDt).Text = Coe.CreateDt;
return view;
}
public override int Count
{
get { return _Coe.Count(); }
}
public Coe GetCoe(int position)
{
return _Coe.ElementAt(position);
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return position;
}
}
How do I set up the CoeListAdapter.cs page so that it can use the passed in data. As you can see I have a commented out lines where I fill a TextView which error because Coe.Name is not in the table model for Coe. but it is returned in the query. I believe my problem is IEnumerable but what do I change it to. I'm new to Mobile developement and suing VS2010 for Mono
The problem probably lies with the binding/mapping of the object not the creation of the view.
Or probably more specifically, the query.
Adult.LName + ', ' + Adult.MName AS Name
this should be:
Adult.LName || ', ' || Adult.MName AS Name
See also: String concatenation does not work in SQLite
From the sqlite docs: http://www.sqlite.org/lang_expr.html under the Operators heading:
The unary operator + is a no-op. It can be applied to strings,
numbers, blobs or NULL and it always returns a result with the same
value as the operand.
Note that there are two variations of the equals and not equals
operators. Equals can be either = or ==. The non-equals operator can
be either != or <>. The || operator is "concatenate" - it joins
together the two strings of its operands. The operator % outputs the
value of its left operand modulo its right operand.
The result of any binary operator is either a numeric value or NULL,
except for the || concatenation operator which always evaluates to
either NULL or a text value.
This shows that the + will evaluate to zero. If you use ||, the value will either be the correct value or NULL (if either of Adult.LName or Adult.MName is NULL).
This can be fixed by:
coalesce(first, '') || ', ' || coalesce(second, '')
but this may result in , LastName or FirstName,.
Another way would be to create another two properties in Coe called LName and MName.
Then bind the values to those properties and use the Name property like this:
public string Name
{
get { return string.Join(", ", LName, MName); }
}
This will probably be better as you can change how the Name appears especially if there are different combinations of First, Middle and Last names in different places.
And off topic:
I believe my problem is IEnumerable...
This is probably not too true as it returns the correct values. A better way would be to use IList as IEnumerable will iterate through the list each time to get the item as it does not know that the collection is actually a list. (I think)
thanks for the help on the concantination, I did find that was wrong, I did fix my problem, I was using an example by Greg Shackles on how to set up using a data base. what I had to do was create a new model with the elements I was wanting. So I created a new Model and called it CoeList, then everywhere I had List or IEnumerable I changed it to List or IEnumerable and it worked.

Html.Raw() in ASP.NET MVC Razor view

#{int count = 0;}
#foreach (var item in Model.Resources)
{
#(count <= 3 ? Html.Raw("<div class=\"resource-row\">").ToString() : Html.Raw(""))
// some code
#(count <= 3 ? Html.Raw("</div>").ToString() : Html.Raw(""))
#(count++)
}
This code part does not compile, with the following error
Error 18 Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.Web.IHtmlString' d:\Projects\IRC2011_HG\IRC2011\Views\Home\_AllResources.cshtml 21 24 IRC2011
What I must I do?
Html.Raw() returns IHtmlString, not the ordinary string. So, you cannot write them in opposite sides of : operator. Remove that .ToString() calling
#{int count = 0;}
#foreach (var item in Model.Resources)
{
#(count <= 3 ? Html.Raw("<div class=\"resource-row\">"): Html.Raw(""))
// some code
#(count <= 3 ? Html.Raw("</div>") : Html.Raw(""))
#(count++)
}
By the way, returning IHtmlString is the way MVC recognizes html content and does not encode it. Even if it hasn't caused compiler errors, calling ToString() would destroy meaning of Html.Raw()
The accepted answer is correct, but I prefer:
#{int count = 0;}
#foreach (var item in Model.Resources)
{
#Html.Raw(count <= 3 ? "<div class=\"resource-row\">" : "")
// some code
#Html.Raw(count <= 3 ? "</div>" : "")
#(count++)
}
I hope this inspires someone, even though I'm late to the party.
You shouldn't be calling .ToString().
As the error message clearly states, you're writing a conditional in which one half is an IHtmlString and the other half is a string.
That doesn't make sense, since the compiler doesn't know what type the entire expression should be.
There is never a reason to call Html.Raw(...).ToString().
Html.Raw returns an HtmlString instance that wraps the original string.
The Razor page output knows not to escape HtmlString instances.
However, calling HtmlString.ToString() just returns the original string value again; it doesn't accomplish anything.

Resources