How to search special character in an angular smart table?
Last Updated :
15 Sep, 2020
Introduction on Smart table:
It is an Angularjs module used for display purpose as a table format with a set of features like filtering, sorting, etc., This is very much helpful during report preparation, filtering and searching of data in a quicker manner in angular js. It is lightweight, developer-friendly, robust, modular, and extensible
Approaches:
- We can start using smart table by using the below ways
- bower install angular-smart-table (or)
- npm install angular-smart-table
- Once the above scripts are executed, we are set in using angular-smart-table
Syntax:
- Add the module angular.module('<nameofyourapp>', ['smart-table'] to the angular application.
- Just like following normal html table structure, on the table element, need to add the "st-table" attribute to inform smart-table about the collection that will display the data, (i.e., using repeater)
- For Searching a content in angular-smart-table, we need to use, stSearch directive. It can be a comma-separated list of search items that need to be searched
- stDebounceTime attribute (the value is in millisecond) can be used in order to control the searching time. Sometimes useless calls will be made (when especially connected to the server) and in order to overcome, this attribute is needed.
- During search, the input can be a regular expression pattern. Sometimes, in order to escape regexp specific characters in the input, we need to use stSearchEscape attribute.
Basic Examples and Explanations:
Let us see the sample code of searching in the smart table along with filtering and pagination is seen in below code
Data is retrieved from "http://coderszine.com/demo/rest-api/v1/employee/read" for our sample
stSafeSrc attribute:
As we have taken data from the restful endpoint (even from a remote database, restful endpoint, ajax call, etc)
we need to use this attribute without fail. Moreover smart table creates a copy of the displayed collection and since asynchronous data is involved, this attribute is a must.
Here "employees" retrieved as asynchronously and need to be rendered and it is specified in st-safe-src
sort is applied on all columns and hence st-sort is given for sorting.
pagination is applied to have 5 records per page.
Input :
With the above approaches, let us render the data in a smart table with search, sort, and pagination functionalities
javascript
SampleApp = angular.module(
'SampleApp', ['SampleApp.controllers', 'smart-table']);
angular.module('SampleApp.controllers', []).controller(
'sampleController',
['$scope', '$http', function($scope, $http)
{
$scope.loading = false;
$scope.getData = function() {
$scope.loading = true;
$http.get(
"http://coderszine.com/demo/rest-api/v1/employee/read")
.then(function(response){
$scope.employees = response.data;
$scope.loading = false;
});
}
$scope.getData();
}]);
HTML:
html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />
<link rel="stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" />
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/angular-smart-table/2.1.8/smart-table.min.js">
</script>
<link rel="stylesheet"
href=
"https://fonts.googleapis.com/css?family=Open+Sans" />
<!-- Indication to know we are
using smartTableApp.js -->
<script src="smartTableApp.js">
</script>
</head>
<body>
<!-- Including our SampleApp and
iterate in Angular JS -->
<div class="container"
ng-app="SampleApp"
ng-controller="sampleController">
<h2>Angular Smart Table Example with
Pagination, Search and Sorting in
a simpler way</h2>
<div ng-show="loading"><h3>Loading the data...</h3>
</div>
<table st-table="displayEmployee"
st-safe-src="employees"
class="table table-striped">
<thead>
<tr>
<th colspan="1">
<input st-search placeholder=
"Please provide data to search"
class="input-sm form-control"
type="search" />
</th>
</tr>
<tr>
<th st-sort="name">Employee Name</th>
<th st-sort="gender">Gender</th>
<th st-sort="age">Employee Age</th>
<th st-sort="skills">Skills</th>
<th st-sort="designation">
Employee Designation</th>
</tr>
</thead>
<tbody>
<tr st-select-row="row"
st-select-mode="multiple"
ng-repeat="employee in displayEmployee">
<td>{{employee.name}}</td>
<td>{{employee.gender}}</td>
<td>{{employee.age}}</td>
<td>{{employee.skills}}</td>
<td>{{employee.designation}}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" class="text-center">
<div st-pagination=""
st-items-by-page="5"></div>
</td>
</tr>
</tfoot>
</table>
</div>
</body>
</html>
Output:
After Searching a text
Sorting based on Designation
Similar Reads
How to Add Special Characters in URL in Angular?
When developing web applications with Angular, you might encounter situations where you need to include special characters in URLs. Special characters such as spaces, ampersands, question marks, and others often need to be encoded correctly to ensure the URL is valid and functions as expected. In th
4 min read
How to print an array in table format using angularJS?
Given an array & the task is to print the given array in the tabular format using AngularJS. In JavaScript, data can be stored in the form of arrays. Each of the array items has unique indexing, starting from 0. But what if the developer wants to display all the items that are in the array, on t
4 min read
How To Set Width Of mat-table Column In Angular?
In Angular Material, the mat-table component is a powerful data table tool that allows you to display data in a structured and flexible way. However, when it comes to customizing the appearance of the table, especially the width of individual columns, you may need to apply custom styling techniques.
4 min read
How To Set Width Of mat-table Column In Angular?
In Angular Material, the mat-table component is a powerful data table tool that allows you to display data in a structured and flexible way. However, when it comes to customizing the appearance of the table, especially the width of individual columns, you may need to apply custom styling techniques.
4 min read
How to replace a string by another string in AngularJS ?
In this article, we will see how to select a string from the text and replace it with another string using AngularJS and will understand it through the illustrations. The replace() method can be used to search the particular character(s), which is getting replaced with a specific character(s) &
2 min read
How to display static JSON data in table in Angular ?
In this article, we will see How to display static JSON data in the table in Angular. We will be displaying static data objects in Angular Table. We need to iterate over the object keys and values using key values and then handle them in the table. We will be using the bootstrap table and Angular Pr
3 min read
Angular PrimeNG Table Separate Row and Filter Menu
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more.
4 min read
How to create a custom pipe in AngularJS ?
In this article, we will learn how to generate custom pipe other than using built-in pipe in Angular & also explore its implementation. The Pipe is a function that is used to modify the data before rendering it to the user. Some of the pre-built pipes are date, uppercase, lowercase, currency, de
6 min read
How to Sort an Array of Objects by String Property value in Angular ?
An Object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In this article, we will learn how to Sort an array of objects by string property value in Angular. He
3 min read
How to pass input to a custom directive in Angular JS?
In AngularJS, passing input to a custom directive allows the customization of components within an application. Directives can receive input via attributes, expressions, or controller functions, allowing for dynamic behavior and data binding. In this article, we will explore two different approaches
3 min read