$(function () { $('.has-many-spec .add.btn').parent().attr('class', 'col-md-12') .after(`
 同步供应商最新规格
 批量设置库存
 批量设置市场价
 批量设置销售价
`); $('.has-many-spec_self .add.btn').parent().attr('class', 'col-md-12') .after(`
 批量新增
 删除所有
`); //切换类型 $('input[name="type"]').change(function () { if ($(this).val() == 3) { $('#btn-group1').hide(); $('#btn-group2').show(); } else { $('#btn-group1').show(); $('#btn-group2').hide(); } $('.has-many-spec-forms').html(''); $('.has-many-spec_self-form').html(''); }).change(); //批量新增 $('.has-many-spec_self .batch-add').click(function () { var name = prompt('请输入规格名称'); if (!name) { return; } var date = new Date(); date = prompt('请输入起始日期', date.format('yyyy-MM-dd')); if (!date || isNaN(new Date(date).getTime())) { return; } var num = parseInt(prompt('请输入新增数量,最多不超过180', '30')); if (!num || isNaN(num)) { return; } if (num > 180) { num = 180; } var stock = parseInt(prompt('请输入默认库存', '9999')); if (!stock || isNaN(stock)) { return; } var original_price = parseFloat(prompt('请输入默认市场价')); if (!original_price || isNaN(original_price)) { return; } var price = parseFloat(prompt('请输入默认销售价')); if (!price || isNaN(price)) { return; } $('template.spec-tpl .field_name').prop('value', name); var html = $('template.spec_self-tpl').html(); var fields = { 'name': name, 'date': date, 'stock': stock, 'original_price': original_price, 'price': price, }; for (var key in fields) { html = html.replace('name="spec_self[new___LA_KEY__][' + key + ']" value=""', 'name="spec_self[new___LA_KEY__][' + key + ']" value="' + fields[key] + '"'); } var nestedIndex = $('.has-many-spec-forms tr').length - 1; function replaceNestedFormIndex(value) { return String(value).replace(/__LA_KEY__/g, nestedIndex); } for (var i = 0; i < num; i++) { nestedIndex++; var d = new Date(date); d.setDate(d.getDate() + i); var date_template = replaceNestedFormIndex(html.replace(date, d.format('yyyy-MM-dd'))); $('.has-many-spec_self-forms').append(date_template); } }); //删除所有 $('.has-many-spec_self .batch-delete').click(function () { $('.has-many-spec_self-forms .remove.btn').click(); }); //同步最新规格 var template = $('template.spec-tpl').html(); var is_load = false; $('.spec-sync').click(function () { if (is_load) { Dcat.confirm('本次载入将清除上次载入的所有数据,是否继续?', null, function () { Dcat.loading(); post(); }); } else { Dcat.loading(); post(); is_load = true; } function post() { $.ajax({ url: '`{{url}}`', method: 'POST', data: { product_id: $('input[name="product_id"]').val(), agent_product_id: `{{agent_product_id}}`, _form_: '`{{class}}`', }, success: function (res) { var fields = ['supplier_name', 'supplier_date', 'supplier_price', 'supplier_stock', 'supplier_cost_price', 'price', 'stock', 'original_price', 'product_spec_id']; var data = res.data; var forms = $('.has-many-spec-forms'); //只remove掉上次载入的,原来数据库保存的记录(.field_id不为空)不处理 forms.children().each(function () { if (!$(this).find('.field_id').val()) { $(this).remove(); } //清理供应商已删除的规格 else if ($(this).find('.field_supplier_stock').val() == 0 && $(this).find('.field_supplier_price').val() == 0) { $(this).find('.remove.btn').click(); } }); for (var key in data) { var row = $(template.replace(/new___LA_KEY__/g, key)); for (var i=0; i parseFloat(value) ? value : num; $(this).val(result); }); }); //批量设置市场价 $('.batch-original-price').click(function () { var num = prompt('请输入加价金额(在供应商销售价基础上加)'); if (!/^\d+$/.test(num)) { return; } $('.has-many-spec input.field_original_price').each(function () { var value = $(this).parents('tr').find('.field_supplier_price').val(); var result = parseFloat(value) + parseFloat(num); $(this).val(result.toFixed(2)); }); }); //批量销售价 $('.batch-price').click(function () { var num = prompt('请输入加价金额(在供应商销售价基础上加)'); if (!/^\d+$/.test(num)) { return; } $('.has-many-spec input.field_price').each(function () { var value = $(this).parents('tr').find('.field_supplier_price').val(); var result = parseFloat(value) + parseFloat(num); $(this).val(result.toFixed(2)); }); }); }); Date.prototype.format = function (fmt) { var o = { 'M+': this.getMonth() + 1, //月份 'd+': this.getDate(), //日 'h+': this.getHours(), //小时 'm+': this.getMinutes(), //分 's+': this.getSeconds(), //秒 'q+': Math.floor((this.getMonth() + 3) / 3), //季度 'S': this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))); } } return fmt; };