show details when user clicked selected button - javascript

i want to show the bill number when user click it from list,
i have already fetch the data as well but i dont know how can i add this url /api/sales_bill_info/{$bill_number} to show that bill number details.
$.each(resultData, function(index, row) {
// var editUrl = url+'/'+row.id+"/edit";
bodyData += "<tr style='font-size: 0.78em;'>"
bodyData += "<td>" + i++ + "</td>" +
"<td><a href=" {
{
url('/api/sales_bill_info/'. + row.bill_number + )
}
}
" class='btn btn-success btn-sm printbill' value='" + row.bill_number + "' style='margin-left:20px;'>PRINT</a></td>" +
"<td><button class='btn btn-sm btn-danger cancelbill' id='cancelbill' value='" + row.SID + "' style='margin-left:20px;'>CANCEL BILL" + row.SID + "</button></td>" +
"<td>" + row.bill_number + "</td>" +
"<td>" + row.compy_name + "</td>" +
"<td>" + row.truck_number + "/" + row.trailer_number + "</td>" +
"<td>" + row.Devicenumber + "</td>" +
"<td>" + row.slavename + "</td>" +
"<td>" + row.name + "</td>" +
"<td>" + row.tag_area + "</td>" +
"<td>" + row.Lname + "</td>" +
"<td>" + row.bill_number + "</td>" +
"<td>" + row.driverPhone + "</td>" +
"<td>" + row.created_at_sale + "</td>" +
"<td><button class='btn btn-danger delete' value='" + row.id + "' style='margin-left:20px;'>Delete</button></td>";
bodyData += "</tr>";
})
$("#bodyData").append(bodyData);
}
});
i tried to add this "<td><a href="{{url('/api/sales_bill_info/'.+row.bill_number+)}}" class='btn btn-success btn-sm printbill' value='"+row.bill_number+"' style='margin-left:20px;'>PRINT</a></td>"
but does not works.
someone can help me on this

You can try assigning the value of edit url to a variable and then use the same.
$.each(resultData, function(index, row) {
let editUrl = `/api/sales_bill_info/${row.bill_number}`;
// var editUrl = url+'/'+row.id+"/edit";
bodyData += "<tr style='font-size: 0.78em;'>"
bodyData += "<td>" + i++ + "</td>" +
"<td><a href=" + editUrl + "class='btn btn-success btn-sm printbill' value='" + row.bill_number + "' style='margin-left:20px;'>PRINT</a></td>" +
"<td><button class='btn btn-sm btn-danger cancelbill' id='cancelbill' value='" + row.SID + "' style='margin-left:20px;'>CANCEL BILL" + row.SID + "</button></td>" +
"<td>" + row.bill_number + "</td>" +
"<td>" + row.compy_name + "</td>" +
"<td>" + row.truck_number + "/" + row.trailer_number + "</td>" +
"<td>" + row.Devicenumber + "</td>" +
"<td>" + row.slavename + "</td>" +
"<td>" + row.name + "</td>" +
"<td>" + row.tag_area + "</td>" +
"<td>" + row.Lname + "</td>" +
"<td>" + row.bill_number + "</td>" +
"<td>" + row.driverPhone + "</td>" +
"<td>" + row.created_at_sale + "</td>" +
"<td><button class='btn btn-danger delete' value='" + row.id + "' style='margin-left:20px;'>Delete</button></td>";
bodyData += "</tr>";
});
$("#bodyData").append(bodyData);

Maybe it could be useful to consider template strings in JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
They can really simplify your code and avoid some problems like the ones with quotes or double quotes.
I hope this could help you! Happy coding πŸ˜€
const resultData = [{
bill_number: 1,
SID: 2,
compy_name: 'xxx',
truck_number: '54125',
Devicenumber: '123456',
slavename: 'fff',
trailer_number: 'yyy',
name: 'pluto',
tag_area: 'A51',
Lname: 'xxx',
bill_number: '1234',
driverPhone: '+0236851545',
created_at_sale: '2020-01-01',
}];
function url(path) {
return 'http://...'
}
$.each(resultData, function(i, row) {
$(`#bodyData`).append(
`<tr style='font-size: 0.78em;'>
<td>${i++}</td>
<td>
<a
href="${url('/api/sales_bill_info/'+row.bill_number)}}"
class='btn btn-success btn-sm printbill'
value='${row.bill_number}' style='margin-left:20px;'>
PRINT
</a>
</td>
<td>
<button
class='btn btn-sm btn-danger cancelbill'
id='cancelbill' value='${row.SID}'
style='margin-left:20px;'>
CANCEL BILL ${row.SID}
</button>
</td>
<td>${row.bill_number}</td>
<td>${row.compy_name}</td>
<td>${row.truck_number} ${row.trailer_number}</td>
<td>${row.Devicenumber}</td>
<td>${row.slavename}</td>
<td>${row.name}</td>
<td>${row.tag_area}</td>
<td>${row.Lname}</td>
<td>${row.bill_number}</td>
<td>${row.driverPhone}</td>
<td>${row.created_at_sale}</td>
<td>
<button
class='btn btn-danger delete'
value='${row.id}'
style='margin-left:20px;'>
Delete
</button>
</td>
</tr>`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id='bodyData'></table>

Related

How can I get text value from modal with javascript?

I'm fetching the values from my table in ajax.
$(data.mesIzinTanimList).each(function (index, element) {
if (element.izinTanimiVarmi == 'yok')
tr = "<tr class='bg-warning text-warning-50' row='" + element.userId + "' >";
else
tr = "<tr class='bg-light text-white-50 ' row='" + element.userId + "' >";
tr += "<td>" + element.sicilNo + "</td>";
tr += "<td>" + element.adSoyad + "</td>";
tr += "<td>" + element.bagliOlduguKisi + "</td>";
tr += "<td>" + element.bagliOlduguKisiSicilNo + "</td>";
tr += "<td style='display:none'>" + element.bagliOlduguKisiId + "</td>";
tr += "<td> <button onclick= 'tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ," + element.bagliOlduguKisiId + ", " + element.bagliOlduguKisi + ")' type='button' class ='btn btn-info'> TanΔ±mla </button></td>";
tr += "</tr>";
$('#IzinTanimListe').append(tr);
});
I open a modal with the define Btn onclick feature.
function tanimlaBtn(userId, adSoyad, bagliOlduguKisiId, bagliOlduguKisi) {
document.getElementById('userId').value = userId;
document.getElementById('bagliOlduguKisiId').value = bagliOlduguKisiId;
document.getElementById('bagliOlduguKisi').value = bagliOlduguKisi;
document.getElementById('adSoyad').value = adSoyad;
}
The adSoyad and bagliOlduguKisi information appears as undefined. They should be string values.
How can I solve this?
The parameters for the function should be wrapped in doubles quotes and escaped e.g \"
Try replacing:
<button onclick= 'tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ," + element.bagliOlduguKisiId + ", " + element.bagliOlduguKisi + ")'
With:
<button onclick='tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ,\"" + element.bagliOlduguKisiId + "\", \"" + element.bagliOlduguKisi + "\")'

JSON Formatting Issue?

I am trying to populate an html table of JSON data. I was provided the JSON data and cannot determine why it isn't populating but suspect the JSON may not be formatted properly. When I use other sample data it works, then when I sub in the JSON I was provided it doesn't work. I've tried copying the JSON into a file on my direct server, linking to what I was provided (here: https://boiling-fortress-75456.herokuapp.com/) and inserting it on myjson.com and reformatting the JSON data.
Here is my code:
<script>
$(function() {
var entries = [];
var dmJSON = "https://api.myjson.com/bins/6sjud?callback=?";
$.getJSON(dmJSON, function(data) {
$.each(data.entries, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.rank + "</td>" + "<td>" + f.name + "</td>" + "<td>" + f.march_rank + "</td>" + "<td> " + f.april_rank + "</td>" + "<td>" + f.may_rank + "</td>" + f.personal_volume + "</td>" + f.team_volume + "</td>" + "</tr>"
$(tblRow).appendTo("#incentive tbody");
});
});
});
</script>
<div class="wrapper">
<div class="profile">
<table id= "incentive" border="1">
<thead>
<th>Rank</th>
<th>Name</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>Highest Rank</th>
<th>Personal Volume</th>
<th>Team Volume</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
You need to get the objects from affiliate
$.each(data.affiliate, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.rank + "</td>" + "<td>" + f.name + "</td>" + "<td>" + f.march_rank + "</td>" + "<td> " + f.april_rank + "</td>" + "<td>" + f.may_rank + "</td>" + f.personal_volume + "</td>" + f.team_volume + "</td>" + "</tr>"
$(tblRow).appendTo("#incentive tbody");
});
Response from service
{
"affiliate":[{
"rank":1,"name":"Sally","march_rank":"Gold","april_rank":"Silver","may_rank":"Silver","highest_rank":"Silver","team_volume":12345
},{
"rank":2,"name":"Zelda","march_rank":"Gold","april_rank":"Bronze","may_rank":"Silver","highest_rank":"Gold","team_volume":12345
}
]
}

HTML button onclick function not working with jQuery variable as argument

success: function(jsonresponse) {
data = JSON.stringify(jsonresponse);
$.each(JSON.parse(data), function (index, item) {
var Id=item.id;
var JobId=item.JobId;
var eachrow = "<tr>"
+ "<td>" + item.id + "</td>"
+ "<td>" + item.JobId + "</td>"
+ "<td>" + item.UserId + "</td>"
+ "<td>" + item.updatedAt + "</td>"
+ "<td>" + "<button onclick='myFunction(JobId,Id, \"accepted\")' class='btn btn-success btn-block'>Reject</button>" + "</td>"
+ "<td>" + "<button onclick='myFunction(JobId,Id, \"rejected\")' class='btn btn-success btn-block'>Reject</button>" + "</td>"
+ "</tr>";
$('#tbody2').append(eachrow);
});
},
myFuntion(JobId,Id,\"accepted\") does not works with variable as parameter but when manually added then the function works, like myFuntion(1,3,\"accepted\").
Problem : You are not concatenating the JobId & Id in your HTML Code?
Solution :
Properly concatenate your JobId & Id variables in your HTML code and you will be good to go as :
success: function(jsonresponse) {
data = JSON.stringify(jsonresponse);
$.each(JSON.parse(data), function (index, item) {
var Id=item.id;
var JobId=item.JobId;
var eachrow = "<tr>"
+ "<td>" + item.id + "</td>"
+ "<td>" + item.JobId + "</td>"
+ "<td>" + item.UserId + "</td>"
+ "<td>" + item.updatedAt + "</td>"
+ "<td>" + "<button onclick='myFunction("+JobId+","+Id+", \"accepted\")' class='btn btn-success btn-block'>Reject</button>" + "</td>"
+ "<td>" + "<button onclick='myFunction("+JobId+","+Id+", \"rejected\")' class='btn btn-success btn-block'>Reject</button>" + "</td>"
+ "</tr>";
$('#tbody2').append(eachrow);
});
},

table json data returning undefined using jQuery

Hi I'm stumped as to why the data in my table is returned undefined. I think I'm close.
Please check out my jsfiddle:
$(document).ready(function() {
$.getJSON( "http://www.corsproxy.com/dvl.thomascooper.com/data/json_return.json", function( data ) {
//static table head
$('table.stats').append("<th>" + "</th>" + "<th>" + "Date" + "</th>" + "<th>" + "Brand" + "</th>" + "<th>" + "Author" + "</th>" + "<th>" + "Title" + "</th>" + "<th>" + "Posts" + "</th>" + "<th>" + "Exposure" + "</th>" + "<th>" + "Engagement" + "</th>");
//loop through json data
$.each(data.data.rows,function( i, val ){
//+1 to number each row starting at 1
var rowNum = i + 1;
//create table rows and cell and populate with data
$('table.stats').append( "<tr>" + "<td>" + rowNum + "</td>" + "<td>" + val.date + "</td>" +"<td>" + val.brand_id + "</td>" + "<td>" + val.author + "</td>" + "<td>" + val.title + "</td>" + "<td>" + val.posts + "</td>" + "<td>" + val.reach + "</td>" + "<td>" + val.interaction + "</td>" + "</tr>");
});
});
});
fiddle: http://jsfiddle.net/tommy6s/eLbq2wvh/
$.each(data.data.rows,function( i, val ) {
Here, val is not object (Your data told me that)
So you could not access property that undefined like this: val.date
I think, here is what you want:
$(document).ready(function() {
$.getJSON( "http://www.corsproxy.com/dvl.thomascooper.com/data/json_return.json", function( data ) {
//static table head
$('table.stats').append("<th>" + "</th>" + "<th>" + "Date" + "</th>" + "<th>" + "Brand" + "</th>" + "<th>" + "Author" + "</th>" + "<th>" + "Title" + "</th>" + "<th>" + "Posts" + "</th>" + "<th>" + "Exposure" + "</th>" + "<th>" + "Engagement" + "</th>");
//loop through json data
$.each(data.data.rows,function( i, val ){
//+1 to number each row starting at 1
var rowNum = i + 1;
//create table rows and cell and populate with data
$('table.stats').append( "<tr>" + "<td>" + rowNum + "</td>" + "<td>" + val[0].value + "</td>" +"<td>" + val[1].value + "</td>" + "<td>" + val[2] + "</td>" + "<td>" + val[3].label + "</td>" + "<td>" + val[4].values[0] + "</td>" + "<td>" + val[5].values[0] + "</td>" + "<td>" + val[6].values[0] + "</td>" + "</tr>");
});
});
});
If you look at the returned json - rows is an array of arrays not an array of objects.. You failed to take this into consideration.
$.each(data.data.rows[0],function( i, val ){
Also you are trying to access the value of the data by its value instead of by its key.
"<td>" + val.field + "</td>" +"<td>" + val.type + "</td>"
Checkout this jsbin for example
You are trying to reference the data as if it were:
rows: [ { date: '', brand_id: '', author: '', etc }, next set of fields]
What you are actually getting is:
rows: [ [ object with field data, object with field data, etc], another array of field data objects, etc]
Here is a beginning to a script that will be flexible in how things will be returned. I've left out many of the cases you'll need to deal with:
//loop through json data
$.each(data.data.rows,function( i, val ){
//+1 to number each row starting at 1
var rowNum = i + 1, fields = {};
// Find fields
$.each(val, function(j, fieldData) {
if(typeof fieldData == 'string') {
fields.author = fieldData;
} else if(fieldData.field == 'title') {
fields.title = fieldData.label;
} else {
fields[fieldData.field] = fieldData.value;
}
});
//create table rows and cell and populate with data
$('table.stats').append( "<tr>" + "<td>" + rowNum + "</td>" + "<td>" + fields.date + "</td>" +"<td>" + fields.brand_id + "</td>" + "<td>" + fields.author + "</td>" + "<td>" + fields.title + "</td>" + "<td>" + fields.posts + "</td>" + "<td>" + fields.reach + "</td>" + "<td>" + fields.interaction + "</td>" + "</tr>");
});

Add row to a table: String literal issue

This is a part of my code used to add new row to a table. I have problem with compiling the code related to last two TD elements. There is always an error of string literal. Please help me to figure it out.
$( "#content tbody" ).append( "<tr>" +
"<td>" + opr.val() + "</td>" +
"<td>" + flightNum.val() + "</td>" +
"<td>" + from_f.val() + "</td>" +
"<td id=" + flightNum.val() + " class='edit_but'>
<div>
<img src='images/edit.png' alt='Edit' />
</div>
</td>" +
"<td id=" + flightNum.val() + " class='deact_but' onclick="deactivateRow('+flightNum.val()+')">
<div>
<img src='images/delete.png' alt='Deactivate' />
</div>
</td>" + "</tr>" );
$( "#content tbody" ).append( "<tr><td>" + opr.val() + "</td><td>" + flightNum.val() + "</td><td>" + from_f.val() + "</td><td id=" + flightNum.val() + " class='edit_but'><div><img src='images/edit.png' alt='Edit' /></div></td><td id=" + flightNum.val() + " class='deact_but' onclick='deactivateRow(" + flightNum.val() + ")'><div><img src='images/delete.png' alt='Deactivate' /></div></td></tr>" );
Your code is a mess! there are so many quotes (") missing and string concating (+) missing!
After a lot of fixing:
$( "#content tbody" ).append( "<tr>" +
"<td>" + opr.val() + "</td>" +
"<td>" + flightNum.val() + "</td>" +
"<td>" + from_f.val() + "</td>" +
"<td id=" + flightNum.val() + " class='edit_but'>" +
"<div>" +
"<img src='images/edit.png' alt='Edit' />" +
"</div>" +
"</td>" +
"<td id=" + flightNum.val() + " class='deact_but' onclick='deactivateRow('" +flightNum.val()+")'>" +
"<div>" +
"<img src='images/delete.png' alt='Deactivate' />" +
"</div>" +
"</td></tr>" );​​​​​​​​​​​

Categories

Resources