【发布时间】:2017-04-06 04:59:21
【问题描述】:
我正在开发一个应用程序,其中来自服务器 (JSON) 的数据部分显示在表格中。用户可以选择一些行,然后将其添加到具有更详细数据的另一个部分。
以下是此类 JSON 的示例:
示例 JSON:
{
"error": false,
"pageIndex": "0",
"resultset": [{
"title": "Google",
"CreationDate": "Mar 14, 2017 4:06:00 PM",
"location": "USA",
"path": "www.google.com",
"image": "www.google.com/images/logo.jpg"
}, {
"title": "Global",
"CreationDate": "Mar 14, 2017 4:06:00 PM",
"location": "Global",
"path": "www.abc.com",
"image": "www.abc.com/images/logo.jpg"
}, {
"title": "Yahoo",
"CreationDate": "Mar 14, 2017 4:06:00 PM",
"location": "Europe",
"path": "www.yahoo.com",
"image": "www.yahoo.com/images/logo.jpg"
}, {
"title": "Amazon",
"CreationDate": "Mar 14, 2017 4:06:00 PM",
"location": "Europe",
"path": "www.amazon.com",
"image": "www.amazon.com/images/logo.jpg"
}, {
"title": "XYZ",
"CreationDate": "Mar 14, 2017 4:06:00 PM",
"location": "Europe",
"path": "www.xyz.com",
"image": "www.xyz.com/images/logo.jpg"
}
}],
"totalMatches": 35
}
我由此填充表格,在表格列中显示一些信息,但还将整个记录的数据作为 JSON 存储在每行复选框的 value 属性中。
所以,现在我使用表格中的复选框选择一行,然后单击“保存”按钮,然后将数据附加到带有class="container" 的静态div。
但是,如果具有相同weburl(如代码所示)的特定div 已存在于显示div.container 中,则不应再次添加它。
从屏幕截图中显示的表格中选择 A & D,然后点击“保存”。
A & D 被附加到
div.container。现在,从表中选择 A、D、G、J。
当前情况:没有添加任何内容,并且alert显示为“数据已存在!”
理想行为:G & J 应该附加到div.box 并且警报应该显示为“A 已经存在!”。
我尝试将div.parent 中的所有网址存储在一个数组中。同样,将从表中选择的每个 JSON 行的 url 存储在一个变量中。
接下来,我想比较一下tableURL(选中行的URL)是否存在于DOM中,那就不要添加了。
但是,不知何故,它不起作用!我如何做到这一点?
注意:URL(weburl) 在这里是唯一的。因此,拿它来做比较。
这是已填充表格的 HTML(但不是 input 元素的 value 属性,它们将具有 URL 编码的 JSON):
<!doctype html>
<html>
<head>
<style>
table, th, td {
border: 1px solid #ddd;
border-collapse: collapse;
padding: 10px;
}
.parent {
height: 25%;
width: 90%;
padding: 1%;
margin-left: 1%;
margin-top: 1%;
border: 1px solid black;
}
.parent:nth-child(odd){
background: skyblue;
}
.parent:nth-child(even){
background: green;
}
</style>
</head>
<body>
<table>
<tr>
<th>Select</th>
<th>Name</th>
<th>Website</th>
</tr>
<tr>
<td><input type="checkbox" class="selectRow"></td>
<td class="name">A</td>
<td class="weburl"><a href="abc.com">ABC</a></td>
</tr>
<tr>
<td><input type="checkbox" class="selectRow"></td>
<td class="name">D</td>
<td class="weburl"><a href="def.com">DEF</a></td>
</tr>
<tr>
<td><input type="checkbox" class="selectRow"></td>
<td class="name">G</td>
<td class="weburl"><a href="ghi.com">GHI</a></td>
</tr>
<tr>
<td><input type="checkbox" class="selectRow"></td>
<td class="name">J</td>
<td class="weburl"><a href="jkl.com">JKL</a></td>
</tr>
</table>
<button onclick="saveData()">Save</button>
<br />
<div class="container">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
var content = "";
dispUrl = [];
function saveData(){
$('input[name="selectRow"]:checked').map(function(count) {
var myJSON = JSON.parse(decodeURIComponent(this.value));
var tableDataUrl = myJSON.weburl;
content += '<div class="parent"><label class="dataLabel">Name:</label>'+myJSON.name+'<label class="dataLabel">Website:</label><a href="'+myJSON.weburl+'" class="myLink"></div>';
$('.parent .myLink').each(function() {
dispUrl.push($(this).attr('href'));
});
//console.log(linkArr);
var isRendered = $.inArray(tableDataUrl, dispUrl);
//alert(isRendered);
if(!isRendered) {
$('.container').html(content);
resetEvents();
} else {
alert("Div already added to the result list!");
}
}
}
</script>
</body>
</html>
编辑:
让我们在表格中说,我想要以下内容:
- 用于选择特定行的复选框。
- 标题(例如 Google),它是一个带有 href = path 的超链接。
- 创建日期。
现在,在要显示的内容中(就像我们之前所做的那样),在我选择一行之后,假设我要显示:标题(带有指向路径的超链接)、位置、图像。
【问题讨论】:
标签: javascript jquery arrays dom dynamic