使用ajax服务器端进行数据表过滤不起作用

时间:2019-01-29 18:41:00

标签: javascript ajax filter datatables

我正在从服务器端填充表,并且想对列实现过滤器。

我创建了一个下拉菜单,该菜单调用我的javascript类,但未过滤,始终显示所有记录。

因此,我的目标是在下拉菜单中选择一个选项,然后单击过滤表。

 $(".company_link").on("click", function (e) {
    companies_type = $(this).data('value');
    $("#company_text").html(companies_type);
    clients_types_table.ajax.reload();
});

clients_types_table = $('.clients_types_table').DataTable({
    processing: true,
    serverSide: true,
    columns: [
        {data: 'id', width: '5%', searchable: true,}, //0
        {data: 'origin_id',searchable: true}, //1
        {data: 'description',searchable: true}, //1
        {data: 'company_id', width: 80, searchable: true}, //6
        {data: 'id', width: 80, searchable: true, orderable: true}, //6
    ],
    language:{
        processing:""
    },
    ajax: {
        url: AjaxURL,
        data: function (d) {               
            d.action = "admin_clients_types";              
            d.option = "list";
            d.company_id = companies_type;  // I want to filter this column

        }
    },
    rowCallback: function (row, data, index) {
        var buttons = "<div class='center btn-group btn-group-sm'>";
        buttons += "<button type='button' data-id='" + data.id + "' data-ownerid='" + data.company_id + "' data-type='edit' title='Edit' data-placement='top' class='btn btn-sm bg-blue gridbutton'><i class='far fa-edit fa-fw' ></i></button>";
        buttons += "<button type='button' data-id='" + data.id + "' data-type='delete' title='Delete' data-placement='top' class='btn btn-sm bg-red gridbutton'><i class='fas fa-trash fa-fw fa-sm'></i></button>";
        buttons += "</div>";
        $('td:eq(-1)', row).html(buttons);
        $('td:eq(-1)', row).find('button').tooltip();
    }
});

这是我的表格代码

<div class="header">
                <h2>
                    MyClients Types
                    <button type="button" id="comp_button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                        <span id="company_text">AllanTeste</span> <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <?php $tbo = new TBAS(); $comps = $tbo->getCompaniesList(); foreach ($comps as $k=>$v){ ?>
                            <li><a id="<?=$v['value']?>" data-value="<?=$v['value']?>" class="company_link"><?=$v['name']?></a></li>
                        <?php } ?>
                    </ul>
                </h2>
                <ul class="header-dropdown m-r--5 m-t--5 m-b--5">
                    <li>
                        <button type="button" id="create_clients_type" class="btn btn-primary">
                            <i class="fas fa-plus"></i>
                            <span>Create MyClients Type</span>
                        </button>
                    </li>
                </ul>
            </div>
            <div class="body">
                <div class="row clearfix">
                    <div class="col-xs-12">
                        <div class="table-responsive">
                            <table class="table table-bordered table-striped table-hover clients_types_table dataTable">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Origin ID</th>
                                    <th>Description</th>
                                    <th>Company ID</th>
                                    <th>Tools</th>                                                                   
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>

                            </table>
                        </div>
                    </div>
                </div>
            </div>

这是我要进入数据库的功能。

public function getCompaniesList() {
    $companies_type = $this->dbm->from(TBLM_COMPANIES)->get()->result();
    $data=[];
    foreach ($companies_type as $k=>$v){
        $d = [
            'name'=>$companies_type[$k]['name'],
            'value'=>$companies_type[$k]['id']
        ];
        array_push($data,$d);
    }
    return $data;
}

有人可以帮助我吗?

0 个答案:

没有答案