layui中彈出層的兩種表達方式
阿新 • • 發佈:2018-11-29
方式一:
定義js中定義html變數
方式二:
設定div :hidden:hidden 佈局
資料表格自適應大小:
程式碼:
<style> .btn-container { margin-bottom: 10px; } .layui-table-cell .layui-form-checkbox[lay-skin=primary] { top: 5px; } </style> <div class="layui-fluid"> <div class="layui-card"> <div class="layui-card-header">xx記錄</div> <div class="layui-card-body"> <div class="btn-container layui-form"> <!-- 組別搜尋 --> <div class="btn-container"> <button class="layui-btn layui-btn-normal"onclick="add()">新增xx記錄</button> </div> <table id="table" lay-filter="table" lay-data="{id: table}"></table> </div> </div> </div> </div> <div class="layui-card-body" id="record-user" hidden="hidden"> <div class="layui-form layui-form-pane" lay-filter = 'datafile'> <div class="layui-form-item"> <label class="layui-form-label">xx賬號</label> <div class="layui-input-inline"> <select name="xxxx" lay-verify="xxx"> <option value="">請選擇xx賬號</option> {foreach $xxx as $key => $vo} <option value="{$vo.id}">{$vo.xxxx}</option> {/foreach} </select> </div> </div> <div class="layui-form-item" > <label class="layui-form-label">xx資訊</label> <div class="layui-input-inline"> <input type="text" name="xxx" required placeholder="請輸入xx資訊" class="layui-input"> </div> </div> </div> </div> <script> layui.use(['table', 'form'], function() { var table = layui.table; var form = layui.form; var tableIns = table.render({ elem: "#table", url: "{:url('xxx')}", page: true,
//這是重點
cellMinWidth:70, done: function(res) { }, cols: [[ // {type: 'checkbox', fixed: 'left'}, {field: 'id', title: 'ID', sort: true, align: 'center'}, {field: 'xx', title: 'xx', sort: true, align: 'center'}, {field: 'xx', title: 'xx', sort: true, align: 'center'}, {field: 'xx', title: 'xx', sort: true, align: 'center'}, {title: '操作', align: 'center', sort: true, templet: '#operation', fixed: 'right'} ]] }); // 監聽工具欄 table.on('tool(table)', function(o) { var d = o.data, lEv = o.event, tr = o.tr if (parseInt('{$xx_power}') != 0) { layer.msg('您沒有許可權操作', { icon: 5, time: 1200 }); return false; } if (lEv == 'detail') pop(d); if (lEv == 'deleted') deleted(d); }) // 型別搜尋 form.on('select(type)', function(data) { search(tableIns); }); $(document).on('click','#seach',function(){ nameseach(tableIns); }); }); // 新增 var add = function() { var title = 'xx記錄新增' layer.open({ type:1, title: title, content: $('#record-user'), btn:['確定','取消'], area:['350px','35%'], yes:function(res){ // 判斷輸入框是否為空 if ($('[name=xxx]').val().length == 0) { layer.alert('不能存在空的輸入框', { icon: 5 }); return ; } var d = { xx: $('[name=xx]').val(), xx: $('[name=xx]').val(), } var u = '{:url("xx")}' layer.load(2); $.post(u, d, function(res) { // 清除彈出層 layer.closeAll() if (res.code == 0) { layer.msg('新增成功', {icon: 6, time: 1500}, function() { window.location.reload() }) return ; } layer.msg('新增失敗', {icon: 5, time: 1500}) }) } }); } // 修改 var pop = function(data) { var title ='xx編輯' var html = '<div class="layui-form">' html += '<div class="layui-form-item" >' html += '<label class="layui-form-label">xx賬戶</label>' html += '<div class="layui-input-block">' html += '<input type="text" name="xx" required placeholder="xxxxx" class="layui-input" disabled>' html += '</div>' html += '</div>' html += '<div class="layui-form-item">' html += '<label class="layui-form-label">xxx</label>' html += '<div class="layui-input-inline">' html += '<input type="text" name="xx" required placeholder="xxxx" class="layui-input">' html += '</div>' html += '</div>' html += '</div>' layer.open({ title: title, content: html, yes: function(res) { // 判斷輸入框是否為空 if ($('[name=xx]').val().length == 0) { layer.alert('不能存在空的輸入框', { icon: 5 }); return ; } var d = { xxx: $('[name=xxx]').val(), } var u = '{:url("xxx")}' d.id = data.id layer.load(2); $.post(u, d, function(res) { // 清除彈出層 layer.closeAll() if (res.code == 0) { layer.msg('修改成功', {icon: 6, time: 1500}, function() { window.location.reload() }) return ; } layer.msg('修改失敗', {icon: 5, time: 1500}) }) } }); // 設定資料 if (data != null) { $('[name=xxxx]').val(data.xxx) $('[name=xxx]').val(data.xxx) } } // 刪除 var deleted = function(data) { layer.confirm('確認刪除該資料?', function() { layer.closeAll() $.ajax({ url : "{:url('xxx')}", type : "POST", data : { 'xx' : data.xx, }, dataType : 'json' , success : function(res){ console.log(res); if(res.code == 0){ layer.msg("刪除成功", {icon: 6}, function() { location.reload(); }); } else{ layer.msg("刪除失敗", {icon: 5}); } } }); }); } </script> <script type="text/html" id="operation"> <a class="layui-btn layui-btn-xs" lay-event="detail">編輯</a> <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="deleted">刪除</a> </script>