Github:
https://github.com/DubFriend/jquery.repeater
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<script src="/plugins/jquery.repeater.min.js"></script> <script> var valid_products = '{!! $row->valid_products !!}'; $(document).ready(function(){ $('#bt_submit').on('click', function(e){ $('#iform').submit(); }); $('#iform').ajaxForm({ dataType: 'json', beforeSubmit: function(arr, $form, options) { //console.log(arr); var new_arr = []; for(var i = 0; i < arr.length; i++){ var name = arr[i]; var pattern = /valid_products.+/; if(pattern.test(name) == false){ new_arr.push(arr[i]); } } var values = $('.repeater').repeaterVal(); var item = { name: 'valid_products', value: values } new_arr.push(item); //console.log(values); //return false; return new_arr; }, success: function(data, statusText, xhr, $form){ //console.log(data); if(typeof data.status == 'undefined'){ alert(data); }else if(data.status == 'fail'){ alert(data.message); }else{ //console.log(data); window.location.href = base_url + 'store/{{$controller}}'; } }, error: function(data){ console.log(data); var res = data.responseJSON; show_response_error(res.errors); } }); $('[name="discount_type"]').trigger('change'); var $repeater = $('.repeater').repeater({ isFirstItemUndeletable: true, defaultValues: { 'discount': 100 }, }); valid_products = $.parseJSON(valid_products); console.log(valid_products); $repeater.setList(valid_products); }); </script> |
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<div class="form-group {{$field[0]}} repeater"> <label for="{{$field[0]}}" class="col-md-3 control-label"> {{$field[1]}} @if(isset($field[2]) && $field[2] == true) * @endif </label> <div class="col-md-9 filled"> <input data-repeater-create type="button" value="Add"/> <table class="table table-hover dynamic_values"> <thead> <tr> <th>Item</th> <th>Discount</th> <th>Action</th> </tr> </thead> <tbody data-repeater-list="{{$field[0]}}"> <tr data-repeater-item> <td> <select class="form-control product-list" name="item_code"> <option value="">-- Select --</option> @foreach($options as $key => $item) @php $opt_value = $item->item_code; $display = $item->name . ' (' . $item->subtitle . ') : $' . $item->price; @endphp <option value="{{$opt_value}}">{{$display}}</option> @endforeach </select> </td> <td> <input type="text" class="form-control" name="discount" value="100"> </td> <td> <input data-repeater-delete type="button" value="Delete"/> </td> </tr> </tbody> </table> </div> </div> |