bootstrap-table-filter-control extension doesn't work in bootstrap-table
Asked Answered
A

2

7

I use bootstrap-table and would like to use table-filter-control extension. In this example you can see how to use this extension. When I want to use this extension for more columns, it doesn't work. In my example filter works only for one column.

jsfiddle

html

<table ref="mainTable" class="table table-striped table-bordered table-hover" 
       cellSpacing="0" id="mainTable" data-show-toggle="true" 
       data-show-columns="true" data-search="true" data-pagination="true" data-filter-control="true">
    <thead>
        <tr>
            <th data-field="state" data-checkbox="true"></th>
            <th data-field="Customer Name" data-sortable="true" data-filter-control="select">Customer Name</th>
            <th data-field="Location Type" data-sortable="true">Location Type</th>
            <th data-field="Location" data-sortable="true" data-filter-control="select">Location</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td>Cap Corp</td>
            <td>Main</td>
            <td>Norwalk CT 06851</td>
        </tr>
        <tr>
            <td></td>
            <td>Cap Corp</td>
            <td>Other</td>
            <td>Norwalk CT 06851</td>
        </tr>
        <tr>
            <td></td>
            <td>Tel</td>
            <td>Main</td>
            <td>Slough SL1 4DX</td>
        </tr>
        <tr>
            <td></td>
            <td>Tel</td>
            <td>Other</td>
            <td>London W1B 5HQ</td>
        </tr>
    </tbody>
</table>
Alidis answered 10/9, 2015 at 12:45 Comment(0)
C
4

data-filed should have not spaces, try to change

data-field="Customer Name"

to

data-field="CustomerName"

I updated your jsfiddle and filter-control works.

http://jsfiddle.net/5h595r6g/9/

However it would be great to implement filter options updating to available values, as I described in this post:

bootstrap table filter-control - how to unset unnecessary values from select options

Contexture answered 14/10, 2015 at 8:58 Comment(0)
M
2

I actually put select2 boxes in the headers, and then used the params feature to pass the code to the server. I made for a much nicer solution. My code isn't with me but if you are interested in it I can pass a sample along Monday.

Edited to add example.

Basic Table

        <table id='90day'  class='table table-striped' data-filter-control='true'>
            <thead>
                <tr>
                    <th></th>
                    <th><select id='findfield' class='form-control gosearch'><option></option></select></th>
                    <th><select id='findwellname' class='form-control gosearch'><option></option></select></th>
                </tr>
            </thead>
        </table>        

Initialize the select2

        $('#90day').on('post-header.bs.table', function () {
            $('#findfield').select2({
                width: '100%',
                placeholder: 'Field',
                allowClear: true,
                SingleSelection: true,
                ajax: {
                    url: 'selectfield90day.php?active=y',
                    dataType: 'json',
                    //delay: 250,
                    data: function (params) {
                        $('#findfield').empty();
                        var d = new Date();
                        var n = d.getTime();
                        return {
                            q: params.term,
                            n: n
                        };
                    },
                    processResults: function (data) {
                        return { results: data };
                    }
                }
            }); 
            $('#findwellname').select2({
                width: '100%',
                placeholder: 'Name',
                allowClear: true,
                ajax: {
                    url: 'selectwellname90day.php?active=y',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        $('#findwellname').empty();
                        var d = new Date();
                        var n = d.getTime();
                        return {
                            q: params.term,
                            field: $('#findfield').text(),
                            pad: $('#findpad').text(),
                            n: n
                        };
                    },
                    processResults: function (data) {
                        return {
                            results: data
                        };
                    }
                }
            });

            //refresh on any select2 change
            $('.gosearch').on('select2:close', function(){
                $('#90day').bootstrapTable('refresh');
            }); 
        });

Finally table initialization

$('#90day').bootstrapTable({
    url: ...,
    columns:[
        ...
    ],
    queryParams: function(params){
        params['field']=$('#findfield').text();
        params['well_name']=$('#findwellname').text();      
        return params;
    }
});
Mcvey answered 11/9, 2015 at 14:17 Comment(4)
Thank you, if you can pass me the sample it would be great :)Alidis
edited original comment with example code added. p.s. my example has bootstrap style form controls added with class='form-control'. Photo of end result can be seen here. (obviously only 2 select2 boxes added in example code). s24.photobucket.com/user/solowookie1791/media/…Mcvey
Thank you for your snippet. Very helpful. I keep this thread open, just in case someone will show up with another solution.Alidis
You're welcome. I took a bit to get it all worked out, but it works extremely well from a usability stand point. The end users love it.Mcvey

© 2022 - 2024 — McMap. All rights reserved.