I'm working on an application where data coming from the server (JSON) is partly displayed in a table. The user can select some rows, which are then added to another section with more detailed data.
Here is a sample of such JSON:
Sample 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
}
I populate the table from this, with some information displayed in the table columns, but also storing the whole record's data as JSON in the value
attribute of each row's checkbox.
So, now I select a row using the checkbox from the table, and click on the 'Save' button which would then append the data to a static div
with class="container"
.
However, if a particular div
with the same weburl
(as shown in the code) already exists inside the display div.container
, it shouldn't add that a second time.
Select A & D from the table shown in the screenshot and click on 'Save'.
A & D get appended to
div.container
.Now, select A,D,G,J from the table.
Current situation: Nothing is added, and alert is shown as "Data already exists!"
Ideal behaviour: G & J should get appended to div.box
and alert should be shown as "A already exists!".
I've tried to store all the urls from the div.parent
in an array. And similarly, had stored the url of each JSON row selected from the table in a variable.
Next, I'm trying to compare if the tableURL (the URL of the selected row) exists in the DOM, then do not add it.
But, somehow it doesn't work! How do I achieve this?
Note: URL(weburl) is going to be unique here. Hence, taking that for comparison.
Here is the HTML with the table already populated (but not the value
attributes of the input
elements, which would have URL-encoded 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>
Edit:
Lets say in the table, I'd want the following:
- checkbox to select the particular row.
- title (lets say Google for e.g.) which is a hyperlink with href = path.
- creation date.
Now, in the content to be displayed(like we did earlier), after I select a row, lets say I want to display : title(with a hyperlink to path), location, image.