2

I am using AngularJs, html to show table and display the records in the table.
Demo: http://plnkr.co/edit/xHRLAynOpUiLWbOKhqUm?p=preview&preview

I am trying to click on the number's hyperlink present in the second column. Currently I'm splitting the numbers using the comma (,) delimiter and used the <a href> link to pass the associated number I have clicked on (can see working on first row links).

Sometimes as the data is dynamic, I may get semicolon (;) or colon (:) as the separator, and then the code breaks, passing multiple associated numbers when clicked on the link.

<a ng-repeat="associateNum in player.associatedNumber .split(',')" href="https://urladdr/associateid={{associateNum}}" target="_blank">
       {{associateNum}}<span ng-if="$index+1 != player.associatedNumber.split(',').length">;</span></a>

How to support the above <a href> link even when the associated numbers in the same row are having , or ; or : as the separators?

js code:

app.controller('MainCtrl', function($scope) {
  $scope.players = [{
    "name": "Robert C",
    "associatedNumber": "21,10,133",
    "standing": true,
    "result":"Delivered,shipped,shipped"
}, {
    "name": "Joey C",
    "associatedNumber": "55,2:22;33",
    "standing": false,
    "result":"To be delivered,Delivered"
}, {
    "name": "Bobby A",
    "associatedNumber": "15;22:11",
    "standing": true,
    "result":"TO be delivered"
}, {
    "name": "John A",
    "associatedNumber": "1,33,34",
    "standing": true,
    "result":"To be delivered,shipped"
}];
});
2
  • Can you try encodeURIComponent(value)? Commented Jun 19, 2018 at 18:11
  • no need to use encodeURIComponent..i need to pass the associated number when clicked on the number. Example.,for second row I have 15;22:11 when user mouse over on 15 and click on it ,15 has to pass in the URL. When clicked on 22 ,22 should pass in the URL..But as I'm using split(',') it is not splitting the values 15;22:11. so I want to split using the delimiters , or ; or : Commented Jun 19, 2018 at 18:16

2 Answers 2

4

I would add a filter:

app.filter('splitNumber', function() {
  return function(input) {
        return input.replace(/[;:]/g,',').split(',');
  };
});

and use it like this:

ng-repeat="associateNum in player.associatedNumber | splitNumber"

I have forked your plunk here

Sign up to request clarification or add additional context in comments.

1 Comment

You can also replace the split in the <span ng-if...> with this. Nice solution!
1

Split also takes a regular expression:

app.filter('splitNumber', function() {
  return function(input) {
    //return input.replace(/[;:]/g,',').split(',');
    return input.split(/[,:;]/);
  };
});

The DEMO on PLNKR

For more information, see MDN JavaScript Reference - String.prototype.split()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.