Home > Article > Backend Development > How to use PHP and Vue to design data display for employee attendance system
How to use PHP and Vue to design the data display of employee attendance system
Introduction:
In modern enterprises, the attendance system is a very important part and can be effective Manage employee attendance effectively and improve work efficiency. With the help of PHP and Vue, we can design a simple and efficient employee attendance system, and intuitively display the employee's attendance status through the data display function. This article will introduce how to use PHP and Vue to design the data display of the employee attendance system, and provide specific code examples.
1. Build the environment
To use PHP and Vue to design the data display of the employee attendance system, first we need to build the corresponding environment. Please follow the steps below:
Installing Vue CLI
Vue CLI is a scaffolding tool used to quickly build Vue projects. You can use npm (Node.js package manager) to install it. Enter the following command in a terminal or command prompt to install the Vue CLI:
npm install -g @vue/cli
Create a Vue project
In the command prompt, switch to the directory where you wish to create the project, And enter the following command to create a new Vue project:
vue create employee-attendance-system
2. Write the front-end code
Next, we will write the Vue front-end code to display employee attendance data. Please follow the steps below:
Modify App.vue
Open the App.vue file in the src directory and modify it as follows:
<template> <div> <h1>员工考勤系统</h1> <table> <thead> <tr> <th>员工姓名</th> <th>日期</th> <th>上班时间</th> <th>下班时间</th> </tr> </thead> <tbody> <tr v-for="record in records" :key="record.id"> <td>{{ record.name }}</td> <td>{{ record.date }}</td> <td>{{ record.startTime }}</td> <td>{{ record.endTime }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { records: [], }; }, mounted() { this.fetchRecords(); }, methods: { fetchRecords() { // 使用axios发送GET请求到后端的api.php脚本 axios.get('api.php') .then(response => { this.records = response.data; }) .catch(error => { console.error(error); }); }, }, }; </script>
Modify main.js
Open the main.js file in the src directory and modify it as follows:
import Vue from 'vue'; import App from './App.vue'; Vue.config.productionTip = false; new Vue({ render: h => h(App), }).$mount('#app');
3. Write the back-end code
Now, we will write the back-end PHP code to handle the requests sent from the Vue front-end and return the corresponding data. Please follow the steps below:
Write api.php
Open the api.php file in the root directory and modify it as follows:
<?php // 模拟员工考勤数据 $records = [ [ 'id' => 1, 'name' => '员工1', 'date' => '2020-01-01', 'startTime' => '09:00:00', 'endTime' => '18:00:00', ], [ 'id' => 2, 'name' => '员工2', 'date' => '2020-01-02', 'startTime' => '08:30:00', 'endTime' => '17:30:00', ], ]; // 设置响应头,允许跨域访问 header('Access-Control-Allow-Origin: *'); header('Content-Type: application/json'); // 将员工考勤数据以JSON格式返回给前端 echo json_encode($records);
Run the front-end and back-end code
In the command prompt, switch to the root directory of your project and enter the following command to start the front-end and back-end code:
npm run serve
This will start A local development server and listening on http://localhost:8080. You can access this address in your browser to see the data display page of the employee attendance system.
Conclusion:
Through the above steps, we successfully designed a simple and efficient employee attendance system using PHP and Vue, and implemented the data display function. Of course, this is just a basic example and you can extend and improve the code according to your own needs. I hope this article is helpful to you, and I wish you success in designing data display for employee attendance systems using PHP and Vue!
Note: The above code examples are for demonstration purposes only. There may be some security or performance issues. Please make appropriate improvements and optimizations according to your needs in actual applications.
The above is the detailed content of How to use PHP and Vue to design data display for employee attendance system. For more information, please follow other related articles on the PHP Chinese website!