复制内容
<input type="button" value="复制运单号" title="该操作将所有选择的行复制运单号" class="layui-btn layui-btn-danger multi_copy_class">
<input type="hidden" id="copyContentInput" value="">
<script src="https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.6/clipboard.min.js"></script>
var clipboard = new ClipboardJS('.multi_copy_class', {
text: function() {
get_multi_copy('express_no')
var res = layui.jquery('#copyContentInput').val();
return res;
}
});
clipboard.on('success', function(e) {
layer.msg("已复制到剪切板,内容为<br>" + e.text)
e.clearSelection();
});
clipboard.on('error', function(e) {
layer.msg("复制失败!")
});
function get_multi_copy(field){
var checkStatus = layui.table.checkStatus('table');
var data = checkStatus.data;
if (data.length <= 0) {
layer.msg('请选择要操作的行');
return false;
}
let ids = [];
for(let i in data){
ids.push(data[i]['id']);
}
var index;
layui.jquery.ajax({
url: '{:url("/backend/package/getFieldContent")}',
data: {ids : ids.join(), field: field},
dataType: "json",
async:false,
beforeSend:function(){
index = layer.load();
},
complete:function(){
layer.close(index);
},
success: function(res) {
layui.jquery('#copyContentInput').val(res.data)
},
error:function(res){
layer.msg(res.responseJSON.reason);
layer.close(index);
layui.jquery('#copyContentInput').val('')
},
});
}
数据表格多选框自定义是否显示
全选时,全选输入框无法自动勾选,单不影响功能
{field: 'checkbox', title: '<input type="checkbox" name="layTableCheckbox" lay-skin="primary" lay-filter="layTableAllChoose">', width: 60,templet: function(d){
if (d.status === '显示'){
return '<input type="checkbox" name="layTableCheckbox" lay-skin="primary">'
} else{
return ''
}
}},
ajax弹窗
<div id="update_view" style="display: none;"></div>
<script id="update_template" type="text/html">
<form class="layui-form">
<input type="number" name="id" value="{{ d.data.id }}" hidden>
<div class="layui-form-item">
<label class="layui-form-label w100">姓名</label>
<div class="layui-input-inline">
<input type="text" name="provide_username" value="{{ d.data.provide_username }}" required lay-verify="required" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label w100">手机号</label>
<div class="layui-input-inline">
<input type="number" name="provide_user_phone" value="{{ d.data.provide_user_phone }}" required lay-verify="required|phone" autocomplete="off" class="layui-input">
</div>
</div>
<div class="tac">
<button type="reset" onclick="update_cancel()" class="layui-btn layui-btn-primary">取消</button>
<button class="layui-btn layui-btn-normal" lay-submit lay-filter="update">保存</button>
</div>
</form>
</script>
var getTpl = update_template.innerHTML,update_view = document.getElementById('update_view');
layui.laytpl(getTpl).render({data:res.data}, function(html){
update_view.innerHTML = html;
index_update_view = layer.open({
type: 1,
title: '编辑发放人',
area: '500px',
content: layui.jquery('#update_view')
});
});
富文本
编辑时,富文本内容需要使用ajax请求json数据方式填充,直接使用后端语言渲染有可能导致显示错乱
<div id="editor" class="mt20"></div>
<script src="__STATIC__/js/wangEditor.min.js"></script>
<script>
var E = window.wangEditor;
var editor = new E('#editor');
editor.customConfig.uploadImgShowBase64 = true;
editor.customConfig.menus = [
'head', // 标题
'bold', // 粗体
'fontSize', // 字号
'fontName', // 字体
'italic', // 斜体
'underline', // 下划线
'strikeThrough', // 删除线
'foreColor', // 文字颜色
'backColor', // 背景颜色
'link', // 插入链接
'list', // 列表
'justify', // 对齐方式
'quote', // 引用
'emoticon', // 表情
'image', // 插入图片
'table', // 表格
// 'video', // 插入视频
'code', // 插入代码
'undo', // 撤销
'redo' // 重复
];
// 配置服务器端地址
editor.customConfig.uploadImgServer = '{:url("/backend/resource")}';
editor.customConfig.uploadFileName = 'file';
editor.customConfig.uploadImgParams = {
type : 'editor_img'
};
var editor_index;
editor.customConfig.uploadImgHooks = {
before: function (xhr, editor, files) {
editor_index = layer.load();
},
success: function (xhr, editor, result) {
layer.close(editor_index);
},
fail: function (xhr, editor, result) {
layer.close(editor_index);
layer.msg(result.msg);
},
error: function (xhr, editor) {
layer.close(editor_index);
layer.msg('图片上传出错');
},
timeout: function (xhr, editor) {
layer.close(editor_index);
layer.msg('文件上传超时');
},
customInsert: function (insertImg, result, editor) {
layer.close(editor_index);
if (result.code === 0){
var url = result.data;
insertImg(url)
} else{
layer.msg(result.msg);
}
}
};
editor.customConfig.customAlert = function (info) {
layui.msg(info);
};
editor.create();
var note = editor.txt.html();//获取文本内容
</script>
上传多个图片
<div id="img-input"></div>
<div class="layui-form-item mt20">
<div>上传课程轮播图(图片尺寸:750*320 px)(<span id="img_total">0</span>/5)(已上传的图片,点击可进行删除操作)</div>
<div class="mt10">
<div id="img-div"></div>
<div class="mt20" id="upload_div">
<button type="button" class="layui-btn layui-btn-normal ml20" id="upload2"><i class="layui-icon layui-icon-upload"></i>上传</button>
</div>
</div>
</div>
let img_id = 1;
let img_total = 0;
upload.render({
elem: '#upload2',
data: {
type : 'carousel_img',
},
url: '{:url("/backend/resource")}',
accept : 'images',
exts: 'png|jpg',
acceptMime: 'image/jpg, image/png',
size : 8*1024,
before:function(){
layer.load()
},
done: function(res){
layer.closeAll('loading');
if (res.code === 0){
let img_div_template = '<img id="img'+img_id+'" onclick="del('+img_id+')" class="carousel-img" src="'+res.data+'">';
$('#img-div').append(img_div_template);
let img_input_template = '<input type="hidden" id="img_input'+img_id+'" name="carousel_imgs[]" value="'+res.key+'">';
$('#img-input').append(img_input_template);
img_id += 1;
img_total += 1;
$('#img_total').text(img_total);
if (img_total >= 5){
$('#upload_div').hide();
}
layer.msg('上传成功');
} else{
layer.msg(res.msg);
}
},
error: function(res){
layer.closeAll('loading');
layer.msg(res.msg,{icon: 2});
}
});
上传单个图片
<div class="layui-form-item mt20">
<input type="hidden" name="img" value="{$item.img}" id="img">
<div>礼券图片</div>
<div class="mt10">
<img id="carousel-img" class="layui-icon layui-icon-picture carousel-img" src="{$item.img_url}">
<button type="button" class="layui-btn ml20" id="upload">
<i class="layui-icon layui-icon-upload"></i>上传图片
</button>
</div>
</div>
upload.render({
elem: '#upload',
data: {
type : 'ad_img',
},
url: '{:url("/backend/resource")}',
accept : 'images',
exts: 'png|jpg',
acceptMime: 'image/jpg, image/png',
size : 8*1024*1024,
before:function(){
layer.load()
},
done: function(res){
layer.closeAll('loading');
if (res.code === 0){
$('#carousel-img').attr('src', res.data);
$('#img').val(res.key);
layer.msg('上传成功');
} else{
layer.msg(res.msg);
}
},
error: function(res){
layer.closeAll('loading');
layer.msg(res.msg,{icon: 2});
}
});
七牛云大文件上传
//上传进度条
<div id="progress" style="display: none;margin: 10px 0">
<div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="progress-value">
<div class="layui-progress-bar layui-bg-green" lay-percent="0%"></div>
</div>
</div>
layui.use(['upload','element'], function(){
let upload = layui.upload;
let element = layui.element;
upload.render({
elem: '#upload2',
size : 4*1024*1024*1024,
url: '{:url("/backend/resource")}',
accept : 'video'
,auto: false
,bindAction: '#testListAction'
,choose: function(obj){
obj.preview(function(index, file, result){
var index;
layui.jquery.ajax({
type: "POST",
url: '{:url("/backend/resource/token")}',
dataType: "json",
beforeSend:function(){
index = layer.load();
},
complete:function(){
layer.close(index);
},
success: function(res) {
layer.close(index);
if (res.code === 0){
let progress_index;
const domain = res.data.domain;
const key = res.data.key+'.'+file.name.split('.')[1];
const token = res.data.token;
const putExtra = {
fname: '',
params: {},
mimeType: ["video/mp4"],
};
const config = {
useCdnDomain: true, //使用cdn加速
};
const observable = qiniu.upload(file, key, token, putExtra, config);
progress_index = layer.open({
type: 1,
title: '文件上传[总大小: '+(file.size/(1024*1024)).toFixed(2)+' MB]',
content: layui.jquery('#progress'),
area: '80%',
cancel: function(index){
layer.confirm('确定要取消上传吗?', function (index2) {
layer.close(index);
layer.close(index2);
subscription.unsubscribe() // 上传取消
});
return false;
}
});
var subscription = observable.subscribe({
next: (result) => {
console.log(result);
let percent = result.total.percent;
percent = Math.round(percent*100)/100;
console.log(percent);
layui.element.progress('progress-value', percent+'%')
},
error: () => {
layer.msg('上传图片失败');
layer.close('progress_index');
},
complete: (res) => {
console.log(res);
layer.close(progress_index);
if (res.key){
layui.jquery('#carousel-img2').attr('src', domain+res.key);
layui.jquery('#carousel-img2').attr('type', file.type);
layui.jquery('#img2').val(res.key);
layui.jquery('#mimetype').val(file.type);
layer.msg('上传成功');
} else{
layer.msg(res.msg);
}
},
});
} else{
layer.msg(res.msg);
}
},
error:function(){
layer.close(index);
layer.msg('请求失败');
},
});
});
}
});
});
监听输入框Enter
事件
layui.jquery('#search_key').on('keydown', function (event) {
if (event.key === 'Enter') {
search();
return false
}
});
layui
的ajax
写法
var index;
layui.jquery.ajax({
type: "POST",
url: '{:url("/backend/institution/imgs")}',
data: {id: id},
dataType: "json",
beforeSend: function () {
index = layer.load();
},
complete: function () {
layer.close(index);
},
success: function (res) {
if (res.code === 404){
layer.msg(res.msg);
return false;
}
layer.photos({
photos: res
, anim: 5
});
},
error: function () {
layer.close(index);
layer.msg('请求失败');
},
});