Dojo: option[selected ='selected'] not working for run-time change - javascript

I am working on a multi-select box and found
var count = dojo.query("option[selected ='selected']", dojo.byId('select_id')).length;
Always returns whatever original from the page(database) but yet what user selected at run-time. I am running on Dojo 1.6. So how can I count number of selected options from multi-select box AT RUN-TIME?

I made a page that shows users and the groups they are in. Here is some of the code. I had to rip some stuff out to make it concise. The last line of code answers the question. It returns an array with the checked values.
// Programattically create the select.
var _groups = new dojox.form.CheckedMultiSelect({
multiple : true,
class : "cssThing"
}, "groups" + _userNumber);
// Fill the CheckedMultiSelect boxes with unchecked data.
var tempArray = new Array();
dojo.forEach(groupList, function(row, index) {
tempArray.push({
value : row.value,
label : row.label,
selected : false,
});
});
_groups.addOption(tempArray);
// Populate the CheckedMultiSelect with an array.
var tempArray = [];
dojo.forEach(user.groups, function(row) {
tempArray.push(String(row.groupName));
});
_groups.set('value', tempArray);
// Get the CheckedMultiSelect values as an array.
_groups.get('value');

Related

Retrieving the Line Item of an Old Record

I have a User Event Script that is deployed to Sales Orders. It uses the field values of line items to determine the quantity left. However, if I remove a line item it doesn't update the quantities of the removed item. I need the the item to be updated afterSubmit.
Is it possible to edit the quantities of the removed line item using nlapiGetOldRecord() or something of that sort?
Here's what the code looks like:
function afterSubmit(){
var curRec = nlapiGetRecordId();
var item = nlapiLoadRecord('item', curRec);
var sold = item.getFieldValue('cust_sold');
var quantity = item.getFieldValue('cust_quantity');
var leftToSell = quantity - sold;
item.setFieldValue('cust_lefttosell', leftToSell);
var finalValue = item.getFieldValue('cust_lefttosell');
var old = nlapiGetOldRecord(); // only retrieves salesorder record
nlapiSubmitRecord(item);
}
EDIT: So it turns out I can target the line items with a simple old.getLineItemValue('item', 'item', linenum).
As Adolfo pointed out below, I can target the line item of the old record. For some reason I thought the only way to target it was by using nlapiGetLineItemField(type, fldnm, linenum). The getLineItemValue version of the function was exactly what I was looking for.
This is what the code would look like:
var old = nlapiGetOldRecord();
var id = old.getLineItemValue('item', 'item', linenum);
var rec = nlapiLoadRecord('type', id);
Why not make cust_lefttosell a formula field so the value is always calculated on the fly from cust_quantity - cust_sold?
Anyways, if you want to use an afterSubmit then you have to iterate through all the line items in the old record, find out which ones were removed and then update the quantity on those.

Filter data with JSLink Sharepoint

I use JSLink to customize a SharePoint display list.
I managed to filter data as you can see in the following link:
https://sharepoint.stackexchange.com/questions/91317/filter-out-items-in-list-view-using-jslink
When I click on the title of a column to try to filter a data second time, it systematically brings back all the old data instead of sorting out the previously filtered ones.
In the provided example the filter is applied by row index and this is explains why different rows are displayed before and after sorting applied.
The below example demonstrates how to hide rows by list item id, in that case the filter will be applied consistently to the same rows:
(function () {
function listPreRender(renderCtx) {
var excludeItemIds = [1]; //hide list item with Id=1
var rows = renderCtx.ListData.Row; //get current rows
var filteredRows = rows.filter(function(row){
var curItemId = parseInt(row.ID);
if(excludeItemIds.indexOf(curItemId) === -1)
return row;
});
renderCtx.ListData.Row = filteredRows;
renderCtx.ListData.LastRow = filteredRows.length; //update ListData.LastRow property
}
function registerListRenderer()
{
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
Templates : {
OnPreRender : listPreRender
}
});
}
ExecuteOrDelayUntilScriptLoaded(registerListRenderer, 'clienttemplates.js');
})();
Results
Filtered list view
Filtered list view after applying sorting

JQuery Datatables Row Data From AJAX Source

In the past I've always used this to get a hidden column's data. I would hide the column with a css class, but the responsive feature doesn't work well with these.
var td = $('td', this);
var ID = $(td[0]).text();
So I found an alternative, by hiding the columns with these classes with the responsive feature.
"columnDefs": [
//Responsive classes
{ className: 'never', targets: 0 }, //Hide on all devices
{ className: 'all', targets: 1 }, //Show on all devices
]
and then I use either one of these.
var rowData = oTable1.fnGetData(this);
var rowData = oTable1.api().row(this).data();
//Grab the first indexed item in the list
var ID = rowData[0];
That works well if you don't have an AJAX source. It will return a comma separated list of the row data. However, when I try to use this with an AJAX source I just get [object Object] back (instead of a comma separated list) if I output the rowData variable in an alert.
How do I get the row data out of a table with an AJAX source?
It seem to be stored as string so [1, 2, 3] became [object Object] when you turn it into string. Do yourString = yourList.join(',') and store yourString to keep the coma-separated string.
For an object:
yourString = (function () {
var list = [];
for(var i in yourList)
if(yourList.hasOwnProperty(i))
list.push(yourList[i]);
return list.join(',');
})();
The function is not needed, it's just to limit the variables scope.
I ended up using an answer I found here.
Converting a JS object to an array
I can pull the entire row data from the table with this.
var rowData = oTable1.api().row(this).data();
In the console log I can see that it returns a javascript object like this.
Object { id="123456", full_name="Samuel Smith", Last_name="Smith" }
I use this function to convert the object into an array.
var array = $.map(rowData, function (value, index) {
return [value];
});
In the console log, my array would appear like this.
["123456", "Samuel Smith", "Smith"]
I can then extract any item from the array like this.
alert(array[0]);
Simplifying madvora's example:
var rowData = oTable1.api().row(this).data().to$();
rowDataArray = rowData.toArray();

How to fetch existing JSON that exists in data-attr and update that

In the given fiddle , click on Addons buttons and on selection and unselection
of Checkboxes , i am trying to update the data-attr
array present as data-stuff .
Once i set the data how can i fetch the existing and update it with new data .
http://jsfiddle.net/kgm9o693/9/
// checkbox checked
$(document).on('click', '.ui-checkbox-off', function (event) {
var vendoritemsdata = $(".lastItm_Wrap").data('stuff');
var checkboxid = $(this).next().attr("id");
var cost = $(this).attr("cost");
var toppcrusts = [];
toppcrusts.push({
'name': checkboxid,
'cost': cost
});
if (vendoritemsdata.length == 0) {
$('.lastItm_Wrap').attr('data-stuff', toppcrusts);
}
else {
var existingdata = $('.lastItm_Wrap').data('data-stuff');
}
});
Could you please tell me how to resolve this ??
You are trying to use the DOM as a variable. It should be the other way around. Use the DOM only to show results (total cost in your case). But before that keep everything into an array serialize the array if you need it as json or data-stuff.
Examine the example at the bottom of this http://api.jquery.com/serializeArray/
If you want to keep doing it your way, convert the data to JSON and use this:
Set data
$('.lastItm_Wrap').attr('data-stuff', JSON.stringify(toppcrusts) );
Get data
var existingdata = JSON.parse( $('.lastItm_Wrap').attr('data-stuff') );
http://jsfiddle.net/kgm9o693/12/

Creating Select Box from options stored in a variable

I want to create a select box from options stored in a variable (the values will change based on the user).
For now, I'm just trying to get it to work with this variable in my javascript file:
var resp = {"streams": [ {"sweet":"cookies"}, {"savory":"pizza"}]}
In the html file, I have a select id "selectedStream"
How do I invoke, both the select id from html and the variable from javascript to create the select box?
I've seen examples such as the one below, but I don't understand how to link the id and the variable to the box.
$("option:selected", myVar).text()
I hope this was coherent! Thanks
I think what you are trying to do is append option html nodes to an existing select element on your screen with an id of 'selectedStream'. You want to use the data from the 'resp' variable to populate the text and value of the option nodes that you are appending. If this is correct, I have implemented that functionality with this jsfiddle. The javascript is also below:
$(function(){
var resp = {"streams": [ {"sweet":"cookies", "savory":"pizza"}]};
var streamData = resp.streams[0];
var optionTemplate = "<option value=\"{0}\">{1}</option>";
for(var key in streamData){
var value = streamData[key];
var currentOptionTemplate = optionTemplate;
currentOptionTemplate = currentOptionTemplate.replace("{0}", key);
currentOptionTemplate = currentOptionTemplate.replace("{1}", value);
$("#selectedStream").append($(currentOptionTemplate));
}
});
Is that array necessary? If you're just trying to display the keys within that object I'd create a for loop:
var resp = { "streams": {"sweet": "cookies", "savory": "pizza"} }
for (property in resp.streams) {
$('#selectStream').append($('<option/>', {text: property, value: property}));
}
JSFiddle: http://jsfiddle.net/pWFNb/

Categories

Resources