来自 JSON 的 bootstrap 弹出窗口的自动完成内容

Autocomplete content of bootstrap popover from JSON

在一个表单中,我有 2 个输入。在第一个输入中,我使用了一个数据列表,我通过 JSON 加载它,第二个输入在第一个输入更改时自动完成。

到这里一切正常!

我添加了一个新按钮,我使用 bootstrap 中的弹出窗口,我想根据用户的选择在那里显示一些文本(如上所述)。我要显示的文字是元素labels.

的信息

我阅读了相关的question,但我到现在还没有成功..

var dataList = $('.products');
var jsonOptions = [{
  "product": "11111",
  "description": "description 1",
  "labels": [{
    "version": "01",
    "quantity": 500
  }, {
    "version": "02",
    "quantity": 800
  }, ]
}, {
  "product": "22222",
  "description": "description 2",
  "labels": [{
    "version": "01",
    "quantity": 900
  }, {
    "version": "02",
    "quantity": 100
  }, ]
}, {
  "product": "33333",
  "description": "description 3",
  "labels": [{
    "version": "01",
    "quantity": 200
  }, {
    "version": "02",
    "quantity": 4300
  }, ]
}];

jsonOptions.forEach(function(item) {

  var option = '<option value="' + item.product + '">' + item.description + '</option>';

  dataList.append(option);
});

$(function() {
  $('body').on('input', '.product,.products', function() {

    var i = this.value;
    var description = "";
    var productsInBox = 0;
    var text = "";

    jsonOptions.forEach(function(a) {
      if (a.product == i) {
        description = a.description;
        productsInBox = a.productsInBox;
        for (let i = 0, l = a.labels.length; i < l; i++) {
          text += "version " + a.labels[i].version + " has " + a.labels[i].quantity + "\n"
        }

      }
    });
    $(this).closest('.form-group').find('.description').val(description);
    $(this).closest('.form-group').find('.mytext').val(text);
    console.log(text);

  });
});

$('[data-toggle="popover"]').popover();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>


<form id="form1" method="post" class="form-horizontal" role="form">
  <fieldset>
    <div class="form-group">
      <div class="col-sm-2">
        <input type="text" list="products" class="form-control product" name="product[]" />
        <datalist id="products" class="products"></datalist>
      </div>

      <div class="col-sm-3">
        <input id="" type="text" class="form-control description" name=" description[]" />
      </div>
      
      
      <div class="col-sm-3" style="margin-left: 10px;">
        <button type="button" class="btn btn-info btn-group-sm mytext" data-toggle="popover" data-content="text to change to like:  version 01 has 500 "><i class="fa fa-info"></i></button>
      </div>

    </div>
  </fieldset>
</form>

您可以使用另一个 forEach 循环遍历 labels 并获取当前选择的 versionsquantities,将它们附加到预定义的空变量然后使用 attr() 方法将它们添加到弹出窗口:

var dataList = $('.products');
var jsonOptions = [{
  "product": "11111",
  "description": "description 1",
  "labels": [{
    "version": "01",
    "quantity": 500
  }, {
    "version": "02",
    "quantity": 800
  }, ]
}, {
  "product": "22222",
  "description": "description 2",
  "labels": [{
    "version": "01",
    "quantity": 900
  }, {
    "version": "02",
    "quantity": 100
  }, ]
}, {
  "product": "33333",
  "description": "description 3",
  "labels": [{
    "version": "01",
    "quantity": 200
  }, {
    "version": "02",
    "quantity": 4300
  }, ]
}];

jsonOptions.forEach(function(item) {

  var option = '<option value="' + item.product + '">' + item.description + '</option>';

  dataList.append(option);
});

$(function() {
  $('body').on('input', '.product,.products', function() {

    var i = this.value;
    var description = "";
    var productsInBox = 0;
    var text = "";
    var version = '';
    var quantity = '';
    jsonOptions.forEach(function(a) {
      if (a.product == i) {
        description = a.description;
        productsInBox = a.productsInBox;
        text = a.labels
        a.labels.forEach(function(el) {
          version += el.version + " ";
          quantity += el.quantity + " ";
        });
      }
    });
    
    $('[data-toggle="popover"]').attr('data-content', "versions are: " + version + ' and quantity are: ' + quantity).data('bs.popover').setContent();
    $(this).closest('.form-group').find('.description').val(description);
    $(this).closest('.form-group').find('.mytext').val(description);


  });
});

$('[data-toggle="popover"]').popover();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>


<form id="form1" method="post" class="form-horizontal" role="form">
  <fieldset>
    <div class="form-group">
      <div class="col-sm-2">
        <input type="text" list="products" class="form-control product" name="product[]" />
        <datalist id="products" class="products"></datalist>
      </div>

      <div class="col-sm-3">
        <input id="" type="text" class="form-control description" name=" description[]" />
      </div>

      <div class="col-sm-3" style="margin-left: 10px;">
        <button type="button" class="btn btn-info btn-group-sm mytext" data-toggle="popover" data-content="text to change to like:  version 01 has 500 "><i class="fa fa-info"></i></button>
      </div>

    </div>
  </fieldset>
</form>

如果我的理解正确,您将需要根据所选值更新属性数据内容。

<div class="col-sm-3" style="margin-left: 10px;">
   <button type="button" class="btn btn-info btn-group-sm mytext" data-toggle="popover" data-content="text to change to like:  version 01 has 500 ">  
   <i class="fa fa-info"></i></button>
</div>

更新了您的代码段。 可能您需要处理:更改选择时如何关闭弹出窗口

var dataList = $('.products');
var jsonOptions = [{
  "product": "11111",
  "description": "description 1",
  "labels": [{
    "version": "01",
    "quantity": 500
  }, {
    "version": "02",
    "quantity": 800
  }, ]
}, {
  "product": "22222",
  "description": "description 2",
  "labels": [{
    "version": "01",
    "quantity": 900
  }, {
    "version": "02",
    "quantity": 100
  }, ]
}, {
  "product": "33333",
  "description": "description 3",
  "labels": [{
    "version": "01",
    "quantity": 200
  }, {
    "version": "02",
    "quantity": 4300
  }, ]
}];

jsonOptions.forEach(function(item) {

  var option = '<option data- value="' + item.product + '">' + item.description + '</option>';

  dataList.append(option);
  
});

$(function() {
  $("#clear").click(function (e) {
    $(".product, .description").val("");
    $('.mytext').popover('hide');
  });

  $('body').on('input', '.product', function() {

    var i = this.value;
    
    var description = "";
    var productsInBox = 0;
    var labelData = "";

    jsonOptions.forEach(function(a) {
      if (a.product == i) {
        description = a.description;
        productsInBox = a.productsInBox;
        labelData = 
          'product ' + a.product + ' | ' +
          'version: ' + a.labels[0].version + 
          ' | quantity: ' + a.labels[0].quantity + " | " +
          'version: ' + a.labels[1].version + 
          ' | quantity: ' + a.labels[1].quantity + "\r\n";
       }
    });
    
    $(this).closest('.form-group').find('.description').val(description);

    $(this).closest('.form-group').find('.mytext').attr("data-content", labelData);
    $('.mytext').popover('toggle').popover('show');

  });
});

// $('.mytext').popover();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>


<form id="form1" method="post" class="form" role="form">
  <fieldset>
    <div class="form-group">
      <a class="btn btn-danger" id="clear">clear</a>
    
      <div class="col-sm-2">
        <input type="text" list="products" class="form-control product" name="product[]" />
        <datalist id="products" class="products"></datalist>
      </div>

      <div class="col-sm-3">
        <input id="" type="text" class="form-control description" name=" description[]" />
      </div>

      <div class="col-sm-3">
        <button type="button" class="btn btn-info btn-group-sm mytext" data-toggle="popover" data-content="text to change to like:  version 01 has 500 "><i class="fa fa-info"></i></button>
        
        
      </div>

      

    </div>
  </fieldset>
</form>