with the Bootstrap Table plugin, how to set a search string on first load?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 07:13:57

问题


With the Bootstrap Table plugin (http://bootstrap-table.wenzhixin.net.cn/documentation/) how to set a search string when the table is loaded for the first time?

Exists only a "data-search" attribute to enable the search input, but this doesn't valorize it.

I looked for a "data-search-text" attribute but I've not found it.


回答1:


There isn't support for data attribute but only via JavaScript

bootstrapTable({
    formatSearch: function () {
        return 'search string';
    }
});



回答2:


A better solution is this one:

var srch_str = 'your search string';
var bstable_first_load = true;
table.on('post-body.bs.table', function () {//when the table is loaded the first time and visible in the DOM, set the search string...
    if(srch_str.length > 0 && bstable_first_load){$('.fixed-table-toolbar').find('.search > :input').val(srch_str);bstable_first_load = false};
});



回答3:


I needed such a behavior too. My first attempts were to use bootstrapTable('resetSearch', 'my-query') with a fixed timeout; sadly this was yielding two requests (and additional reflows if the first query made it to the client) so it was sub-optimal.

After a while I found that the goal can be achieved modifying query params wisely (i.e. for the first request only):

var start_query = '20';

$('#table').bootstrapTable({
     queryParams: function(params){
        if(start_query){
            $('.bootstrap-table').find('.search input').val(start_query)
            params['search'] = start_query
            start_query = null
        }
        return params
    }
});

Interestingly the $input.val() call does not fire any additional callbacks so the code above uses only one query/request.

Please see a working demo here: http://jsfiddle.net/e3nk137y/15364/

Edit: The preferred way to do implement such behavior probably should use searchText option; sadly it also issues two requests. I created an issue in bootstrap-table repo.

Edit 2: A patch for two-request behaviour of searchText has been merged into the development branch of the Bootstrap Table plugin and should be a part of the next release.



来源:https://stackoverflow.com/questions/28028857/with-the-bootstrap-table-plugin-how-to-set-a-search-string-on-first-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!