How to use Layui to implement drag-and-drop search and filtering functions
Background introduction:
With the rapid development of the Internet, the amount of data continues to grow, how to efficiently Filtering and searching data has become an important issue. Layui is a lightweight front-end UI framework with rich components and plug-ins that can easily implement various functions. This article will introduce how to use Layui to implement a drag-and-drop search and filter function to facilitate users to customize search conditions according to their own needs.
Implementation ideas:
Specific implementation:
HTML part:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>可拖拽的搜索筛选功能</title> <link rel="stylesheet" href="layui/css/layui.css"> </head> <body> <div class="layui-container"> <div class="layui-row"> <div id="searchBar" class="layui-col-md12"> <form class="layui-form" action=""> <div class="layui-form-item"> <label class="layui-form-label">关键词</label> <div class="layui-input-inline"> <input type="text" name="keyword" placeholder="请输入关键词" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">筛选条件</label> <div class="layui-input-inline"> <input type="text" name="filter" placeholder="请选择筛选条件" autocomplete="off" class="layui-input"> </div> <div class="layui-form-mid layui-word-aux">点击右侧按钮进行自定义筛选条件</div> </div> </form> </div> <div id="tableContainer" class="layui-col-md12"> <table id="dataTable" lay-filter="dataTable"></table> </div> </div> </div> <script src="layui/layui.js"></script> <script> layui.use(['layer', 'table', 'form'], function(){ var layer = layui.layer; var table = layui.table; var form = layui.form; // 监听自定义筛选条件按钮点击事件 $('#searchBar').on('click', 'button', function(){ layer.open({ type: 1, title: '自定义筛选条件', content: $('#filterForm'), area: ['600px', '400px'], success: function(){ // 渲染表单 form.render(); }, btn: ['确定', '取消'], yes: function(index, layero){ // 提交表单 form.on('submit(filterSubmit)', function(data){ // 获取筛选条件,并进行搜索操作 var filterData = data.field; table.reload('dataTable', { where: filterData }); // 关闭弹窗 layer.close(index); return false; }); } }); }); // 渲染表格 table.render({ elem: '#dataTable', url: 'data.json', cols: [[ {field: 'name', title: '姓名'}, {field: 'age', title: '年龄'}, {field: 'gender', title: '性别'} ]] }); }); </script> <!-- 自定义筛选条件弹窗 --> <script type="text/html" id="filterForm"> <form class="layui-form" lay-filter="filterForm"> <div class="layui-form-item"> <label class="layui-form-label">年龄段</label> <div class="layui-input-inline"> <input type="text" name="minAge" placeholder="最小年龄" autocomplete="off" class="layui-input"> </div> <div class="layui-form-mid">-</div> <div class="layui-input-inline"> <input type="text" name="maxAge" placeholder="最大年龄" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">性别</label> <div class="layui-input-block"> <input type="checkbox" name="gender" value="男" title="男"> <input type="checkbox" name="gender" value="女" title="女"> </div> </div> <div class="layui-form-item layui-hide"> <input type="button" lay-submit lay-filter="filterSubmit" id="filterSubmit"> </div> </form> </script> </body> </html>
JavaScript part:
layui.use(['layer', 'table', 'form'], function(){ var layer = layui.layer; var table = layui.table; var form = layui.form; // 监听自定义筛选条件按钮点击事件 $('#searchBar').on('click', 'button', function(){ layer.open({ type: 1, title: '自定义筛选条件', content: $('#filterForm'), area: ['600px', '400px'], success: function(){ // 渲染表单 form.render(); }, btn: ['确定', '取消'], yes: function(index, layero){ // 提交表单 form.on('submit(filterSubmit)', function(data){ // 获取筛选条件,并进行搜索操作 var filterData = data.field; table.reload('dataTable', { where: filterData }); // 关闭弹窗 layer.close(index); return false; }); } }); }); // 渲染表格 table.render({ elem: '#dataTable', url: 'data.json', cols: [[ {field: 'name', title: '姓名'}, {field: 'age', title: '年龄'}, {field: 'gender', title: '性别'} ]] }); });
Summary:
Using Layui components and plug-ins, We can easily implement drag-and-drop search and filter functions. Through pop-up windows and form components, users can customize search criteria. Combined with the table component, we can display data and dynamically search and filter based on the user's filtering conditions. Such a function can greatly improve users' search efficiency and provide a better user experience.
The above is the detailed content of How to use Layui to implement drag-and-drop search and filtering functions. For more information, please follow other related articles on the PHP Chinese website!