1、网上的代码:
<div> 搜索商户: <div> <input autocomplete="off"> </div> <button data-type="reload">搜索</button></div> 在js加入初始化代码和定义加载方法 layui.use('table', function(){ var table = layui.table; //方法级渲染 table.render({ elem: '#LAY_table_user' ,url: 'UVServlet' ,cols: [[ {checkbox: true, fixed: true} ,{field:'id', title: 'ID', width:80, sort: true, fixed: true} ,{field:'aid', title: '商户', width:80, sort: true} ,{field:'uv', title: '访问量', width:80, sort: true,edit:true} ,{field:'date', title: '日期', width:180} ,{field:'datatype', title: '日期类型', width:100} ]] ,id: 'testReload' ,page: true ,height: 600 }); var $ = layui.$, active = { reload: function(){ var demoReload = $('#demoReload'); table.reload('testReload', { where: { keyword: demoReload.val() } }); } }; )};
绑定click点击事件
$('.demoTable .layui-btn').on('click', function(){ var type = $(this).data('type'); //注意顶一个括号中的格式,class用.id用#,中间有一个空格 active[type] ? active[type].call(this) : ''; });
此时点击查询按钮,会将keyword这个关键字传到后端,接下来就是自己处理查询关键字业务了。
到目前为止,搜索也有了,分页也有了,对了,分页会自动传到后端page,limit2个值到后台,相当于(pageindex,pagesize)
2、实践,
(1)html页面及javascript代码如下:
<!--index.html内容如下--> {% extends 'common.html'%} {% block content %} <div> <form> <div> <label>手机号</label> <input placeholder="手机号"> </div> <div> <label>身份证</label> <input placeholder="身份证"> </div> <div> <label>姓名</label> <input placeholder="姓名"> </div> <div> <label>crfuid</label> <input placeholder="crfuid"> </div> </form> </div> <div> <button>扩展功能</button> <button>开始查询</button> <button>额度查询</button> <button>会员查询</button> <button>账户查询</button> <button οnclick="return false;" data-type="reload">短信验证码查询</button> <div lay-accordion> <div> <h2>扩展功能</h2> <div> {% for business in common_business %} <button data-type="{{business.business_code}}" data-extra="{{business.business_is_extra}}">{{business.business_name}}</button> {% endfor %} </div> </div> </div> </div> <div> <div> <h2>日志</h2> <div><textarea placeholder=""></textarea></div> </div> <div> <h2>附加信息</h2> <div> </div> </div> </div> <table lay-filter="user"></table> <script type="text/javascript" src="https://www.jb51.net/static/lib/layui/layui.js" charset="utf-8"></script> <script> layui.use('table', function(){ var table = layui.table; //方法级渲染 table.render({ elem: '#table_message' ,url: '/sms/message' ,method:'post' ,where: { phone: '', env_tools:parent.window.env_tools } ,cellMinWidth: 100 ,cols: [[ {checkbox: true, fixed: true} ,{field:'id', title: 'ID', sort: true,width:120, fixed: true} ,{field:'phone', title: '手机号',width:120} ,{field:'content', title: '短信内容', sort: true} ,{field:'create_time', title: '发送时间',width:160} ]] ,id: 'testReload' ,page: true ,height: 500 }); var $ = layui.$, active = { reload: function(){ var demoReload = $('#phone'); $('.env_message').html('当前操作环境:'+ parent.window.env_tools); //执行重载 table.reload('testReload', { page: { curr: 1 //重新从第 1 页开始 } ,where: { phone: demoReload.val(), env_tools:parent.window.env_tools } }); } }; $('.weadmin-block #query_sms').on('click', function(){ var type = $(this).data('type'); active[type] ? active[type].call(this) : ''; }); }); </script> {% endblock %}
(2)后台代码如下:
@toolsbp.route('https://www.jb51.net/') @toolsbp.route('/index',methods=['GET','POST']) def index(): if request.method=='POST': data={} data['data']='' data['code']=0 data['msg']='查询成功' env=request.form.get('env_tools') if env is None: return json.dumps(data) phone=request.form.get('phone') where='' if phone: where="where phone like '%(phone)s'"%{'phone':'%'+phone+'%'} dbinfo=env_config.ENV_LIST.get(env.upper()).get('sms_platform') dbtool=MysqlPool.MysqlPool(dbinfo.get('db_name')) result=dbtool.getAll("select * from %(table_name)s.sms_send_record %(where)s order by create_time desc limit %(page)d,%(pagesize)d;"%{'where':where,'table_name':dbinfo.get('table_name'),'page':int(request.form.get('page'))-1,'pagesize':int(request.form.get('limit'))}) data['data']=result if result: data['count']=len(result) else: data['count']=0 return json.dumps(data,cls=MysqlPool.DateEncoder) return render_template('tools/index.html')
(3)页面功能如下:
以上这篇layui数据表格重载实现往后台传参就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章: