Skip appendTo dynamic string value to bootstrap modal

I am currently using appendTo

to add dynamic lines. What I want is that if I click on one of td

(not including the delete button) it will open the modal window and get the value of the other td

row it owns. here is the image.

enter image description here

enter image description here

Here's my code adding dynamically that I want to get the value td

and pass it to the bootstrap modal.

function AddOrder(new_code, new_name, new_qtty, new_cost) {    
    var rows = "";  
    var code = new_code;
    var name = new_name;
    var cost = new_cost;
    var qtty = new_qtty;

    rows += 
        '<tr>"' + 
        '<td class="item_code" data-toggle="modal" data-target="#mymodal">'+code+'</td>'+
        '<td class="item_name" data-toggle="modal" data-target="#mymodal">'+name+'</td>'+       
        '<td class="item_qtty" data-toggle="modal" data-target="#mymodal">'+qtty+'</td>'+ 
        '<td class="item_cost" data-toggle="modal" data-target="#mymodal">'+cost+'</td>'+ 
        '<td>'+'<button class="btn remove-button">X</button>'+'</td>'+
        '</tr>';
    $(rows).appendTo("#dynamic_added tbody");
}

      

I tried putting onclick

in td

with data-toggle="modal" data-target="#mymodal"

, but only the modal doesn't work on the function I'm using, for example:

function GetData() {
    $('.item_code').click(function(){
        alert($(this).closest('tr').find('.item_code'));
    });
}

      

+3


source to share


2 answers


I just solved my problem, just a modal call to bootstrap is all I am missing. I have added a class getdata

that I will call in my javascript function

function AddOrder(new_code, new_name, new_qtty, new_cost) {    
    var rows = "";  
    var code = new_code;
    var name = new_name;
    var cost = new_cost;
    var qtty = new_qtty;

    rows += 
        '<tr>"' + 
        '<td class="item_code getdata" onclick="GetData();">'+code+'</td>'+
        '<td class="item_name getdata" onclick="GetData();">'+name+'</td>'+       
        '<td class="item_qtty getdata" onclick="GetData();">'+qtty+'</td>'+ 
        '<td class="item_cost getdata" onclick="GetData();">'+cost+'</td>'+ 
        '<td>'+'<button class="btn remove-button">X</button>'+'</td>'+
        '</tr>';
    $(rows).appendTo("#dynamic_added tbody");
}

      



If I click anywhere on the td other than the button, it will output the modal text and pass the current data of that line.

function GetData() {
    $(document).on('click', '.getdata', function(){
        $('#mymodal').modal();
        var modal_code =$(this).closest('tr').find('.item_code').text();
        var modal_name =$(this).closest('tr').find('.item_name').text();
        var modal_qtty =$(this).closest('tr').find('.item_qtty').text();
        var modal_cost =$(this).closest('tr').find('.item_cost').text();

        var modal = $("#mymodal"); // this is the id of my modal 
        modal.find('.modal-body').text(modal_code + modal_name + modal_qtty + modal_cost);
    }); 
}

      

+1


source


You want something like this:



$('.item_code, .item_name, .item_qtty, .item_cost').click(function() {
  $('.modal-body').html($(this).closest('tr').html());
  $('#myModal').modal('show');
});
      

td {
  border: 1px solid black;
}
      

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<table id="dynamic_added">
  <tbody>
    <tr>
      <td class="item_code">code_1</td>
      <td class="item_name">name_1</td>
      <td class="item_qtty">qtt_1</td>
      <td class="item_cost">cost_1</td>
      <td>
        <button class="btn remove-button">X</button>+</td>
    </tr>
    <tr>
      <td class="item_code">code_2</td>
      <td class="item_name">name_2</td>
      <td class="item_qtty">qtt_2</td>
      <td class="item_cost">cost_2</td>
      <td>
        <button class="btn remove-button">X</button>+</td>
    </tr>
    <tr>
      <td class="item_code">code_3</td>
      <td class="item_name">name_3</td>
      <td class="item_qtty">qtt_3</td>
      <td class="item_cost">cost_3</td>
      <td>
        <button class="btn remove-button">X</button>+</td>
    </tr>
  </tbody>
</table>


<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
      

Run codeHide result


+1


source







All Articles