复制内容

  1. <input type="button" value="复制运单号" title="该操作将所有选择的行复制运单号" class="layui-btn layui-btn-danger multi_copy_class">
  2. <input type="hidden" id="copyContentInput" value="">
  1. <script src="https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.6/clipboard.min.js"></script>
  2. var clipboard = new ClipboardJS('.multi_copy_class', {
  3. text: function() {
  4. get_multi_copy('express_no')
  5. var res = layui.jquery('#copyContentInput').val();
  6. return res;
  7. }
  8. });
  9. clipboard.on('success', function(e) {
  10. layer.msg("已复制到剪切板,内容为<br>" + e.text)
  11. e.clearSelection();
  12. });
  13. clipboard.on('error', function(e) {
  14. layer.msg("复制失败!")
  15. });
  16. function get_multi_copy(field){
  17. var checkStatus = layui.table.checkStatus('table');
  18. var data = checkStatus.data;
  19. if (data.length <= 0) {
  20. layer.msg('请选择要操作的行');
  21. return false;
  22. }
  23. let ids = [];
  24. for(let i in data){
  25. ids.push(data[i]['id']);
  26. }
  27. var index;
  28. layui.jquery.ajax({
  29. url: '{:url("/backend/package/getFieldContent")}',
  30. data: {ids : ids.join(), field: field},
  31. dataType: "json",
  32. async:false,
  33. beforeSend:function(){
  34. index = layer.load();
  35. },
  36. complete:function(){
  37. layer.close(index);
  38. },
  39. success: function(res) {
  40. layui.jquery('#copyContentInput').val(res.data)
  41. },
  42. error:function(res){
  43. layer.msg(res.responseJSON.reason);
  44. layer.close(index);
  45. layui.jquery('#copyContentInput').val('')
  46. },
  47. });
  48. }

数据表格多选框自定义是否显示

全选时,全选输入框无法自动勾选,单不影响功能

  1. {field: 'checkbox', title: '<input type="checkbox" name="layTableCheckbox" lay-skin="primary" lay-filter="layTableAllChoose">', width: 60,templet: function(d){
  2. if (d.status === '显示'){
  3. return '<input type="checkbox" name="layTableCheckbox" lay-skin="primary">'
  4. } else{
  5. return ''
  6. }
  7. }},

ajax弹窗

  1. <div id="update_view" style="display: none;"></div>
  2. <script id="update_template" type="text/html">
  3. <form class="layui-form">
  4. <input type="number" name="id" value="{{ d.data.id }}" hidden>
  5. <div class="layui-form-item">
  6. <label class="layui-form-label w100">姓名</label>
  7. <div class="layui-input-inline">
  8. <input type="text" name="provide_username" value="{{ d.data.provide_username }}" required lay-verify="required" autocomplete="off" class="layui-input">
  9. </div>
  10. </div>
  11. <div class="layui-form-item">
  12. <label class="layui-form-label w100">手机号</label>
  13. <div class="layui-input-inline">
  14. <input type="number" name="provide_user_phone" value="{{ d.data.provide_user_phone }}" required lay-verify="required|phone" autocomplete="off" class="layui-input">
  15. </div>
  16. </div>
  17. <div class="tac">
  18. <button type="reset" onclick="update_cancel()" class="layui-btn layui-btn-primary">取消</button>
  19. <button class="layui-btn layui-btn-normal" lay-submit lay-filter="update">保存</button>
  20. </div>
  21. </form>
  22. </script>
  23. var getTpl = update_template.innerHTML,update_view = document.getElementById('update_view');
  24. layui.laytpl(getTpl).render({data:res.data}, function(html){
  25. update_view.innerHTML = html;
  26. index_update_view = layer.open({
  27. type: 1,
  28. title: '编辑发放人',
  29. area: '500px',
  30. content: layui.jquery('#update_view')
  31. });
  32. });

富文本

编辑时,富文本内容需要使用ajax请求json数据方式填充,直接使用后端语言渲染有可能导致显示错乱

  1. <div id="editor" class="mt20"></div>
  2. <script src="__STATIC__/js/wangEditor.min.js"></script>
  3. <script>
  4. var E = window.wangEditor;
  5. var editor = new E('#editor');
  6. editor.customConfig.uploadImgShowBase64 = true;
  7. editor.customConfig.menus = [
  8. 'head', // 标题
  9. 'bold', // 粗体
  10. 'fontSize', // 字号
  11. 'fontName', // 字体
  12. 'italic', // 斜体
  13. 'underline', // 下划线
  14. 'strikeThrough', // 删除线
  15. 'foreColor', // 文字颜色
  16. 'backColor', // 背景颜色
  17. 'link', // 插入链接
  18. 'list', // 列表
  19. 'justify', // 对齐方式
  20. 'quote', // 引用
  21. 'emoticon', // 表情
  22. 'image', // 插入图片
  23. 'table', // 表格
  24. // 'video', // 插入视频
  25. 'code', // 插入代码
  26. 'undo', // 撤销
  27. 'redo' // 重复
  28. ];
  29. // 配置服务器端地址
  30. editor.customConfig.uploadImgServer = '{:url("/backend/resource")}';
  31. editor.customConfig.uploadFileName = 'file';
  32. editor.customConfig.uploadImgParams = {
  33. type : 'editor_img'
  34. };
  35. var editor_index;
  36. editor.customConfig.uploadImgHooks = {
  37. before: function (xhr, editor, files) {
  38. editor_index = layer.load();
  39. },
  40. success: function (xhr, editor, result) {
  41. layer.close(editor_index);
  42. },
  43. fail: function (xhr, editor, result) {
  44. layer.close(editor_index);
  45. layer.msg(result.msg);
  46. },
  47. error: function (xhr, editor) {
  48. layer.close(editor_index);
  49. layer.msg('图片上传出错');
  50. },
  51. timeout: function (xhr, editor) {
  52. layer.close(editor_index);
  53. layer.msg('文件上传超时');
  54. },
  55. customInsert: function (insertImg, result, editor) {
  56. layer.close(editor_index);
  57. if (result.code === 0){
  58. var url = result.data;
  59. insertImg(url)
  60. } else{
  61. layer.msg(result.msg);
  62. }
  63. }
  64. };
  65. editor.customConfig.customAlert = function (info) {
  66. layui.msg(info);
  67. };
  68. editor.create();
  69. var note = editor.txt.html();//获取文本内容
  70. </script>

上传多个图片

  1. <div id="img-input"></div>
  2. <div class="layui-form-item mt20">
  3. <div>上传课程轮播图(图片尺寸:750*320 px)(<span id="img_total">0</span>/5)(已上传的图片,点击可进行删除操作)</div>
  4. <div class="mt10">
  5. <div id="img-div"></div>
  6. <div class="mt20" id="upload_div">
  7. <button type="button" class="layui-btn layui-btn-normal ml20" id="upload2"><i class="layui-icon layui-icon-upload"></i>上传</button>
  8. </div>
  9. </div>
  10. </div>
  11. let img_id = 1;
  12. let img_total = 0;
  13. upload.render({
  14. elem: '#upload2',
  15. data: {
  16. type : 'carousel_img',
  17. },
  18. url: '{:url("/backend/resource")}',
  19. accept : 'images',
  20. exts: 'png|jpg',
  21. acceptMime: 'image/jpg, image/png',
  22. size : 8*1024,
  23. before:function(){
  24. layer.load()
  25. },
  26. done: function(res){
  27. layer.closeAll('loading');
  28. if (res.code === 0){
  29. let img_div_template = '<img id="img'+img_id+'" onclick="del('+img_id+')" class="carousel-img" src="'+res.data+'">';
  30. $('#img-div').append(img_div_template);
  31. let img_input_template = '<input type="hidden" id="img_input'+img_id+'" name="carousel_imgs[]" value="'+res.key+'">';
  32. $('#img-input').append(img_input_template);
  33. img_id += 1;
  34. img_total += 1;
  35. $('#img_total').text(img_total);
  36. if (img_total >= 5){
  37. $('#upload_div').hide();
  38. }
  39. layer.msg('上传成功');
  40. } else{
  41. layer.msg(res.msg);
  42. }
  43. },
  44. error: function(res){
  45. layer.closeAll('loading');
  46. layer.msg(res.msg,{icon: 2});
  47. }
  48. });

上传单个图片

  1. <div class="layui-form-item mt20">
  2. <input type="hidden" name="img" value="{$item.img}" id="img">
  3. <div>礼券图片</div>
  4. <div class="mt10">
  5. <img id="carousel-img" class="layui-icon layui-icon-picture carousel-img" src="{$item.img_url}">
  6. <button type="button" class="layui-btn ml20" id="upload">
  7. <i class="layui-icon layui-icon-upload"></i>上传图片
  8. </button>
  9. </div>
  10. </div>
  11. upload.render({
  12. elem: '#upload',
  13. data: {
  14. type : 'ad_img',
  15. },
  16. url: '{:url("/backend/resource")}',
  17. accept : 'images',
  18. exts: 'png|jpg',
  19. acceptMime: 'image/jpg, image/png',
  20. size : 8*1024*1024,
  21. before:function(){
  22. layer.load()
  23. },
  24. done: function(res){
  25. layer.closeAll('loading');
  26. if (res.code === 0){
  27. $('#carousel-img').attr('src', res.data);
  28. $('#img').val(res.key);
  29. layer.msg('上传成功');
  30. } else{
  31. layer.msg(res.msg);
  32. }
  33. },
  34. error: function(res){
  35. layer.closeAll('loading');
  36. layer.msg(res.msg,{icon: 2});
  37. }
  38. });

七牛云大文件上传

  1. //上传进度条
  2. <div id="progress" style="display: none;margin: 10px 0">
  3. <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="progress-value">
  4. <div class="layui-progress-bar layui-bg-green" lay-percent="0%"></div>
  5. </div>
  6. </div>
  7. layui.use(['upload','element'], function(){
  8. let upload = layui.upload;
  9. let element = layui.element;
  10. upload.render({
  11. elem: '#upload2',
  12. size : 4*1024*1024*1024,
  13. url: '{:url("/backend/resource")}',
  14. accept : 'video'
  15. ,auto: false
  16. ,bindAction: '#testListAction'
  17. ,choose: function(obj){
  18. obj.preview(function(index, file, result){
  19. var index;
  20. layui.jquery.ajax({
  21. type: "POST",
  22. url: '{:url("/backend/resource/token")}',
  23. dataType: "json",
  24. beforeSend:function(){
  25. index = layer.load();
  26. },
  27. complete:function(){
  28. layer.close(index);
  29. },
  30. success: function(res) {
  31. layer.close(index);
  32. if (res.code === 0){
  33. let progress_index;
  34. const domain = res.data.domain;
  35. const key = res.data.key+'.'+file.name.split('.')[1];
  36. const token = res.data.token;
  37. const putExtra = {
  38. fname: '',
  39. params: {},
  40. mimeType: ["video/mp4"],
  41. };
  42. const config = {
  43. useCdnDomain: true, //使用cdn加速
  44. };
  45. const observable = qiniu.upload(file, key, token, putExtra, config);
  46. progress_index = layer.open({
  47. type: 1,
  48. title: '文件上传[总大小: '+(file.size/(1024*1024)).toFixed(2)+' MB]',
  49. content: layui.jquery('#progress'),
  50. area: '80%',
  51. cancel: function(index){
  52. layer.confirm('确定要取消上传吗?', function (index2) {
  53. layer.close(index);
  54. layer.close(index2);
  55. subscription.unsubscribe() // 上传取消
  56. });
  57. return false;
  58. }
  59. });
  60. var subscription = observable.subscribe({
  61. next: (result) => {
  62. console.log(result);
  63. let percent = result.total.percent;
  64. percent = Math.round(percent*100)/100;
  65. console.log(percent);
  66. layui.element.progress('progress-value', percent+'%')
  67. },
  68. error: () => {
  69. layer.msg('上传图片失败');
  70. layer.close('progress_index');
  71. },
  72. complete: (res) => {
  73. console.log(res);
  74. layer.close(progress_index);
  75. if (res.key){
  76. layui.jquery('#carousel-img2').attr('src', domain+res.key);
  77. layui.jquery('#carousel-img2').attr('type', file.type);
  78. layui.jquery('#img2').val(res.key);
  79. layui.jquery('#mimetype').val(file.type);
  80. layer.msg('上传成功');
  81. } else{
  82. layer.msg(res.msg);
  83. }
  84. },
  85. });
  86. } else{
  87. layer.msg(res.msg);
  88. }
  89. },
  90. error:function(){
  91. layer.close(index);
  92. layer.msg('请求失败');
  93. },
  94. });
  95. });
  96. }
  97. });
  98. });

监听输入框Enter事件

  1. layui.jquery('#search_key').on('keydown', function (event) {
  2. if (event.key === 'Enter') {
  3. search();
  4. return false
  5. }
  6. });

layuiajax写法

  1. var index;
  2. layui.jquery.ajax({
  3. type: "POST",
  4. url: '{:url("/backend/institution/imgs")}',
  5. data: {id: id},
  6. dataType: "json",
  7. beforeSend: function () {
  8. index = layer.load();
  9. },
  10. complete: function () {
  11. layer.close(index);
  12. },
  13. success: function (res) {
  14. if (res.code === 404){
  15. layer.msg(res.msg);
  16. return false;
  17. }
  18. layer.photos({
  19. photos: res
  20. , anim: 5
  21. });
  22. },
  23. error: function () {
  24. layer.close(index);
  25. layer.msg('请求失败');
  26. },
  27. });