删除另一个字段后重新填充表单输入

时间:2016-02-03 10:40:05

标签: javascript jquery html typeahead.js

美好的一天,我有这段javascript给我带来了麻烦 https://jsfiddle.net/o5x8qgvt/2/ 当我添加一个新字段时,当我删除一个字段重新填充位置时,位置会增加,但是在产品名称上,即使我在每个字段中添加数据,它也不会重新填充它给我空白的字段。你能帮帮我吗?

使用Javascript:

// setup an "add a tag" link
var $addTagLink = $('<button href="#" class="btn btn-info add_tag_link">Adauga Produs</button>');
$('.form-buttons > .btn-group').prepend($addTagLink);
var $newLinkLi = $('.append-me');

jQuery(document).ready(function() {
  // Get the ul that holds the collection of product-group
  var $collectionHolder = $('#products-group');

  // add the "add a tag" anchor and li to the product-group ul
  $collectionHolder.append($newLinkLi);

  // count the current form inputs we have (e.g. 2), use that as the new
  // index when inserting a new item (e.g. 2)
  $collectionHolder.data('index', $collectionHolder.find(':input').length);

  $addTagLink.on('click', function(e) {
    // prevent the link from creating a "#" on the URL
    e.preventDefault();

    // add a new tag form (see code block below)
    addTagForm($collectionHolder, $newLinkLi);

    typeInitialize();
  });


});

function regenerateTagList() {
  var vals = $('.product-group > .product-counter > input').toArray().map(function(v, i) {return v.value;});
  var products = $('.product-group > .product-name > span > input:last').toArray().map(function(v, i) {return v.value;});
  console.log(products);

  $('.product-group').remove();

  $('#products-group').data('index', 1);

  for (var i = 0; i < vals.length; i++) {

    if (vals[i] !== i + 1) {

      vals[i] = i + 1;

    }

    addTagForm($('#products-group'), $newLinkLi);

    typeInitialize();

    $('.product-group > .product-counter > input:last').val(vals[i]);
    $('.product-group > .product-name > span > input:last').val(products[i]);
  }
}

function addTagForm($collectionHolder, $newLinkLi) {
  // Get the data-prototype explained earlier
  var prototype = $collectionHolder.data('prototype');

  // get the new index
  var index = $collectionHolder.data('index');

  if (index === 0) {

    index = 1;
  }

  // Replace '$$name$$' in the prototype's HTML to
  // instead be a number based on how many items we have
  var newForm = prototype.replace(/__name__/g, index);

  // increase the index with one for the next item
  $collectionHolder.data('index', index + 1);

  // Display the form in the page in an li, before the "Add a tag" link li
  var $newFormLi = $('<tr class="product-group"></tr>').append(newForm);

  // also add a remove button, just for this example
  //$('<button type="button" class="product-highlight btn btn-primary">Highlight</button>
   // <button type="button" class="remove-tag btn btn-primary">Sterge</button>').appendTo($newFormLi);

  $newLinkLi.append($newFormLi);

  // handle the removal, just for this example
  $('.remove-tag').click(function(e) {
    e.preventDefault();

    $(this).closest('tr').remove();

    regenerateTagList();

    return false;
  });

  $('.product-highlight').on('click', function(e) {
    e.preventDefault();

    $(this).closest('tr').toggleClass('toggle-highlight');;

  });
}
// Initialize Typeahead
function typeInitialize() {

  // Create typeahead instance
  var url = Routing.generate('offer_ajax_search', {
    name: 'WILDCARD'
  });
  // Trigger typeahead + bloodhound
  var products = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    identify: function(obj) {
      console.log('fired');
      return obj.u_name;
    },
    prefetch: '/export/json/products.json',
    remote: {
      url: url,
      wildcard: 'WILDCARD',
    }
  });
  products.initialize();



  $('.product-group').find('.typeahead:last').typeahead({
    minLength: 3,
    highlight: true,
    limit: 10,
  }, {
    name: 'product',
    display: 'u_name',
    source: products.ttAdapter()
  }).on('typeahead:select', function(e, datum) {

    e.preventDefault();
    /* Act on the event */

    var information = '<div class="col-md-2"><label for="boxCode" class="label-control">BEX Code</label><input class="form-control" type="text" disabled="true" value="' + datum.u_bexCode + '"/></div>';
    information += '<div class="col-md-2"><label for="base-price">Pret de baza</label><input class="form-control " type="text" disabled="true" value="' + datum.u_image + '"/></div>';

    var div = $(this).parent().closest('.form-group').find('.product-information');
    $(div).empty().append(information);
  }).on('typeahead:autocomplete', function(e, datum) {
    e.preventDefault();
    /* Act on the event */

    var information = '<div class="col-md-2"><label for="boxCode" class="label-control">BEX Code</label><input class="form-control" type="text" disabled="true" value="' + datum.u_bexCode + '"/></div>';
    information += '<div class="col-md-2"><label for="base-price">Pret de baza</label><input class="form-control " type="text" disabled="true" value="' + datum.u_image + '"/></div>';

    var div = $(this).parent().closest('.form-group').find('.product-information');
    $(div).empty().append(information);
  });

  $("input[type='text']").on("click", function() {
    $(this).select();
  });
}

HTML:

<div class="row">
    <div class="col-md-12">
    <form name="offer" method="post" action="/app_dev.php/offer-generator/new/submited" class="form-horizontal">
    <div class="form-group"><label class="col-sm-2 control-label required" for="offer_name">Name</label><div class="col-sm-10"><input type="text" id="offer_name" name="offer[name]" required="required" maxlength="255" class="form-control"></div>
</div>

    <div id="products-group" data-prototype="<td class=&quot;product-counter col-md-1&quot;><input type=&quot;text&quot; id=&quot;offer_products___name___position&quot; name=&quot;offer[products][__name__][position]&quot; disabled=&quot;disabled&quot; required=&quot;required&quot; class=&quot;counter form-control&quot; value=&quot;__name__&quot; /></td>
<td class=&quot;product-name&quot;><input type=&quot;text&quot; id=&quot;offer_products___name___name&quot; name=&quot;offer[products][__name__][name]&quot; required=&quot;required&quot; class=&quot;typeahead product form-control&quot; name=&quot;product&quot; id=&quot;products&quot; data-provider=&quot;product&quot; placeholder=&quot;Nume Produs&quot; autocomplete=&quot;false&quot; /></td>
<td class=&quot;col-md-1&quot;><input type=&quot;text&quot; id=&quot;offer_products___name___price&quot; name=&quot;offer[products][__name__][price]&quot; required=&quot;required&quot; class=&quot;form-control&quot; /></td>
<td class=&quot;col-md-1&quot;><input type=&quot;text&quot; id=&quot;offer_products___name___quantity&quot; name=&quot;offer[products][__name__][quantity]&quot; required=&quot;required&quot; class=&quot;form-control&quot; /></td>
<td class=&quot;product-action&quot;>
<div class=&quot;btn-group&quot; role=&quot;group&quot; aria-label=&quot;...&quot;>
    <button type=&quot;button&quot; class=&quot;product-highlight btn btn-warning&quot;>Highlight</button>
    <button type=&quot;button&quot; class=&quot;remove-tag btn btn-danger&quot;>delete</button>
</div>
</td>
"><table class="table table-striped table-hover table-bordered append-me">
                <thead>
            <tr>
                <th class="col-md-1">Position</th>
                <th>Product</th>
                <th class="col-md-1">Price</th>
                <th class="col-md-1">Quantity</th>
                <th class="col-md-2">Action</th>
            </tr>
        </thead>

        <tbody>
                </tbody>
    </table></div>
    </form></div>
    <div class="form-buttons">
        <div class="btn-group" role="group" aria-label="..."><button href="#" class="btn btn-info add_tag_link">Add Product</button>
          <button type="submit" id="offer_save" name="offer[save]" class="btn-default btn">Create Offer</button>
          <button type="submit" id="offer_send_email" name="offer[send_email]" class="btn-default btn">Send Email</button>
          <button type="submit" id="offer_export_excel" name="offer[export_excel]" class="btn-default btn">Export Excel</button>
          <button type="submit" id="offer_export_pdf" name="offer[export_pdf]" class="btn-default btn">Export PDF</button>
        </div>
    </div>
    <div class="form-group"><div class="col-sm-2"></div><div class="col-sm-10"><div id="offer_products" data-prototype="<div class=&quot;form-group&quot;><label class=&quot;col-sm-2 control-label required&quot;>__name__label__</label><div class=&quot;col-sm-10&quot;><div id=&quot;offer_products___name__&quot;><div class=&quot;form-group&quot;><label class=&quot;col-sm-2 control-label required&quot; for=&quot;offer_products___name___pricePerLine&quot;>Line Total</label><div class=&quot;col-sm-10&quot;><input type=&quot;text&quot; id=&quot;offer_products___name___pricePerLine&quot; name=&quot;offer[products][__name__][pricePerLine]&quot; required=&quot;required&quot; class=&quot;form-control&quot; /></div>
</div></div></div>
</div>"></div></div>
</div><input type="hidden" id="offer__token" name="offer[_token]" class="form-control" value="cSVdt50kupA_BwFeY4T43PslnfjiA-UgjT4EA_HBdqs">
  </div>

0 个答案:

没有答案