Create multiple unique barcodes in a loop using JSBarcode - javascript

I am creating a table of barcodes using JSBarcode and a for-each loop. The problem is that the value of the barcodes are always the same (for some reason they are all the value of the last element in the loop).
Here is my code:
<tbody>
<% lineitemObj.forEach(function(obj) { %>
<tr>
<td><%= obj.id %></td>
<td><%= obj.drawing_num %></td>
<td><%= obj.description %></td>
<td><%= obj.qty %></td>
<td>
<svg id="barcode"></svg>
<script>
var ln = '<%= obj.id %>'
JsBarcode("#barcode", ln);
</script>
</td>
</tr>
</tbody>
How do I get the barcodes to properly represent the unique id of each object?

Try it
JsBarcode("#barcode", "12345", {format: "pharmacode"});

Related

Rails how to Get value in Javascript of each object in table?

this is my table:
<tbody>
<% #productos.each do |producto| %>
<tr>
<td><%= producto.codigo %></td>
<td><%= producto.descripcion %></td>
<td><%= producto.stock %></td>
<td><%= button_to '+', "#", method: :get,class: 'btn btn-info btn-sm btn_add' %></td>
</tr>
<% end %>
</tbody>
On button click i want to get the value of |producto| (each row)in JAvascript and send to other table in the same view OR the right way is to get the value from td ? example:
<td><%= producto.codigo %></td> //in this case how i get the value of this td?
Using Javascript or Jquery. thanks
First add id in every td al follows
<tbody>
<% #productos.each do |producto| %>
<tr>
<td id="code-<%= producto.codigo %>"><%= producto.codigo %></td>
<td id="des-<%= producto.codigo %>"><%= producto.descripcion %></td>
<td id="stock-<%= producto.codigo %>"><%= producto.stock %></td>
<td><%= button_to '+', "#", method: :get,class: 'btn btn-info btn-sm btn_add' %></td>
</tr>
<% end %>
</tbody>
Now in you button add an attribute data-id like this
<button class="btn btn-info btn-sm btn_add" data-id="<%= producto.codigo %>" type="button">Click</button>//I don't know about Rails so do this code in your language.
Now The jQuery is given below...
<script>
$(document).on('click','btn_add',function(){
id = $(this).data('id'); //get the clicked button's id
var code = $('#code-'+id).html(); //get the code of that row
var des = $('#des-'+id).html(); //get the description of that row
var stock = $('#stock-'+id).html(); //get the stock of that row
alert('Code - '+code+'<br> Description - '+des+'<br>Stock - '+stock);
});
</script>
1- pass all the variable which you want in js code block on click event
<td><%= button_to '+', "#", method: :get,class: 'btn btn-info btn-sm btn_add', :onclick => "get_current_producto(this, '<%=producto.codigo%>', '<%=producto.descripcion%>', '<%=producto.stock%>');" %></td>
2- get all variables here and
<script type="text/javascript>
function get_current_producto(elm, codigo,descripcion, stock ){
//code goes here
//console.log(codigo);
alert(descripcion);
};
</script>
here elm is button element on which click event is called you can get id or class of that button element by using elm

Automatic Serial Number Generator Using EJS Loop

I want to Generate Automatic Serial number for my Registered Admins
<tbody>
<% Admins.forEach(function (Admin){ %>
<tr>
<td>//S.No Here Starts from 1 //</td>
<td><%= Admin.First_Name %></td>
<td><%= Admin.Last_Name %></td>
<td><%= Admin.User_Name %></td>
<td><%= Admin.Email %></td>
<td><%= Admin.Password %></td>
<td><%= Admin.Contact %></td>
</tr>
<% }) %>
forEach passes the index as the second argument. It's zero-based so you'll need to add 1:
<% Admins.forEach(function (Admin, index) { %>
<tr>
<td><%= index + 1 %></td>

How to apply a color to <td> tag in .ejs file in nodeJS

I want to know how to apply a color in .ejs file. Here is the code.
<% resultList.forEach(function(item, index){ %>
<tr>
<td><%= item.function %></td>
<td><%= item.value %></td>
<td><%= item.content %></td>
</tr>
<% }); %>
if(item.value == false) //I want to apply red color
How can I do that? Thank you.
Would something like this work?
<% resultList.forEach(function(item, index){ %>
<tr>
<td><%= item.function %></td>
<% if (item.value === false) { %>
<td style='color:red;'><%= item.value %></td>
<% else %>
<td><%= item.value %></td>
<% } %>
<td><%= item.content %></td>
</tr>
<% }); %>
Note personally I think this is ugly. I would try to keep more code out of template side and would also use a CSS class for the coloring, but it was easier to illustrate this way.

Iterate over an array by index when click on submit button Ruby on Rails

I have an array of Pet instances and each of them have a name, type, sex and size, etc... I'd like to iterate through this collection of Pet instances and display the each one by one.
This is what I have so far:
<% #pets.each do |pet| %>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Sex</th>
<th>Age</th>
<th>breed</th>
<th>Size</th>
<th>Picture</th>
<th>Description</th>
<th>Shelter_id</th>
<th>Shelter</th>
<th>Phone</th>
<th>Email</th>
<th>City</th>
<th>Zip</th>
<th>Like</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= pet.name %></td>
<td><%= pet.species %></td>
<td><%= pet.sex %></td>
<td><%= pet.age %></td>
<td><% pet.breeds.each do |breed| %>
<li><%= breed.name %></li>
<%end%>
</td>
<td><%= pet.size %></td>
<td><%= image_tag pet.picture %></td>
<td><%= pet.description %></td>
<td><%= pet.shelter_id %></td>
<% shelter = Shelter.find(pet.shelter_id)%>
<td><%= shelter.name %></td>
<td><%= shelter.phone %></td>
<td><%= shelter.email %></td>
<td><%= shelter.city %></td>
<td><%= shelter.zip %></td>
<td><%= form_for pet, :url => { :controller => "favorite_pets", :action => "create" }, :html => {:method => :post} do |f| %>
<%= f.text_field :id %>
<%= f.submit %>
<% end %>
</td>
</tr>
</tbody>
</table>
<%end%>
So now, I need two thinks.
1st
When a user clicks the submit button (<%= f.submit %>) to send a POST request i do not want the page to reload nor to redirected me to another page. I looked around and it seems like :remote => true included in the form_for would do the trick but i'd need more help with that.
2nd
I'd like to control the iterating through #pets so that only the first one is displayed pet[0] and every time I click on the <%= f.submit %> button not only does it not reload the page but it displays the next index in the array pet[1]
I think I need to set up a sort of counter which would increment the index of the pet array every time a user click on the <%= f.submit %> button.
Any guiding thoughts?
If I were to do this I would use some javascript. So your submit button turns into more of an "Add Button" it doesn't actually submit anything. It would just add that object (in your case the pet, along with its info) to a javascript array as well as display the new object (again the pet along with the info) on the window. Then create another button to actually submit all the pets. (The javascript array)

Rails rest in peace gem - how to use in each loop?

I am using the Rest in place gem: https://github.com/janv/rest_in_place.
How to use it correctly inside of an each loop? More specifically, for a belongs_to relationship ?
For Example, User has_many :fee_agreements and FeeAgreement belongs_to :user
How to utilize the edit in place feature to update the fee_agreement "loan_amount" here?
<tbody>
<% #user.fee_agreements.each do |fa| %>
<tr>
<td><%= fa.user_id %></td>
<td><%= fa.loan_id %></td>
<td id="<%= dom_id(fa) %>"><span class="rest-in-place" data-formtype="input" data-attribute="loan_amount"><%= fa.loan_amount %></span></td>
#above is the line in question
</tr>
<% end %>
</tbody>
Console output:
Started PUT "/users/51" for 127.0.0.1 at 2014-03-19 00:19:29 -0400
Processing by UsersController#update as JSON
Parameters: {"authenticity_token"=>"xxx=", "fee_agreement"=>{"loan_amount"=>"998988"}, "id"=>"51"}
I do have a separate controller for FeeAgreements which "builds" for the user..
def create
#fee_agreement = current_user.fee_agreements.build(:loan_id => params[:loan_id])
if #fee_agreement.save
redirect_to fee_agreements_path
else
redirect_to index_path
end
end
try this out:
instead of rest_in_place use Best in Place gem
after some initial setup as described in screencast you can use it by
<tbody>
<% #user.fee_agreements.each do |fa| %>
<tr>
<td><%= fa.user_id %></td>
<td><%= fa.loan_id %></td>
<td><%= beast_in_place fa.loan_amount %></span></td>
#above is the line in question
</tr>
<% end %>
</tbody>

Categories

Resources