在JavaScript中的右列中对表单元格进行排序
我认为,理解这个问题会变得晦涩难懂,但请尝试描述它。 //i.sstatic.net/olhlr.png“ alt =“在此处输入图像说明”>
如果您瞥了一眼桌子,则包含名称的单元格(nazarbek,toshkent,toshkent,qo'qon ...)不在正确的列中(列还具有包括单元格的名称)。我想像“纳扎贝克”列中的那样对它们进行排序,仅包括名为“ nazarbek”,“ raximova” =&gt的单元格; “ raximova”等。没有带有列的通用名称的单元格应保持空(例如第一行中的最后2个单元格,第二行中的3个单元格)。
PS:尝试使用Node.js中的EJS文件中的MongoDB数据库实现数据。
我希望你明白。
下面有有意义或提供任何提示的代码,您不必纠正我的代码。任何实现我想要的方法的方法都被接受。
<%- include('partials/header') -%>
<% console.log("-----//-----//-----//-----//-----//-----") %>
<table id="mainTable" width='100%' class="table table-bordered table-hover table-striped table-sm">
<tr>
<th></th>
<% allAddresses.forEach((gtem) => { %>
<th scope="col" style='writing-mode: vertical-rl; transform: rotate(180deg)'><%= gtem %></th>
<% }) %>
</tr>
<% for (let item = 0; item < address.length; item++){ %>
<% let stockIdentifier = address[item].rollingStockNumber.slice(11, 22) %>
<% let arrayOfAddresses = _.values(address[item].addresses).sort() %>
<tr>
<th align="center"><%- stockIdentifier.slice(0, 4) + " " + stockIdentifier.slice(4, 6) + " " + stockIdentifier.slice(6, 10) + " " + stockIdentifier.slice(10, 11) %></th>
<% var current = null; %>
<% var count = 0; %>
<% for (var index = 0; index < arrayOfAddresses.length; index++){ %>
<% if (arrayOfAddresses[index] != current){ %>
<% if (count > 0){ %>
<td><%= count + " " + current %></td>
<% allAddresses.forEach((address) => { %>
<% if (current === address){ %>
<% let cell = allAddresses.indexOf(address) %>
<% console.log("The first part is " + (cell + 1)) %>
<% } %>
<% }) %>
<% } %>
<% current = arrayOfAddresses[index] %>
<% count = 1 %>
<% } else { %>
<% count++ %>
<% } %>
<% } %>
<% if (count > 0){ %>
<td><%= count + " " + current %></td>
<% allAddresses.forEach((address) => { %>
<% if (current === address){ %>
<% let cell = allAddresses.indexOf(address) %>
<% console.log("The second part " + (cell + 1)) %>
<% } %>
<% }) %>
<% } %>
</tr>
<% } %>
</table>
<form action="/indexCalculated.html" method="post">
<button class="btn btn-dark" type="submit" name="backToMainPage" style="margin: 15px 10px 40px 742px; display: inline-block">Back</button>
</form>
<%- include('partials/footer') -%>
在app.js中:
app.post('/indexReplaced.html', (req, res) => {
Address.find({}, function(err, item){
Address.aggregate([
{
$group: {
_id: null,
addresses: {
$push: "$addresses"
}
}
},
{
$project: {
allAddresses: {
$reduce: {
input: "$addresses",
initialValue: [],
in: {
"$concatArrays": [
"$$value",
"$$this"
]
}
}
}
}
}
], function (err, addresses){
function onlyUnique(value, index, self) {
return self.indexOf(value) === index
};
let unique = addresses[0]["allAddresses"].filter(onlyUnique)
// console.log(unique);
res.render("indexCalculated", {
address: item,
allAddresses: unique.sort()
});
});
})
});
任何支持都非常感谢。
I think, this question will be obscure to understand, but try to describe it.
If you take a glance on a table, cells that contain names (Nazarbek, Toshkent, Qo'qon ...) aren't in right columns (Columns also have names that includes cells' names). I wanted to sort them like in "Nazarbek" column should include only cells that named "Nazarbek", "Raximova" => "Raximova" etc. Cells that no common names with column should stay empty (Like the last 2 cells in the first row, 3 cells in the second row).
P.S: Trying to implement data from the MongoDb database in EJS file using Node.js.
I hope you understood.
There is code below for making sense or providing any hints, you don't have to correct my code. Any methods of achieving what I wanted is accepted.
<%- include('partials/header') -%>
<% console.log("-----//-----//-----//-----//-----//-----") %>
<table id="mainTable" width='100%' class="table table-bordered table-hover table-striped table-sm">
<tr>
<th></th>
<% allAddresses.forEach((gtem) => { %>
<th scope="col" style='writing-mode: vertical-rl; transform: rotate(180deg)'><%= gtem %></th>
<% }) %>
</tr>
<% for (let item = 0; item < address.length; item++){ %>
<% let stockIdentifier = address[item].rollingStockNumber.slice(11, 22) %>
<% let arrayOfAddresses = _.values(address[item].addresses).sort() %>
<tr>
<th align="center"><%- stockIdentifier.slice(0, 4) + " " + stockIdentifier.slice(4, 6) + " " + stockIdentifier.slice(6, 10) + " " + stockIdentifier.slice(10, 11) %></th>
<% var current = null; %>
<% var count = 0; %>
<% for (var index = 0; index < arrayOfAddresses.length; index++){ %>
<% if (arrayOfAddresses[index] != current){ %>
<% if (count > 0){ %>
<td><%= count + " " + current %></td>
<% allAddresses.forEach((address) => { %>
<% if (current === address){ %>
<% let cell = allAddresses.indexOf(address) %>
<% console.log("The first part is " + (cell + 1)) %>
<% } %>
<% }) %>
<% } %>
<% current = arrayOfAddresses[index] %>
<% count = 1 %>
<% } else { %>
<% count++ %>
<% } %>
<% } %>
<% if (count > 0){ %>
<td><%= count + " " + current %></td>
<% allAddresses.forEach((address) => { %>
<% if (current === address){ %>
<% let cell = allAddresses.indexOf(address) %>
<% console.log("The second part " + (cell + 1)) %>
<% } %>
<% }) %>
<% } %>
</tr>
<% } %>
</table>
<form action="/indexCalculated.html" method="post">
<button class="btn btn-dark" type="submit" name="backToMainPage" style="margin: 15px 10px 40px 742px; display: inline-block">Back</button>
</form>
<%- include('partials/footer') -%>
In app.js:
app.post('/indexReplaced.html', (req, res) => {
Address.find({}, function(err, item){
Address.aggregate([
{
$group: {
_id: null,
addresses: {
$push: "$addresses"
}
}
},
{
$project: {
allAddresses: {
$reduce: {
input: "$addresses",
initialValue: [],
in: {
"$concatArrays": [
"$value",
"$this"
]
}
}
}
}
}
], function (err, addresses){
function onlyUnique(value, index, self) {
return self.indexOf(value) === index
};
let unique = addresses[0]["allAddresses"].filter(onlyUnique)
// console.log(unique);
res.render("indexCalculated", {
address: item,
allAddresses: unique.sort()
});
});
})
});
Any support is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论