Skip to content

Commit e8e8203

Browse files
authored
Merge pull request #1 from jspreadsheet/refactor/update-v11
Update README and Code Compatibility for Jspreadsheet v11
2 parents 9fbc382 + 0c8ea4e commit e8e8203

File tree

6 files changed

+106
-50
lines changed

6 files changed

+106
-50
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
composer.lock
1+
composer.lock
2+
/vendor

README.md

+78-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,87 @@
1-
# Jspreadsheet spreadsheet
2-
## Database integration
1+
# Jspreadsheet Spreadsheet
32

4-
Persisting the spreadsheet data using a database.
3+
## Database Integration
54

6-
Step 1 - Download this project to a local machine, with docker installed.
5+
This guide explains how to persist jspreadsheet data using postgresql.
76

8-
Step 2 - On the project root folder: % docker-compose up
7+
### Step 1: Clone the Project
98

10-
Step 3 - In another terminal go to the root folder: % docker-compose exec php bash
9+
Download the project to your local machine. Ensure that [Docker](https://www.docker.com/) is installed and running.
1110

12-
Step 4 - On the PHP container terminal: % composer install
11+
### Step 2: Start Docker Containers
1312

14-
Step 5 - In another terminal go to the root folder: % docker-compose exec postgresql bash
13+
Navigate to the project’s root folder and run the following command to start the necessary Docker containers:
1514

16-
// Now create your own tables
15+
```bash
16+
docker-compose up
17+
```
1718

18-
Step 6 - Edit your public/index.php to sync with one table.
19+
### Step 3: Access the PHP Container
1920

20-
Step 7 - http://localhost:8081<br><br>
21+
Open a new terminal, navigate to the project’s root folder, and access the PHP container by running:
22+
23+
```bash
24+
docker-compose exec php bash
25+
```
26+
27+
### Step 4: Install Dependencies
28+
29+
Inside the PHP container terminal, install the required PHP dependencies using [Composer](https://getcomposer.org/):
30+
31+
```bash
32+
composer install
33+
```
34+
35+
### Step 5: Access the PostgreSQL Container
36+
37+
In a new terminal, navigate to the root folder and access the PostgreSQL container:
38+
39+
```bash
40+
docker-compose exec postgresql bash
41+
```
42+
43+
#### Step 5.1: Create Database and Table
44+
45+
Once inside the PostgreSQL container, follow these steps to create a `users` table:
46+
47+
1. Access PostgreSQL and create the `test` database:
48+
```bash
49+
psql -U postgres
50+
```
51+
52+
2. Create a new database:
53+
```sql
54+
CREATE DATABASE test;
55+
```
56+
57+
3. Connect to the `test` database:
58+
```sql
59+
\c test
60+
```
61+
62+
4. Create the `users` table:
63+
```sql
64+
CREATE TABLE users (
65+
user_id SERIAL PRIMARY KEY,
66+
username VARCHAR(50),
67+
email VARCHAR(100)
68+
);
69+
```
70+
71+
### Step 6: Update PHP Configuration
72+
73+
Verify that the table name in your `public/index.php` matches the database table you created. In this example, the table is `users`:
74+
75+
```php
76+
$jss = new \Jspreadsheetdb($database, 'users');
77+
```
78+
79+
### Step 7: Access the Spreadsheet
80+
81+
Open your browser and navigate to [http://localhost:8009](http://localhost:8009). Any updates made in the spreadsheet will now persist in the database table.
82+
83+
---
84+
85+
### Notes:
86+
- You can further customize the database structure depending on your needs.
87+
- Ensure that your Docker containers are running before attempting to access the site.

docker-compose.yml

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: "3.7"
21
services:
32
postgresql:
43
image: postgres:latest
@@ -18,35 +17,29 @@ services:
1817
image: dpage/pgadmin4
1918
restart: always
2019
environment:
21-
PGADMIN_DEFAULT_EMAIL: postgres
20+
PGADMIN_DEFAULT_EMAIL: postgres@postgres.com
2221
PGADMIN_DEFAULT_PASSWORD: postgres
2322
PGADMIN_LISTEN_PORT: 80
2423
ports:
2524
- "8035:80"
2625
volumes:
2726
- postgres_data:/var/lib/postgresql/data
28-
links:
27+
depends_on:
2928
- postgresql
3029

3130
redis:
3231
image: redis:alpine
3332
expose:
3433
- 6379
3534

36-
mailhog:
37-
image: mailhog/mailhog
38-
ports:
39-
- 1021:1025
40-
- 8021:8025
41-
4235
web:
4336
build: ./resources/docker/aws
4437
ports:
4538
- 8009:80
4639
- 8010:443
4740
volumes:
4841
- .:/var/www/html
49-
links:
42+
depends_on:
5043
- php
5144

5245
php:
@@ -55,14 +48,9 @@ services:
5548
- 9000
5649
volumes:
5750
- .:/var/www/html
58-
links:
59-
- redis
60-
- postgresql
61-
- mailhog
6251
depends_on:
6352
- redis
6453
- postgresql
65-
- mailhog
6654

6755
volumes:
6856
postgres_data:

public/Jspreadsheetdb.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function save($post)
4848
// Parse the posted json
4949
$request = json_decode($post['data'], true);
5050

51-
// Process the setValue request
52-
if (isset($request['setValue']) && $request['setValue']) {
51+
// Process the updateData request
52+
if (isset($request['updateData']) && $request['updateData']) {
5353
// Process all records
54-
foreach ($request['setValue'] as $v) {
54+
foreach ($request['updateData'] as $v) {
5555
// Verify if the record exists in the table
5656
$result = $this->database->table($this->table)
5757
->argument(1, $this->primaryKey, $v['id'])
@@ -74,11 +74,11 @@ public function save($post)
7474
'success' => 1,
7575
'message' => 'Updated',
7676
];
77-
} else if (isset($request['insertRow']) && $request['insertRow']) {
78-
// No, create a new record
79-
$this->database->column($v['data'], true)
80-
->insert()
81-
->execute();
77+
} else {
78+
return [
79+
'success' => 1,
80+
'message' => 'This action is not yet being persisted',
81+
];
8282
}
8383
}
8484

@@ -89,9 +89,8 @@ public function create($options)
8989
'columns' => [],
9090
'data' => [],
9191
'persistence' => '/',
92-
'defaultColWidth' => '100px',
93-
'tableOverflow' => true,
94-
'tableWidth' => '1200px'
92+
'defaultColWidth' => '200px',
93+
'minDimensions' => [3, 10]
9594
], $options);
9695

9796
// Get all column information
@@ -119,6 +118,7 @@ public function create($options)
119118
$column['primaryKey'] = true;
120119
$column['readOnly'] = true;
121120
$column['type'] = 'number';
121+
$column['width'] = '80px';
122122
}
123123

124124
$config['columns'][] = $column;
@@ -132,11 +132,11 @@ public function create($options)
132132
$options['id'] = 'j' . rand(1000,9000);
133133
}
134134

135-
// Create the Jexcel configuration
135+
// Create the Jspreadsheet configuration
136136
return "<div id='{$options['id']}'></div><script>jspreadsheet(document.getElementById('{$options['id']}'), { worksheets: [$config] });</script>";
137137
} else {
138138
// Table not found
139-
return 'Not found';
139+
return 'Table not found';
140140
}
141141
}
142142

public/index.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@
2020
]);
2121

2222
// Jspreadsheet license
23-
$license = 'YjQzMzdlOTRiOGY3ZTQ0ZDQ4ZTI1YWU3MDFjMDI0ZWJmOTNjODA1NWFiZTRiNDJhNmRiYTJlZjkwODQ3N2IwMWRmNWRjYWUwZDViM2VhMmI3NzVjOTcwMzVlN2ZhODI1Y2EyMmE3NDI0ZmE0ZjVmNGQ2MWEzN2M3MTA4MThhMDUsZXlKdVlXMWxJam9pY0dGMWJDNW9iMlJsYkNJc0ltUmhkR1VpT2pFMk16SXdPVEkwTURBc0ltUnZiV0ZwYmlJNld5SnFjMlpwWkdSc1pTNXVaWFFpTENKcVpYaGpaV3d1Ym1WMElpd2lZMjlrWlhOaGJtUmliM2d1YVc4aUxDSnFjMmhsYkd3dWJtVjBJaXdpTVROemQyMHVZM05pTG1Gd2NDSXNJbXh2WTJGc2FHOXpkQ0pkTENKd2JHRnVJam9pTXlKOQ==';
23+
$license = 'ZTk0NmUwMDYzMTk4NTY0ZDNiMjhhOGM5M2NlNjdhNDNiM2M2MjQ2YTkwZDZiNDMzMjQxYmJhOWY4MDBhODc4OTEzNTQyNGQ0MWUwODAwNWIzMDc2NjU2NzExMWUwYTMxZjMyYmJkODIzMzZiYjE5MjQ4Mjc1OTYzZDVkYjZjMzMsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnpJMU5UZ3pNVE0yTENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0ozWldJaUxDSnNiMk5oYkdodmMzUWlYU3dpY0d4aGJpSTZJak0wSWl3aWMyTnZjR1VpT2xzaWRqY2lMQ0oyT0NJc0luWTVJaXdpZGpFd0lpd2lkakV4SWl3aVkyaGhjblJ6SWl3aVptOXliWE1pTENKbWIzSnRkV3hoSWl3aWNHRnljMlZ5SWl3aWNtVnVaR1Z5SWl3aVkyOXRiV1Z1ZEhNaUxDSnBiWEJ2Y25SbGNpSXNJbUpoY2lJc0luWmhiR2xrWVhScGIyNXpJaXdpYzJWaGNtTm9JaXdpY0hKcGJuUWlMQ0p6YUdWbGRITWlMQ0pqYkdsbGJuUWlMQ0p6WlhKMlpYSWlMQ0p6YUdGd1pYTWlYU3dpWkdWdGJ5STZkSEoxWlgwPQ==';
2424

2525
// Jspreasdheet Db class - Updates the table users to whatever table name
2626
$jss = new \Jspreadsheetdb($database, 'users');
2727
// Define the primary key
2828
$jss->setPrimaryKey('user_id');
2929

3030
if (Render::isAjax()) {
31-
echo $jss->save($_POST);
31+
echo json_encode($jss->save($_POST));
3232
} else {
33-
echo '<html>
34-
<script src="https://jspreadsheet.com/v10/jspreadsheet.js"></script>
35-
<link rel="stylesheet" href="https://jspreadsheet.com/v10/jspreadsheet.css" type="text/css" />
36-
<script src="https://jsuites.net/v5/jsuites.js"></script>
37-
<link rel="stylesheet" href="https://jsuites.net/v5/jsuites.css" type="text/css" />
33+
echo "<html>
34+
<script src='https://jspreadsheet.com/v11/jspreadsheet.js'></script>
35+
<link rel='stylesheet' href='https://jspreadsheet.com/v11/jspreadsheet.css' type='text/css' />
36+
<script src='https://jsuites.net/v5/jsuites.js'></script>
37+
<link rel='stylesheet' href='https://jsuites.net/v5/jsuites.css' type='text/css' />
3838
<script>
39-
jspreadsheet.setLicense("Y2EzNjI4ODU4Y2Q5MTZmMWQwNWMwMWUwM2VlMzVkMzYwYjcyMWFjNTE4YzExYjgzMjlhYTc3MzBlOGM5NDQ1N2NmZTk2N2JjYzYxZGVkNDM2NjQxY2M4YTA2YzhhMDgyMGRlNTA3OTViMmU4ZGRkODY0MDljZmI0ODBjOTQ2NmUsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UY3dOekExTURRNE9Td2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklsMHNJbVJsYlc4aU9uUnlkV1Y5");
40-
</script>';
39+
jspreadsheet.setLicense(\"$license\");
40+
</script>";
4141

4242
echo $jss->create([]);
4343
}

resources/docker/aws/default.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ server {
22
listen 80;
33
client_max_body_size 100M;
44

5-
server_name ~^(.+)$
5+
server_name ~^(.+)$;
66
access_log /var/log/nginx/access.log;
77
error_log /var/log/nginx/error.log;
88

0 commit comments

Comments
 (0)