javascript - Search for character in string, within an loop -


i have string containing 3 different types of characters special meaning. have loop iterates 160times, , each iteration of loop, want check current character in string. thus, if first iteration of loop, want check first position in string , check kind of character is. if second iteration, want check second position in string etcetc.

how can in angularjs?

i'm returning string php backend.

here solution in php, want work angularjs:

<?php $string = "........####........ .....##..##......... ......#.......&&&&&& ......&&&&&&........ .&..&&&....%..##.&&& &&......##......&&&. .................... .#..................";  $k = 1; for($j = 1; $j <= 8; $j++) {     echo "<tr>";     for($i = 1; $i <= 20; $i++)     {            if($string[$k] == "#")         {             echo "<td id='$k'>#</td>";         }         elseif($string[$k] == "&")         {             echo "<td class='click' val='water' id='$k'>&</td>";         }         else         {             echo "<td class='click' id='$k'><a href='#'></a></td>";         }         $k++;     }     echo "</tr>"; } ?> 

here controller in angularjs:

gameapp.controller("gamectrl", function($scope,$http,link,$location) {     $scope.trr = [1,2,3,4,5,6,7,8];     $scope.tdd = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];     $scope.getmonsters = "1";     var map;      $http.post("lib/action.php", {monsters: $scope.getmonsters}).success(function(data) {         map = data;         console.log(map);     });      if(link.user) {         /*$scope.message = "fisk";         console.log(link.user);*/     } else {         /*$scope.message = "ledsen fisk";         console.log("Är inte satt");*/     } }); 

here php-backend:

<?php     session_start();     include("classes.php");     $data = file_get_contents("php://input");     if(isset($data))     {         $objdata = json_decode($data);         $login = new user();         echo $login->dologin($objdata->username, $objdata->password);          if(isset($objdata->monsters))          {             $string = "........####.............##..##...............#.......&&&&&&......&&&&&&.........&..&&&....%..##.&&&&&......##......&&&......................#..................";             echo $string;         }     } 

here html looop:

<div ng-controller="gamectrl">     <table>         <tr ng-repeat="row in trr">         <td ng-repeat="r in tdd"></td>     </tr>     </table> </div> 

anyone can me?

?>

well, create function in javascript same :

function maketablefrom(str) {    var k = 1,     result = "";    (var = 1; <= 8; i++) {     result += '<tr>';      (var j = 1; j <= 20; j++) {        if (str[k] === '#') {         result += '<td id="' + k + '">#</td>';       }       else if (str[k] === '&') {         result += '<td class="click" val="water" id="' + k + '">&</td>';       }       else {         result += '<td class="click" id="' + k + '"><a href="#"></a></td>';       }        k++;     }      result += '</tr>';   }    return result; } 

and call success function of post request :

$http.post("lib/action.php", {monsters: $scope.getmonsters}).success(function(data) {   map = data;   console.log(map);   // result declared outside   result = maketablefrom(data);   // use intended }); 

however, let me clear fact that, in php function or in one, calls $string[$k] or str[k] should k-1 because of offset in string indexing...

here link jsfiddle illustrates difference : http://jsfiddle.net/jpreynat/hmutk1y5/

update:

to use resulting string inner html table, should use ng-bind-html in combination ngsanitize module, :

<div ng-app="gameapp">   <div ng-controller="gamectrl">     <table ng-bind-html="gametable">     </table>   </div> </div> 

then don't forget add angular-sanitize.js resources , add app :

angular.module('gameapp', ['ngsanitize']) 

and edit $http.post().success() function :

$http.post("lib/action.php", {monsters: $scope.getmonsters}).success(function(data) {   map = data;   console.log(map);    $scope.gametable = maketablefrom(data); // update table depending on ajax call }); 

link updated jsfiddle : http://jsfiddle.net/jpreynat/hmutk1y5/1/


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -