bootstrap table onSearch搜索事件

当bootstrap table开启搜索输入框,用户输入关键词时的事件,返回text关键词字符串。

onSearch事件

搜索时的事件,返回参数如下。

参数名称说明
text用户输入的关键词

绑定onSearch事件方法一

$('#table').bootstrapTable({
    columns: columns, //列对象
    data: data, //要显示的数据对象
    onSearch : function(text) { //搜索查询事件
       alert(text);  
    }
});

绑定onSearch事件方法二

$('#table').on('search.bs.table', function (e,text) {
      alert(text);
});

完整代码

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="https://www.itxst.com/package/bootstrap-table-1.14.1/jquery-3.3.1/jquery.js"></script>
    <link href="https://www.itxst.com/package/bootstrap-table-1.14.1/bootstrap-4.3.1/css/bootstrap.css" rel="stylesheet" />
    <link href="https://www.itxst.com/package/bootstrap-table-1.14.1/bootstrap-table-1.14.1/bootstrap-table.css" rel="stylesheet" />
    <script src="https://www.itxst.com/package/bootstrap-table-1.14.1/bootstrap-table-1.14.1/bootstrap-table.js"></script>
    <title>bootstrap table onSearch搜索事件例子</title>
    <style>
        .table-demo {
            width: 80%;
            margin: 30px auto 0px auto;
        }
        .fixed-table-header {
            border-right: solid 1px #ddd;
            border-top: solid 1px #ddd;
        }
            .fixed-table-header table  {
                border-top: solid 0px #ddd !important;
                margin-top:-1px;
            }
    </style>
</head>
<body >
    <div class="table-demo">
        <table id="table"></table>
    </div>
    <script>
        //设置需要显示的列
        var columns = [{
            field: 'Id',
            title: '编号'
        }, {
            field: 'ProductName',
            title: '名称'
        }, {
            field: 'StockNum',
            title: '库存'
        }];
 
//bootstrap table初始化数据
$('#table').bootstrapTable({
  columns: columns,
  data: getData(),
  classes: "table table-bordered table-striped table-sm table-dark", 
  height:400,
  search:true, //******开启搜索框****
  searchOnEnterKey:false, //******回车后执行搜索****
  onSearch:function(text)
  {
  alert(text);
  }
 });


  function getData()
  {
            var data = [];
            for (var i = 0; i < 200; i++)
            {
                var item = {
                    Id: i,
                    ProductName: '苹果',
                    StockNum: '200'
                };
                data.push(item);
            };
            return data;
  }
    </script>
</body>
</html>

在线试一试