1

I wrote a table which has Promo code details and a button. When clicked on that button, a pop-up(modal) would open. After entering all the data in that pop up(modal) should be copied to table and a new row containing New button should be created. Here is the code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="well">Promo code</div>
<table class="table table-bordered">
  <thead>
    <tr>
      <th>Promo code</th>
      <th>Price</th>
      <th>Total allocation</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>INFSG7089</td>
      <td>USD 450</td>
      <td>1000</td>
    </tr>
    <tr>
      <td>GENSI1517</td>
      <td>USD 400</td>
      <td>500</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>
        <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" id="">New</button>
      </td>
      <td></td>
      <td></td>
    </tr>
  </tfoot>
</table>
<!-- Modal -->
<div id="myModal" class="modal fade" 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">Promo code details</h4>
      </div>
      <div class="modal-body">
        <form action="#" method="post">
          <div class="login-fields">
            <div class="field">
              <label for="promocode">Promo code:</label>
              <input type="text" id="promotitle" name="promotitle" value="" class="login username-field form-control" />
            </div>
            <!-- /field -->
            <div class="field">
              <label for="password">Price:</label>
              <div class="row">
                <div class="col-md-3 col-xs-3">
                  <select class="form-control">
                    <option value="USD">$ USD</option>
                    <option value="SGD">$ SGD</option>
                    <option value="EUR">€ EUR</option>
                    <option value="AUD">$ AUD</option>
                    <option value="JPY">¥ JPY</option>
                    <option value="CHN">¥ CHN</option>
                    <option value="THB">฿ THB</option>
                    <option value="MYR">RM MYR</option>
                  </select>
                </div>
                <div class="col-md-9 col-xs-9">
                  <input type="number" id="a_fee" name="admin fee" value="" class="login password-field form-control" />
                </div>
              </div>
            </div>
            <div class="field">
              <label for="password">Total allocation:</label>
              <input type="number" id="allocation" name="allocation" value="" class="login password-field form-control" />
            </div>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="addToTable">Submit</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

JSFiddle: https://jsfiddle.net/3vwjrzzk/3/
How can I do it?

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Ajay Kulkarni
  • 2,900
  • 13
  • 48
  • 97

1 Answers1

4

Here:

$('#addToTable').click(function() {
  var prom = $('#promotitle').val(),
      cur = $('select').val(),
      price = $('#a_fee').val(),
      allocation = $('#allocation').val();

  $('table tbody').append('<tr><td>' + prom + '</td><td>' + cur + ' ' + price + '</td><td>' + allocation + '</td></tr>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div class="well">Promo code</div>
<table class="table table-bordered">
  <thead>
    <tr>
      <th>Promo code</th>
      <th>Price</th>
      <th>Total allocation</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>INFSG7089</td>
      <td>USD 450</td>
      <td>1000</td>
    </tr>
    <tr>
      <td>GENSI1517</td>
      <td>USD 400</td>
      <td>500</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>
        <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" id="">New</button>
      </td>
      <td></td>
      <td></td>
    </tr>
  </tfoot>
</table>
<!-- Modal -->
<div id="myModal" class="modal fade" 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">Promo code details</h4>
      </div>
      <div class="modal-body">
        <form action="#" method="post">
          <div class="login-fields">
            <div class="field">
              <label for="promocode">Promo code:</label>
              <input type="text" id="promotitle" name="promotitle" value="" class="login username-field form-control" />
            </div>
            <!-- /field -->
            <div class="field">
              <label for="password">Price:</label>
              <div class="row">
                <div class="col-md-3 col-xs-3">
                  <select class="form-control">
                    <option value="USD">$ USD</option>
                    <option value="SGD">$ SGD</option>
                    <option value="EUR">€ EUR</option>
                    <option value="AUD">$ AUD</option>
                    <option value="JPY">¥ JPY</option>
                    <option value="CHN">¥ CHN</option>
                    <option value="THB">฿ THB</option>
                    <option value="MYR">RM MYR</option>
                  </select>
                </div>
                <div class="col-md-9 col-xs-9">
                  <input type="number" id="a_fee" name="admin fee" value="" class="login password-field form-control" />
                </div>
              </div>
            </div>
            <div class="field">
              <label for="password">Total allocation:</label>
              <input type="number" id="allocation" name="allocation" value="" class="login password-field form-control" />
            </div>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="addToTable">Submit</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • Works as expected. But I had to change `a_fee` to `ad_fee` because there was another id with same name for another field. Thanks for the help :) – Ajay Kulkarni Aug 10 '16 at 12:45
  • Is it possible to do it vice versa? I.e., copying table values to modal when modal is loaded? – Ajay Kulkarni Aug 10 '16 at 12:54
  • I don't understand.. Tell me the flow.. Do you mean, another modal? To present the table inside a modal? – Mosh Feu Aug 10 '16 at 13:09
  • Yes, it is another modal. I want data in new modal like this: `Promo code:
    Price:
    Total allocation: `. No table in new modal
    – Ajay Kulkarni Aug 10 '16 at 13:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120603/discussion-between-mosh-feu-and-ajay-kulkarni). – Mosh Feu Aug 10 '16 at 13:23
  • @MoshFeu I have 2 tables in my current page, how can I identify where I should put the entries? Thanks a lot! – wolfQueen Feb 21 '17 at 06:36
  • In this case, I think you should add some code: listen to the open event and detect which button clicked. See [related answer](http://stackoverflow.com/a/10635652/863110) – Mosh Feu Feb 21 '17 at 09:32