当前位置: 首页 > 工具软件 > Tabler > 使用案例 >

layui table.reload 的坑

冯宪
2023-12-01

table.reload()

参考:https://www.cnblogs.com/yanan7890/p/11818902.html

当使用 table.reload() 多次对 table 进行重载时候,它默认会带有上一次 where{} 的参数条件。

 // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    //将搜索的表单数据封装进去result
                    var result = data.field;
                    console.log(result);

                    //执行搜索重载
                    table.reload('currentTableId', {
                        url: url
                        ,page: {
                            curr: 1
                        }
                        , where: {
                        	//默认会带有上一次的条件
                        }
                    }, 'data');
                    table.render();
                    return false;
                });

解决方法

加入 done:function(),在查询结束后 重新给where条件赋值为{}

 // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    //将搜索的表单数据封装进去result
                    var result = data.field;
                    console.log(result);

                    //执行搜索重载
                    table.reload('currentTableId', {
                        url: url
                        ,page: {
                            curr: 1
                        }
                        , where: result
                        ,done: function () {
                            this.where={};
                        }
                    }, 'data');
                    table.render();
                    return false;
                });

后续

过了几天我发现这个方法有个bug,就是你清除参数之后影响layui的分页,会导致翻页之后没有任何参数,直接返回全部分页

转自:https://blog.csdn.net/qq_41193133/article/details/106555244?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase


	//搜索
	var book_tb_this;
	form.on('submit(search_btn)', function(data) {
		if (book_tb_this != null) {
			book_tb_this.where = {};  //置空where
		}
		book_tb.reload({   //book_tb是table的实例
			url: '/book/search',
			where: data.field,
			page: {
				curr: 1
			},
			done: function() {
				book_tb_this = this;
			}
		});
		return false;
	});
 类似资料: