How to delete json element using Jaggery?

271 Views Asked by At

Im kind a new to jaggery(http://jaggeryjs.org/) and I'm implementing a sample site using this technology. I have the following file in my file directory

employees.json (note: ignore the validation of the data)

{
    "employees": [
        {
            "id": "1",
            "name": "2",
            "age": "3",
            "town": "4",
            "salary": "5"
        },
        {
            "id": "3",
            "name": "fd jf",
            "age": "dfkd jf",
            "town": " jdks fj",
            "salary": " fjkdf "
        },
        {
            "id": "1",
            "name": "2",
            "age": "3",
            "town": "4",
            "salary": "5"
        }
    ]
}

I displayed this data using html table below and i included function to delete the json element within that row itself.

index.jag(note: this is a part of that code)

    <head>
<link rel="stylesheet" type="text/css" href="style.css" />
<%
var json = require("employees.json");
%>
</head>
<body>
 <h1>Employee Details</h1>
 <%
 include ("new_employee.jag");
 %>
 <h2>List all Employees</h2>
 <table "employeesTable" border="2">
  <tr>
   <th>Emplyee ID</th>
   <th>Name</th>
   <th>Age</th>
   <th>Town</th>
   <th>Salary</th>
  </tr>
  <%
  for (var i in json.employees){
%>
  <tr>
   <td><%=json.employees[i].id%></td>
   <td><%=json.employees[i].name%></td>
   <td><%=json.employees[i].age%></td>
   <td><%=json.employees[i].town%></td>
   <td><%=json.employees[i].salary%></td>
       <td><%delete employees[i]%></td>
      </tr>
      <%
     }

     %>

     </table>

as you can see my delete function is wrong because i could find any resource document that guid me to delete json element i.

Can some help me to delete this json element using Jaggery.

4

There are 4 best solutions below

0
On

If you want to do what harsha89 has mentioned, you could make an ajax call to jag script on the server and do the deletion and update the table asynchronously with callback function(assuming the table is getting populated from server side source) if you need to just remove table element on client side JQuery would be easy.

0
On

try

<%delete json.employees[i]%>

this will do the trick

0
On

If you want to delete the 1st element, then try it.

example -:

<%delete json.employees[o]%> //delete the first element element

Then print array and see.

0
On

As I understood, you want to have a link such that you need to delete the entire row right? This is not the way to do this. What you need to do is, have a link with a listener for that link using jQuery. When the delete link clicked, you have to remove that row from the table using JQuery. I think using employee id as row id would be ideal for delete that row using JQuery. Refer [1] and [2].

[1]-delete table row using jQuery

[2]-remove table row with specific id