admin管理员组文章数量:1330427
I'm using angular datatable plugin, and I want to get the value of the search box that es with the generated datatable. Angular datatable has a few options to make a datatable, but any of them allow me specify attributes or something that I could watch with the purpose to get a particular value.
Since I need to get the value of the input search of the datatable, I can't find a way to acplish my purpose.
So, how can I get the search box value in angular datatable?
I'm using angular datatable plugin, and I want to get the value of the search box that es with the generated datatable. Angular datatable has a few options to make a datatable, but any of them allow me specify attributes or something that I could watch with the purpose to get a particular value.
Since I need to get the value of the input search of the datatable, I can't find a way to acplish my purpose.
So, how can I get the search box value in angular datatable?
Share Improve this question edited Feb 1, 2016 at 14:41 davidkonrad 85.6k17 gold badges209 silver badges271 bronze badges asked Jan 18, 2016 at 17:52 fablexisfablexis 5787 silver badges20 bronze badges2 Answers
Reset to default 5Unfortunetaly you cannot $watch
the search term / filter. Angular dataTables is directives that makes jQuery dataTables runnable in angular without DOM conflicts etc, but the internals still works as always - in a pletely none-angular way :)
However, dataTables broadcasts a search
(.dt
) event each time a search / filter is performed, and then you can extract the value of the search directly from the DOM :
angular.element('body').on('search.dt', function() {
var searchTerm = document.querySelector('.dataTables_filter input').value;
console.log('dataTables search : ' + searchTerm);
})
This is of course the most simple jQuery-like approach. You may have more than one dataTable on the page and want to extract more detailed information for each search, you then can use in your angular app :
angular.element('body').on('search.dt', function(e, api) {
if (!$scope.$$phase) {
$scope.$apply(function() {
$scope.searchTerm = api.oPreviousSearch;
$scope.searchTerm.table = e.target.id;
})
}
})
$apply
in order to update $scope
from inside the event handler. oPreviousSearch
is in fact the current search, so the above produces a $watch
'able searchTerm
on the form
{
"bCaseInsensitive": true,
"sSearch": "test",
"bRegex": false,
"bSmart": true,
"_hungarianMap": {
"caseInsensitive": "bCaseInsensitive",
"search": "sSearch",
"regex": "bRegex",
"smart": "bSmart"
},
"table": "tableId"
}
see demo -> http://plnkr.co/edit/E5Tr7FrLRIVTtguQHFx9?p=preview
You can get the actual search value with the search() function (with no arguments).
To get it updated make sure you:
Have a dtInstance and a dtOptions object:
<table datatable dt-instance="vm.dtInstance" dt-options="vm.dtOptions">
You bind a callback to "draw" event in your dtOptions:
this.dtOptions = { drawCallback: this.myCallbackFunction }
Then in your function you can do:
this.myCallbackFunction = () => {
let mySearchValue = this.dtInstance.DataTable.search()
}
本文标签: javascripthow to get the search box value in angular datatableStack Overflow
版权声明:本文标题:javascript - how to get the search box value in angular datatable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742247238a2440081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论