javascript - How do I split a table column into a new column at the nth delimiter -


i managed create code splits, @ slash delimiter, third column new columns.

what did not manage make split @ nth (i.e. 2nd) occurrence. not find similar question on internet, that's why post here.

the desired outcome should follows:

enter image description here

all welcome!

	function split() {  	    var delimiter = "/";  	    var arr = [];  	    var highest = 0;  	    var columnindex = "";  	    $('#tbl td:nth-child(3)').each(function() {  	        columnindex = $(this).index();  	        var string = $(this).text();  	        var array = string.split(delimiter);  	        var nbrcharacter = (string.split(delimiter).length - 1) //count occurences of character  	        var temp = (nbrcharacter > highest) ? highest++ : highest = highest;  	        arr.push(string.split(delimiter));  	    });  	    (i = 0; < highest; i++) { //add empty columns  	        $('#tbl').find('tr').each(function() {  	            $(this).find('td').eq(columnindex).after('<td></td>');  	        });  	    }  	    (i = 0; < arr.length; i++) { //populate cells array  	        var columntracker = columnindex  	        (j = 0; j < arr[i].length; j++) {  	            $('#tbl').find('tr:eq(' + (i + 1) + ')').find('td:eq(' + columntracker + ')').html(arr[i][j]);  	            columntracker++  	        }  	    }  	}
th {      height: 15px;      min-width: 30px;      border: 1px solid black;      font-size: 12px;      font-family: courier, monospace;      padding: 2px 5px 2px 5px;  }    td {      height: 15px;      min-width: 30px;      border: 1px solid black;      font-size: 12px;      font-family: courier, monospace;      padding: 2px 5px 2px 5px;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <button type="button" onclick="split()">split</button>    <br>  <br>  <table id="tbl">      <thead>          <tr class="tbl-header">              <th>1</th>              <th>2</th>              <th>3</th>              <th>4</th>          </tr>      </thead>      <tbody>          <tr>              <td>a/b/c</td>              <td>b/c</td>              <td>c</td>              <td></td>          </tr>          <tr>              <td>a/b</td>              <td>b/c</td>              <td></td>              <td>d/e</td>          </tr>          <tr>              <td></td>              <td></td>              <td></td>              <td></td>          </tr>          <tr>              <td>a/b/c/d</td>              <td></td>              <td>c/d/e</td>              <td>d/e/f</td>          </tr>          <tr>              <td></td>              <td>b/c/d</td>              <td>c/d</td>              <td>d/e/f/g</td>          </tr>          <tr>              <td>a/b/c</td>              <td>b/c/d/e</td>              <td>c/d/e/f</td>              <td></td>          </tr>      </tbody>  </table>

you can use logic split cell data second slash.

     var string = "a/b/c/d/e/f";  //use $(this).text(); here  		 var delimiter = "/"; // remove line use delimiter variable       var indexofsecond = string.indexof(delimiter, string.indexof(delimiter)+1);  		 console.log(indexofsecond);       if(indexofsecond > -1){        var fone = string.substring(0,indexofsecond);        var sone = string.substring(indexofsecond+1);        console.log(fone);        console.log(sone);       }


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -