Is it possible in javascript to check if multiple records in a table have certain identical fields, before saving?
Ex.
| article | size_type | adj_type | size_list | quant |
|---|---|---|---|---|
| 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 2 | 1 |
| 1 | 1 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 |
| 1 | 2 | 0 | 1 | 1 |
| 1 | 2 | 1 | 1 | 1 |
If the size_type field is equal to 1, the adj_type and / or size_list fields must be the same. An alert should appear stating “for a size_type there cannot be different add_type and size_list.
It's possible? Thank you.
The first thing is to pass the data from the table to a data structure. For example in the following example I show a form:
const data = {
article: [1, 1, 1, 1, 1, 1],
size_type: [1, 1, 1, 1, 2, 2],
adj_type: [0, 0, 1, 1, 0, 1],
size_list: [1, 2, 1, 1, 1, 1],
quant: [1, 1, 1, 1, 1, 1],
};
const incidents = [];
data.size_type.forEach((value, index) => {
if (value === 1 && data.adj_type[index] !== 1) {
incidents.push({ row: index, entry: "adj_type" });
}
if (value === 1 && data.size_list[index] !== 1) {
incidents.push({ row: index, entry: "size_list" });
}
});
console.log(incidents);
The result could fit better, but in this case it shows the index where the issue occurs and the name of the property where it occurs
Update: Regarding passing the table data to a data structure, you can try the following
const rows = document.querySelectorAll("table tbody tr");
const entries = Array.from(rows).map((row) => {
return Array.from(row.cells).map((cell) => +cell.textContent);
});
const data = {
article: entries[0],
size_type: entries[1],
adj_type: entries[2],
size_list: entries[3],
quant: entries[4],
};
console.log(data);
<table>
<thead>
<tr>
<th>article</th>
<th>size_type</th>
<th>adj_type</th>
<th>size_list</th>
<th>quant</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>0</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>