fivem scripting NUI I'm using Datatable I want to create a table with the columns Date,…
- User
fivem scripting NUI
I'm using Datatable
I want to create a table with the columns Date, Agent, Buyer, Price, Adress
- Assistant
fivem scripting NUI
I'm using Datatable
I want to create a table with the columns Date, Agent, Buyer, Price, Adress
To create a table with the mentioned columns using DataTable and NUI in FiveM, follow these steps:
You can use the DataTable library to incorporate advanced tables in your script. First, download it from https://datatables.net/ and place the files into the directory where you store your NUI assets, like 'html' folder.
Create a new HTML file in the same folder where you put the DataTable files, such as 'table.html', and add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Table</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="css/dataTables.bootstrap4.min.css"> <!-- Data table CSS -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="js/jquery.dataTables.min.js"></script> <!-- Data table JS -->
<script src="js/dataTables.bootstrap4.min.js"></script> <!-- Data table Bootstrap JS -->
<style>
body {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<div class="container my-5">
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Date</th>
<th>Agent</th>
<th>Buyer</th>
<th>Price</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- Add table data here -->
</tbody>
</table>
</div>
<script>
$(document).ready(function() {
$('#example').DataTable();
});
</script>
</body>
</html>
Inside the <tbody> element, add your table data using <tr> and <td> tags, like this:
<tbody>
<tr>
<td>01/01/2022</td>
<td>John Doe</td>
<td>Jane Smith</td>
<td>$350,000</td>
<td>123 Main St, Los Santos</td>
</tr>
<!-- Add more rows here -->
</tbody>
Add the following code to your FiveM script (Lua) to show the HTML file in-game:
local isOpen = false
function openTableNui()
if not isOpen then
SetNuiFocus(true, true)
SendNUIMessage({
action = 'open'
})
isOpen = true
end
end
function closeTableNui()
if isOpen then
SetNuiFocus(false, false)
SendNUIMessage({
action = 'close'
})
isOpen = false
end
end
RegisterNUICallback('close', function()
closeTableNui()
end)
RegisterCommand('tablet', function()
openTableNui()
end, false)
Replace 'open' and 'close' in the SendNUIMessage function with the names of JavaScript functions responsible for opening and closing the table UI in the 'table.html' file.
Add the following JavaScript functions inside the <script> tag of the "table.html" file to handle the communication between the NUI and Lua script:
window.onload = function() {
window.addEventListener('message', function(event) {
var data = event.data;
if (data.action === 'open') {
document.body.style.display = 'block';
} else if (data.action === 'close') {
document.body.style.display = 'none';
}
});
}
function closeTable() {
$.post('http://localhost/close', JSON.stringify({}));
}
Not yet included in a published release.