我有一个小Laravel脚本(订单),我使用select2作为产品列表上的搜索,但是这里有一个小问题。 Pressing "Add product" will create a new node to add a new product。 It should look like this, but there is something wrong with my newly created node,看起来像这样:Newly added node。
以下是我添加节点的脚本。
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
router.post("api/report",async(req,res) =>{
let rep = new Report({
name:req.body.name,
age:req.body.age,
customerDetails:req.body.customerDetails,
userReport:req.body.userReport
});
await readFile(req.body.userReport, function(err,data){
if (err) {
console.log(err)
} else {
rep.userReport.pdf.data = data;
rep.userReport.pdf.contentType = 'application/pdf';
rep
.save()
.then(result => {
return res.status(200).json({
result: result
});
})
})
}
查看
<script>
$('#add-product').click(function () {
let element = $(
' <div class="row new-product">\n' +
' <div class="col-xs-12 col-md-4 form-group">\n' +
' <div class="label-mb"><label for="product">Product</label></div>\n' +
' <select class="form-control kt-select2 products" id="kt_select2_2" name="products[]">\n' +
' <option selected disabled>Select a product</option>' +
' {!!$options!!}\n' +
' </select>' +
' </div>\n' +
' <div class="col-xs-12 col-md-4 form-group">\n' +
' <div class="label-mb"><label class="order-label">Product Code</label></div>\n' +
' <input type="text" class="form-control" id="product_code" name="product_code[]" placeholder="Enter product code" value="{{ old('product_code') }}"/>\n' +
' </div>\n' +
' <div class="col-xs-12 col-md-4 form-group">\n' +
' <div class="label-mb"><label class="order-label">Product Quantity</label></div>\n' +
' <input type="number" class="form-control" id="quantity" name="quantity[]" placeholder="Enter product quantity" value="{{ old('quantity') }}" />\n' +
' </div>\n' +
' <div class="col-xs-12 col-md-4 form-group">\n' +
' <div class="label-mb"><label class="order-label">Product Price</label></div>\n' +
' <input type="text" class="form-control" id="price" name="price[]" placeholder="Enter product price" value="{{ old('price') }}" />\n' +
' </div>\n' +
' <div class="col-xs-12 col-md-4 form-group">\n' +
' <div class="label-mb"><label class="order-label">Discount</label></div>\n' +
'<div class="input-group">\n' +
'<div class="input-group-prepend"><span class="input-group-text"><i class="fal fa-percentage"></i></span></div>\n' +
'<input type="text" id="discount" name="discount[]" class="form-control" placeholder="Enter product discount">\n' +
'</div>\n' +
' </div>\n' +
'<div class="col-lg-4">\n' +
'<div class="form-group">\n' +
'<label for="actions">Actions</label>\n' +
'<div class="input-group actions d-flex">\n' +
'<span class="delete-product"><i class="fal fa-trash"></i> Delete</span>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n' +
' </div>\n' +
'</div>');
$('.last').before(element);
});
</script>
答案 0 :(得分:0)
首先,您需要destroy all
select2,然后recreate
。
请将此代码添加到您的点击功能中
$(".kt-select2").select2('destroy');
$('.kt-select2').select2();