From 7ad7452e09483c7faa1e6b09658e46e1a12170bb Mon Sep 17 00:00:00 2001
From: wtt <1985833171@qq.com>
Date: 星期六, 18 一月 2025 14:36:01 +0800
Subject: [PATCH] tableselect输入框输入搜索添加防抖

---
 WebTM/layuiadmin/modules/tableSelect.js |   35 +++++++++++++++++++++++++++++------
 1 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/WebTM/layuiadmin/modules/tableSelect.js b/WebTM/layuiadmin/modules/tableSelect.js
index d0a2659..2939f83 100644
--- a/WebTM/layuiadmin/modules/tableSelect.js
+++ b/WebTM/layuiadmin/modules/tableSelect.js
@@ -10,7 +10,7 @@
         table = layui.table,
         form = layui.form;
     var tableSelect = function () {
-        this.v = '1.3.0';
+        this.v = '1.4.0';
     };
 
     /**
@@ -20,7 +20,7 @@
         var elem = $(opt.elem);
 		
         //榛樿璁剧疆
-        opt.searchKey = opt.searchKey || 'keyword';
+        opt.searchKey = opt.searchKey || 'keyword';//鍙涓敤锛屽垎鍓�
         opt.table.page = opt.table.page || false;
         opt.table.height = opt.table.height || 290;
         elem.off('click').on('click', function(e) {
@@ -167,13 +167,36 @@
                 }
                 
             })
+            // 闃叉姈鍑芥暟
+            function debounce(func, wait) {
+                let timeout;
+                return function () {
+                    const context = this, args = arguments;
+                    clearTimeout(timeout);
+                    timeout = setTimeout(() => func.apply(context, args), wait);
+                };
+            }
+
+
             //杈撳叆妗嗚緭鍏ユ墽琛�
-            elem.off('input').on('input', function (e) {
+            elem.off('input').on('input', debounce(
+                function (e) {
                 // 闃绘琛ㄥ崟鎻愪氦锛堝鏋滆緭鍏ユ鍦ㄨ〃鍗曞唴锛�
                 e.preventDefault();
+                var searchKeyList = opt.searchKey.split(',');//鑾峰彇鏌ユ壘鐨勫叧閿瓧鎼滅储妗�
+                //鎷兼帴sql瀛楃涓�
+                var sWhereStr = " and ("
+                searchKeyList.forEach(function (value, index) {
+                    if (index == 0) {
+                        sWhereStr += (value + " like '%" + elem.val() + "%' ")
+                    } else {
+                        sWhereStr += ("or " + value + " like '%" + elem.val() + "%' ")
+                    }
+                })
+                sWhereStr +=")"
                 //鍚庣鍒嗛〉鎼滅储
                 if (opt.table.where != undefined) {
-                    opt.table.where['sWhere'] = opt.sWhere + " and " + opt.searchKey + " like '%" + elem.val() + "%' ";
+                    opt.table.where['sWhere'] = opt.sWhere + sWhereStr;
                     tableSelect_table.reload({
                         where: opt.table.where,
                         page: {
@@ -181,13 +204,13 @@
                         }
                     });
                 } else {
-                    var sWhere = opt.sWhere + " and " + opt.searchKey + " like '%" + elem.val() + "%' ";
+                    var sWhere = opt.sWhere + sWhereStr;
                     tableSelect_table.reload({
                         data: opt.ajaxSelect(sWhere)
                     });
                 }
                 
-            })
+            },200))
 
             //鍙屽嚮琛岄�変腑
             table.on('rowDouble('+tableName+')', function(obj){

--
Gitblit v1.9.1