Write .each data to two different classes with same div id - javascript

I have a jQuery .each that loops through div id's and writes JSON data from an AJAX call to inputs assigned a class.
Most div's only have one row of inputs to populate, but some have two rows of input classes.
All divs and classes are created dynamically, but each dynamic div does get its own unique ID.
When the .each hits the div with two rows with the same class it does not write to both rows but writes to the second row of classes with the second row from the database.
When users write to the db with the data from both rows it creates a separate row in the db.
One for badge1, and one for badge2.
Here is my .each that writes one row of inputs great
$.each(data, function(i, item) {
$("#" + item.full_op_id).find('.badge1').val(item.badge1);
$("#" + item.full_op_id).find('.badge2').val(item.badge2);
$("#" + item.full_op_id).find('.op_date').val(item.op_date);
$("#" + item.full_op_id).find('.op_qty').val(item.op_qty);
});
I have tried several things including an if/else statement on just those id's with two rows of classes.
I understand that when the .each sees the two rows it basically sees it as one row since they have the same class names except for badge1/badge2.
Should I run another loop inside the .each to loop through and write both rows for that div id and same input classes? If so, how should I do this?
Background on app: Created with Foundation 5, dynamically creates each panel row with data from one DB (MSSQL), then using AJAX/JSON users write to a separate DB (MYSQL) their input data.
The user should then be able to pull up the job again and see what they have done by pulling their data from the MYSQL db of their inputs.
That is the db I am pulling the data from when trying to insert the JSON data to the inputs for each div row and inputs.
Here is the HTML that creates these two rows:
<div class="row" id="op_div_id">
<div class="large-3 columns badge_div">
<label>
Operator 1 <span class="badge_required" style="font-size: 0.7em;">*required</span>
<div class="row collapse">
<div class="small-3 columns">
<button class="button prefix radius badge_scan_button" onclick="badgeButton1($(this));">
Scan
</button>
</div>
<div class="small-9 columns">
<input type="text" class="badge1" name="badge1" onchange="badgeColor1($(this));" placeholder="Badge Number">
<input type="hidden" class="time_assigned" name="time_assigned" value="">
</div>
</div>
</label>
</div>
<div class="large-3 columns date_div">
<label>Today's Date <span class="date_required" style="font-size: 0.7em;">*required</span>
<input type="text" class="op_date" name="op_date" onclick="popDate($(this));" placeholder="Today's Date">
</label>
</div>
<div class="large-3 columns qty_div">
<label>Quantity <span class="qty_required" style="font-size: 0.7em;">*required</span>
<input type="text" class="op_qty" name="op_qty" onblur="opQtyColor($(this));" placeholder="Quantity">
</label>
</div>
<div class="large-3 columns">
<label>Complete Operation</label>
<button class="button postfix save_op1" onclick="saveOp1($(this));">
Save
</button>
</div>
</div>
<div class="row" id="op_div_id2">
<div class="large-3 columns badge_div">
<label>
Operator 2 <span class="badge_required" style="font-size: 0.7em;">*required</span>
<div class="row collapse">
<div class="small-3 columns">
<button class="button prefix radius badge_scan_button" onclick="badgeButton2($(this));">
Scan
</button>
</div>
<div class="small-9 columns">
<input type="text" class="badge2" name="badge2" onchange="badgeColor2($(this));" placeholder="Badge Number">
<input type="hidden" class="time_assigned" name="time_assigned" value="">
</div>
</div>
</label>
</div>
<div class="large-3 columns date_div">
<label>Today's Date <span class="date_required" style="font-size: 0.7em;">*required</span>
<input type="text" class="op_date" name="op_date" onclick="popDate($(this));" placeholder="Today's Date">
</label>
</div>
<div class="large-3 columns qty_div">
<label>Quantity <span class="qty_required" style="font-size: 0.7em;">*required</span>
<input type="text" class="op_qty" name="op_qty" onblur="opQtyColor($(this));" placeholder="Quantity">
</label>
</div>
<div class="large-3 columns">
<label>Complete Operation</label>
<button class="button postfix save_op2" onclick="saveOp2($(this));">
Save
</button>
</div>
</div>

Thanks everyone for your support and help on this. I have come up with a solution that works pretty well. Thank you #Jason for the tip to only select the ID once.
success: function (data) {
var el;
var done_row1 = false;
$.each(data, function (i, item) {
el = $("#" + item.full_op_id);
if (item.full_op_id == "210-SUTB015" || item.full_op_id == "210-PICKA" || item.full_op_id == "44-SUTB015") {
if (done_row1 === false) {
el.find('div#op_div_id input.badge1').val(item.badge1);
el.find('div#op_div_id input.op_date').val(item.op_date);
el.find('div#op_div_id input.op_qty').val(item.op_qty);
done_row1 = true;
} else {
el.find('div#op_div_id2 input.badge2').val(item.badge2);
el.find('div#op_div_id2 input.op_date').val(item.op_date);
el.find('div#op_div_id2 input.op_qty').val(item.op_qty);
// reset row1 test
done_row1 = false;
}
} else {
el.find('div#op_div_id input.badge1').val(item.badge1);
el.find('div#op_div_id input.op_date').val(item.op_date);
el.find('div#op_div_id input.op_qty').val(item.op_qty);
}
});
}
Another developer at my work helped me with this solution, so I am not 100% sure what it is doing to make it work but it is. We just separated out the writing of the rows between badge1 and badge2 using another if/else statement and true/false statements. Thanks again everyone!

I'm not sure I understand correctly, but try changing
$("#" + item.full_op_id).find('.badge1').val(item.badge1);
to
$("#" + item.full_op_id).find('.badge1').each(function() {
$(this).val(item.badge1);
});

Related

Replacing files in input using Vue.js

I have Vue.js template file with data that contains documents. My page has table. Table has rows with input buttons upload file, like this
<tr v-for="(doc, index) in documents">
<td :id="'doc-' + doc.id" v-show="doc.visible">
<div class="row">
<div class="col-md-9">
<a v-if="doc.document" :href="doc.document.file" v-text="doc.name"></a>
<div v-else v-text="doc.name"></div>
</div>
<div class="col-md-3">
<div class="row">
<div v-if="doc.document" class="col-md-8">
<label :for="'upload_doc_' + doc.id">
<span class="glyphicon glyphicon-upload text-primary" role="button"> Upload</span>
<input type="file"
:id="'upload_doc_' + doc.id"
class="hidden"
#change="replaceDoc($event, index)"
/>
</label>
</div>
</div>
</div>
</div>
</td>
So, some rows may contain some files and some not. But button should replace or add file to the row. So i wrote method:
methods: {
replaceDoc (event, index) {
this.documents[index] = event.target.files[0]
},
But it seems that it does not contain any data, when I'm trying to send it to server it sends empty dictionary.
Have you tried using Vue.set or this.$set
Instead of:
this.documents[index] = event.target.files[0]
Try this:
this.$set(this.documents, index, event.target.files[0]);
OR
Vue.set(this.documents, index, event.target.files[0]);
You can refer to this API.

Jquery Need siblings value but not working

I want to populate the value of the "eventTitle" in "Requirement" input box when some one click on the corresponding check box. i.e If some one clieck on the check box of Vels Group Of Instutions then automatically i want this to populate in texbox with name "Requirement" if multiple check box are clicked i want it to be comma seperated. Below is the code i tried to get but it is not working and getting undefined.
<div class="wid100">
<div class="eventTitle">Vels Group Of Instutions</div>
<div class="eventDate">2017-07-25</div>
<div class="eventVenue">This is world wide institute of technology </div>
<div class="selectEvent">
<input type="checkbox" class="seminar selected" id="179">
<label for="179"></label>
</div>
</div>
<div class="wid100">
<div class="eventTitle">Title goes here</div>
<div class="eventDate">2017-07-25</div>
<div class="eventVenue">sdfdsafasdfdsafdsafsadfsdfsdf </div>
<div class="selectEvent">
<input type="checkbox" class="seminar" id="179">
<label for="179"></label>
</div>
</div>
<input type="text" name="Requirement" placeholder="Title 01" id="divclass" required="required" class="pull-left" />
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
$(".seminar").click(function () {
if ($(this).is(":checked")) {
//checked
$(this).addClass("selected");
var event_title = "";
event_title = $(".selected").siblings('.eventTitle').val();
console.log(event_title); return false;
} else {
//unchecked
$(this).removeClass("selected");
}
});
.eventTitle is not the sibling of .selected and the .eventTitle is a div element having no value, text there. change this line
event_title = $(".selected").siblings('.eventTitle').val();
to
event_title = $(this).parent().siblings('.eventTitle').text();
or
event_title = $(this).parent().siblings('.eventTitle').html();
The issue you have is because .eventTitle is not a sibling of the clicked checkbox, so the DOM traversal logic is wrong. div elements also do not have a val(), so you should use text() or html() instead.
However, you can improve the logic and also achieve the comma separated list of the selected event titles by using map() to build an array which you can then join() before setting in the value of #divclass. Try this:
$(".seminar").click(function() {
$(this).toggleClass('selected', this.checked);
var eventNames = $('.seminar:checked').map(function() {
return $(this).closest('.wid100').find('.eventTitle').text();
}).get().join(',');
$('#divclass').val(eventNames);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="wid100">
<div class="eventTitle">Vels Group Of Instutions</div>
<div class="eventDate">2017-07-25</div>
<div class="eventVenue">This is world wide institute of technology </div>
<div class="selectEvent">
<input type="checkbox" class="seminar selected" id="179">
<label for="179"></label>
</div>
</div>
<div class="wid100">
<div class="eventTitle">Title goes here</div>
<div class="eventDate">2017-07-25</div>
<div class="eventVenue">sdfdsafasdfdsafdsafsadfsdfsdf </div>
<div class="selectEvent">
<input type="checkbox" class="seminar" id="179">
<label for="179"></label>
</div>
</div>
<input type="text" name="Requirement" placeholder="Title 01" id="divclass" required="required" class="pull-left" size="100" />
I'd suggest changing the id of the #divclass to something more descriptive, as the element is not a div, and it's an identifier, not a class.
Finally, your .seminar elements have the same id attribute which is invalid. You should ensure that the ids are unique within the DOM - assuming that this is not just a typo from copy/pasting the code in the question.

How can I use the same jQuery function for multiple divs without repeating the code

I have a add/remove textfield feature wherein I want to implement a jQuery function to all the textfields in a div.
Can I use the same jQuery function for all the fields in the div or should I write different for each.
DEMO can be got here. In the demo I have made it work for one input field, how can I make it work for the rest.
HTML
<div id="single">
<div id="Ivrgroupzbase_grpzWelcomeNotes" class="row">
<h2><a id="addScnt" href="#">Add Another Input Box</a></h2>
<label for="Ivrgroupzbase_grpzWelcomeNotes">Grpz Welcome Notes</label>
<input type="text" id="Ivrgroupzbase_grpzWelcomeNotes" name="Ivrgroupzbase[grpzWelcomeNotes]" cols="50" rows="6">
</div>
<div id="p_scents" class="row">
<h2><a id="addScnt" href="#">Add Another Input Box</a></h2>
<label for="Ivrgroupzbase_grpzWelcomeNotes">Grpz Welcome Notes</label>
<input type="text" id="Ivrgroupzbase_grpzWelcomeNotes" name="Ivrgroupzbase[grpzWelcomeNotes]" cols="50" rows="6">
</div>
</div>
fiddle http://jsfiddle.net/Drea/28h0L6eb/
html
changed id "addScnt" to class - you should avoid using same id multiple times
js
$('.addScnt').on('click', function (event) {
event.preventDefault();
$(this).closest('.row').append(createInput($('#Ivrgroupzbase_grpzWelcomeNotes p').length + 1), '');
});

jquery dynamic subselection combining last and form elements

Update
Tidied up the solution in progress and added some extra details
I have a form area which creates clones based on a template. In order to make sure the form transmits in an order, the script goes through the form at send time appending a number which defines the current batch set. Below is an over simplified representation of what is going on:
<form>
<div class="batch-template">
<div class="batch-piece">
<a class="clone" />
<input name="test-input">
<input name="another-test-input">
<select name="a-drop-down">
</div>
</div>
<div class="batch-paste-area">
</div>
</form>
When the page starts:
The contents of "batch-template" are stored to an object variable
The original template is removed from the page
An instance of the template is appended to the "batch-paste-area"
The following is an example of the output created after clicking twice.
<form>
<div class="batch-template">
</div>
<div class="batch-paste-area">
<div class="batch-piece">
<a class="clone" />
<input name="test-input">
<input name="another-test-input">
<select name="a-drop-down">
</div>
<div class="batch-piece">
<a class="clone" />
<input name="test-input">
<input name="another-test-input">
<select name="a-drop-down">
</div>
</div>
</form>
When it comes to submitting the form: prior to serialization, I would like the script to loop through each "batch-piece" within "batch-paste-area" and add a count value to the end of each form field name. Continuing with the set above, the result (to a browser) would seem like that shown below:
<form>
<div class="batch-template">
</div>
<div class="batch-paste-area">
<div class="batch-piece">
<a class="clone" />
<input name="test-input1">
<input name="another-test-input1">
<select name="a-drop-down1">
</div>
<div class="batch-piece">
<a class="clone" />
<input name="test-input2">
<input name="another-test-input2">
<select name="a-drop-down2">
</div>
</div>
</form>
So far, I can either loop through EVERY input within the paste area or just select the last.
Selecting the last batch-piece is simple:
var intCount = 1;
$('.batch-paste-area .batch-piece').each(function(){
/*
* Would like to be able to loop through form fields here
* Below is an attempt to select all form fields for current set
*/
$(this + ' input, '+ this + ' select').each(function() {
var strName = $(this).attr('name') + intCount;
$(this).attr('name', strName);
});
intCount++;
});
Frustratingly, I had actually tried the correct solution in advance but had forgotten to use the comma at the time!
var intCount = 1;
$('.batch-paste-area .batch-piece').each(function(){
/*
* Would like to be able to loop through form fields here
* Below is an attempt to select all form fields for current set
*/
$(this).find("input, select").each(function() {
var strName = $(this).attr('name') + intCount;
$(this).attr('name', strName);
});
intCount++;
});

Sorting Input Text inside multiple div's using javascript / jquery?

There are three fields(first name, Last name & age) displayed in text boxes. Each field is displayed in separate div's. There are 4 records. On clicking a sort button above each field the div records should be sorted based on the data type of the field and should displayed in the HTML page.
I tried the solution in this link
I can't use this because the records are displayed in text box within the div.
<div id="content">
<div>
<div class="price"><input type="text" class="pri" value="120"/></div>
<div class="dateDiv">2012-05-09 20:39:38.0</div>
<div class="distance">20 mile</div>
</div>
<div>
<div class="price"><input type="text" class="pri" value="123"/></div>
<div class="dateDiv">2012-05-10 20:39:38.0</div>
<div class="distance">30 mile</div>
</div>
<div>
<div class="price"><input type="text" class="pri" value="100" /></div>
<div class="dateDiv">2012-05-11 20:39:38.0</div>
<div class="distance">50 mile</div>
</div>
<div>
<div class="price"><input type="text" class="pri" value="124"/></div>
<div class="dateDiv">2012-05-12 20:39:38.0</div>
<div class="distance">60 mile</div>
</div>
</div>
How can I do this in javascript?
see this demo using jquery.tinysort.min.js
see also this one
You can use knockoutjs to do something like that.
For example, the html:
<div id="content" data-bind="foreach: lines">
<div class="fname"><input type="text class="pri" data-bind="value: fname"/></div>
<div class="lname" data-bind="text: lname"/>
<div class="age" data-bind="text: age"/>
</div>
Javascript:
function EachDivViewModel(line)
{
this.fname = line.fname;
this.lname = line.lname;
this.age = line.age;
}
function YourViewModel()
{
var self = this;
self.lines = ko.observableArray([]); // this array will contain elements of EachDivViewModel type
self.handlerForYourSortButtongs = function() {
// Code here to sort the array based on the button clicked
// The UI will automatically get updated as you reorder the elements in the lines array
}
}
$(document).ready(function() {
var yourViewModelInstance = new YourViewModel();
// Code to get the lines here
ko.applyBindings(yourViewModelInstance);
});
It can be done using tablesorter.
Hope that will help you out.

Categories

Resources