React Native Apply Filter on JSON ListView Data Using PHP MySQL

Real time filter functionality is recommended for every dynamic react native application who wish to Filter the custom JSON ListView. So in this tutorial we would going to create a react native application with JSON data ListView and filter that data using PHP script. So let’s get started .

What we are doing in this project ( Project Description – Must Read ) : 

We are creating a react native app with 2 activities classes using React Navigation Library. First activity name is MainActivity and Second activity name is SecondActivity. The MainActivity class is used to show only the Name of students from JSON fetching from MySQL database. Now we would add the onPress event on ListView and get the selected ListView item ID. After that using the React Navigation inbuilt method this.props.navigation.navigate we would open the SecondActivity and send the Selected Item ID along with it. Now we would receive the Item ID using this.props.navigation.state.params in SecondActivitySecondActivity and run a real time web call to server using Fetch() method. It would send the Item ID on server and on server we are using the PHP script to Get ID and fetch only given ID record from MySQL database. After successfully fetching the only selected record it would return us in JSON form, which we would receive and show inside Text component.

Contents in this project Apply Filter on JSON ListView Data Using PHP MySQL Android iOS Tutorial :

1. Create a fresh MySQL database on your Local / Online hosting server.

2. Create a table inside your MySQL database named as Student_Info_Table with 5 columns id, name, semester, department, phone_number like i did in below screenshot.


3. After successfully creating the MySQL table insert some records inside it like i did  in below screenshot.

4. Create 3 PHP Script files :

DBConfig.php : The DBConfig.php file contains all the server information like Database Host Name, Database name, Database username and Database Password.

CollegeStudentsList.php : This file would fetch all the records from MySQL database and covert them into JSON.

Filter.php : This is used to filter the received ID from react native application and return the only given ID record object of single student.

Code for DBConfig.php file :

Code for CollegeStudentsList.php file :

Screenshot of JSON after running the CollegeStudentsList.php file on web browser :

Code for Filter.php file :

Now the server side coding part is complete . Next step is to Start the app coding .

5. Start a fresh React Native project. If you don’t know how then read my this tutorial.

6. Download and Install the React Navigation library inside your react native project by opening your project name folder into command prompt / terminal and execute the below command. The React  Navigation library is used to add multiple activities in react native project.

Screenshot after installing the library :

7. Open your project’s App.js file and import StyleSheet, Text, View, ListView and ActivityIndicator component.

8. Import the Stack Navigator module from react navigation library.

9. Create the first class named as MainActivity that extends the component. This would be our First main activity class.

10. Create navigationOptions inside MainActivity. The navigationOptions is used to set the activity Title which would show inside the Action Title bar of application.

11. Now create constructor inside MainActivity and inside constructor make a State named as isLoading. This State is used to show and hide the ActivityIndicator component.

12. Create a function named as OpenSecondActivity() and pass id as argument inside it. This function is used to Open SecondActivity and with it we would also send the selected id to next activity using ListViewClickItemHolder object.

13. Create componentDidMount() method. This is a pre define inbuilt method, which would call automatically on application starting time. Now define the fetch() web api inside componentDidMount() method. This would parse the complete JSON.

14. Create a function named as ListViewItemSeparator(). This is used to show a divider line between each ListView item in MainActivity.

15. Set a IF condition inside render’s block and using the State check and hide the ActivityIndicator after done loading JSON data.

16. Create a ListView component inside render’s return block in MainActivity . We are only showing the Student name from JSON data.

dataSource : Set the JSON ListView data source.

renderSeparator : Call the ListViewItemSeparator() method which would display item separator line.

renderRow : Render the Row data from Data source.

onPress : Call the OpenSecondActivity() function and pass the ID inside it using argument.

17. Complete Source code for MainActivity class :

Screenshot of MainActivity :

Filter18. Now create the SecondActivity Class. This would be our Filter record class.

19. Set activity Title using navigationOptions .

20. Create constructor and make 5 state variables named as IdHolder, NameHolder, SemesterHolder, DepartmentHolder and PhoneNumberHolder.

21. Create componentDidMount() method inside SecondActivity and call the Fetch() Web API. Now receive the sent id using this.props.navigation.state.params.ListViewClickItemHolder and pass it with id object. Now after come the response into responseJson array, we would pop up the records and store into each State .

22. Create 5 Text component inside render’s return block. Each component is used to display a Filtered Record.

23. Complete Source code for SecondActivity Class :

Screenshot of Second Activity with record :

24. Now create the StackNavigator method which would tell the project about present classes and which class is call first. This step is very important.

25. Create Custom Style sheet for all the Views and Text.

26Complete source code for App.js File :

Screenshots in android mobile phone application :

Filter

Screenshot in iOS mobile application :