How to achieve result highlighting effect for DataTables search box query
青灯夜游
Release: 2018-10-19 16:09:04
forward
2891 people have browsed it
The content of this article is to introduce how to achieve the result highlighting effect of DataTables search box query. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
DataTables is an encapsulated HTML table plug-in that enriches the style of HTML tables and provides a variety of advanced table functions such as instant search and paging. Users can write very little code (even just use the official sample code) and make a beautiful table to display data. For more information about DataTables, please see: http://www.datatables.club/, https://datatables.net/. The following figure will show the relevant data of Nanjing tourist attractions travel notes, displayed in the DataTables table.
The instant search, paging and other functions in the above DataTable table are created It exists after the DataTables object, so there is no need to write related code. "Instant search" means that as the characters entered change, changing matching information will appear in the table.
However, DataTables itself does not provide the function of highlighting search results. You need to introduce relevant JavaScript files and write relevant codes. DataTables Chinese website provides this js file, but in the example there is one less statement to set the style, so the highlighting function cannot be realized. http://www.datatables.club/blog/2014/10/22/search-result-highlighting.html
##1. DataTables related code
1. Code skeleton
Need to be introduced to use DataTables table jQuery; the example uses the online DataTables CDN.
<script></script>tag to DataTable Make relevant settings. No other styles are set here, only the data source of the table is configured. DataTables tables support multiple data sources, JavaScript object arrays, data returned by ajax, json format data, etc. Here, the data in the Excel table is stored in the "Nanjing Travel Notes.js" file in the form of an object array (each element in the array is an object, that is, a piece of travel record information), and then introduced in src in the HTML page where DataTables is located (" There is only one JavaScript object array in the Nanjing Attractions.js" file). To configure the data source in this way, you need to set the columns attribute in the DataTable constructor. Note that this should correspond to the Table header information. For other methods of DataTables style setting and data source configuration, please check the relevant content in the official documentation: https://datatables.net/examples/index.
DataTables Chinese website provides a method of highlighting (http://www.datatables.club/blog/2014/10/22/search-result-highlighting.html). The provided js file can be implemented Highlighting function, but you need to add the