Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs with DataTable custom search box

I am working with AngularJs+DataTable library, and I wish to create a custom control that can apply a exactly search function from DataTable, but with custom UI and control. However, the serch() return 0 length result which no consist any string value and the draw() isn't call properly.

I have follow some similar question on github, article and implement with $scope.dtInstance.DataTable.search(...).draw(); but turn out, it wouldn't working, so below is what I try, but same result. Any suggestion?

Here is my HTML implementation

<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>

Here is the angular controller

 $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();
};
like image 958
Kira Hao Avatar asked Dec 18 '22 20:12

Kira Hao


1 Answers

finally, I found out this part of implementation work for me, share it out if anyone also face same issues.

$scope.dtInstance = {};

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

    $scope.dtInstance.DataTable.search($scope.searchText).draw();
};
like image 159
Kira Hao Avatar answered Dec 21 '22 10:12

Kira Hao