Search using input field with regex

588 views
Skip to first unread message

Dale Botha

unread,
May 21, 2013, 4:04:00 PM5/21/13
to ang...@googlegroups.com
Hi

First time posting on google groups!  I am new to angular and have done all the basics and now I'm trying to create a small app!

I have a list of items in a table.  A particular row has a column that lists an array of numbers.  I want to search using the numbers to bring up the species of tree that the number corresponds to.  If I type '70' as my number I'd get 70, 170, 870...etc.  How would I regex that search term to be the whole number and not a part of a number! See screenshot to see what I mean.

I've searched the group topics but I'm coming up blank.  I might be stating the problem incorrectly and I lack the correct language to search properly!

I have feeling it's going to be really simple...I just don't know how!

I'd appreciate the help!

regard, Dale

screen-capture-12.png

Raymond Z.

unread,
May 21, 2013, 4:46:01 PM5/21/13
to ang...@googlegroups.com
I think you may to write a custom filter for this. Here's some example code: http://plnkr.co/edit/ZqnLgS10ub35K0wL3xas?p=preview.

It basically just compares all of an item's numbers against a given number input.

Dale Botha

unread,
May 21, 2013, 5:10:24 PM5/21/13
to ang...@googlegroups.com
I hsd tried that before and somehow couldn't get it right...but in the example code you pass in an extra argument to the filter...which I didn't use before!  I'll let you know how it goes!

Thanks!

Dale Botha

unread,
May 21, 2013, 11:13:30 PM5/21/13
to ang...@googlegroups.com
Hi Raymond

I took a closer look at the example and I had seen what similar before. I realised that I was looking for/wanting a solution that didn't have to rebuild a new array each time I typed a number...it seems inelegant to me.

Is this the only way to do it?

On Tuesday, 21 May 2013 22:46:01 UTC+2, Raymond Z. wrote:

Raymond Z.

unread,
May 22, 2013, 12:27:19 AM5/22/13
to ang...@googlegroups.com
Rebuilding a new array is actually how the default angular filters work. (For example, the limitTo filter: http://github.com/angular/angular.js/blob/master/src/ng/filter/limitTo.js#L81).

As far as I know, this is the only way to do it.

Dale Botha

unread,
May 22, 2013, 1:14:09 AM5/22/13
to ang...@googlegroups.com
Ok thanks Rayond!

Because there seem to be many ways of doing things I wasn't sure!

Thanks, Dale

PS. Is it just me or do the AngularJS Docs REALLY need some work? 

Dale Botha

unread,
May 22, 2013, 6:19:12 AM5/22/13
to ang...@googlegroups.com
Hi Raymond!

I'm getting an "Unexpected token !==" with this code:

.filter('numFilter', function(){
return function(items,searchNum) {
if (typeOf searchNum !== 'number' ) {
return items;
}
...

and yet in your plnkr it works fine.  Any idea why?

Thanks

On Tuesday, 21 May 2013 22:46:01 UTC+2, Raymond Z. wrote:

Sander Elias

unread,
May 22, 2013, 7:17:22 AM5/22/13
to ang...@googlegroups.com
Hi Dale,

I modified the plunker a bit. I did put in some extra code, to show what is happening to the dom-nodes while filtering. Try playing around with it a little bit, to get the idea of what's happening.
you see, if you keep elements in your array, the corresponding DOM elements don't get destroyed. This  might change the way your code.

Regards
Sander

Dale Botha

unread,
May 22, 2013, 9:13:35 AM5/22/13
to ang...@googlegroups.com
Thanks Sander.  I really appreciate you taking the time!

Dale Botha

unread,
May 22, 2013, 9:14:21 AM5/22/13
to ang...@googlegroups.com
...and you Raymond!
Reply all
Reply to author
Forward
0 new messages