AngularJs与DataTable自定义搜索框

时间:2016-01-18 03:03:12

标签: javascript jquery angularjs datatable

我正在使用AngularJs + DataTable库,我希望创建一个自定义控件,可以从DataTable应用精确的搜索功能,但具有自定义UI和控件。但是,serch()返回0长度结果,其中不包含任何字符串值,并且draw()未正确调用。

我在github,文章和实施$scope.dtInstance.DataTable.search(...).draw();上关注了一些类似的问题 但事实证明,它不起作用,所以下面是我尝试,但同样的结果。 有什么建议吗?

这是我的HTML实现

<button class="btn btn-white btn-sm" type="button" 
data-toggle="collapse" data-target="#collapseSearch" 
aria-expanded="false"    
aria-controls="collapseSearch">
<i class="fa fa-search"></i> Search
</button>

<div class="collapse" id="collapseSearch">
                        <div class="row margin-top-20px">
                            <div class="col-sm-12 margin-bottom-5px">
                                <div class="input-group bookingRecordDataTable_filter dataTables_filter">
                                    <span class="input-group-addon input-addon-green">Search</span>
                                    <input type="search" class="form-control" 
                                    ng-model="searchText" 
                                      ng-change="searchTable()" 
                                      placeholder="search" 
                                      aria-controls="bookingRecordDataTable">
                                </div>
                            </div>
                        </div>
                    </div>

<table datatable="ng"
class="table table-hover"
dt-options="dtOptions"
dt-column-defs="dtColumnDefs" id="bookingRecordDataTable"
 dt-instance="dtInstanceCallback">
</table>

这是角度控制器

 $scope.dtOptions = DTOptionsBuilder.newOptions()
    .withOption('bInfo', false)
    .withOption('bFilter', false)
    .withOption('bAutoWidth', false)
    .withOption('bLengthChange', false)
    .withDOM("<'col-sm-12't><'col-sm-12'p>")
    .withOption('order', [0, 'desc'])
    .withBootstrap();
$scope.dtColumnDefs = [
    DTColumnDefBuilder.newColumnDef(0).withTitle('Id').notVisible(),
  ...
];
$scope.dtInstanceCallback = function(dtInstance)
{
    var datatableObj = dtInstance;
    $scope.tableInstance = datatableObj;
}
$scope.searchTable = function ()
{
    console.log($scope.tableInstance);
    var query = $scope.searchText;
    console.log(query);
    var result = $scope.tableInstance.DataTable.search(query, false, false, false);
    console.log(result);
    $scope.tableInstance.DataTable.search(query, false, false, true).draw();
};

1 个答案:

答案 0 :(得分:10)

最后,我发现这部分实施工作对我来说,如果有人也面临同样的问题,请分享。

$scope.dtInstance = {};

$scope.searchTable = function ()
{
    $scope.dtInstance.DataTable.search($scope.searchText);

    $scope.dtInstance.DataTable.search($scope.searchText).draw();
};