deleting a column in a table javascript - javascript

I'm developing a JavaScript application using Google Visualization API. I wrote a event listener so whenever the user clicks in the column, he has the option to hide it, if he does not want to see it.
google.visualization.events.addListener(table, 'select',
function selectHandler(){
var data_table = table.getDataTable();
confirm("hide column?", "Yes", "No");
alert(data_table.removeColumn(0));
}
);
I get the following error:
data_table.removeColumn is not a function
alert(data_table.removeColumn(0));
The API description can be seen here. It's interesting why the first alert tells me "30", the number of columns in the table, while the removeColumn(index) function does not do anything at all. Any Thoughts?
Thanks

Is it because your code snippet invokes removeColum instead of removeColumn?

I assume you're saying that the second two alerts don't happen.
If so, it's probably because of this typo...
// -----------------------v
alert(data_table.removeColum(1)); // missing "n"

I suspect that perhaps data_table is not, in fact, not the type of object you expect.
Have you used console.dir to look at the methods available on it?

Related

Closures in For Loops With Onclick Assignment

I'm trying to have a navBar that generates automatically by looping through an array of "Page" objects. Unfortunately, I seem to be falling into the loops/closure trap. I have read several threads related to this and in some cases have copy and pasted solution code and passed in my own variables but I'm struggling to make it assign onclicks correctly.
I know I'm close. In the below code are two options that I have tried.
Am I getting something wrong with the paremeter in parenthesis in the self-calling function? - the ()(divId)? I don't really understand this part.
Could I also be struggling because this is being done as an object method?
Any help much appreciated but go easy on me, I'm learning all this in my spare time! ;)
Thanks in advance.
EDIT: Jsfiddle: https://jsfiddle.net/mcgettrm/fs0mtz6n/
var navBar = {
display: function(){
for(i=0;i<pages.length;i++){
document.getElementById('navBar').innerHTML += pages[i].token;
var divId = pages[i].unique;
// code works fine up to here.
// option one(below): when navBar.display() is called the following code only adds
// the onclick to the final navbar link
document.getElementById(divId).onclick=(function(divId) {
return function() {
alert(divId);
};
})(divId);
//option two(below): when navBar.display() is called the following code logs
// the individual div id's correctly. But, it does it without being clicked. Then,
// only the last item in the loop is clickable.
(function(divId){
document.getElementById(divId).onclick= function(){
console.log(divId);
}
}
)(divId);
}
}
};
I've got it working here - https://jsfiddle.net/pqu9kr85/ it doesn't seem to have been to do with the binding of i more that you needed to build up the navigation html first, making sure it was in the DOM before binding the event. I put two separate loops, one to generate the nav, the second to bind the events. Also updated the page.display() to use this as that will have been affected by the value of i.

Can't access object in jquery (prevObject?)

A function in my WP plugin has just randomly (as far as I can tell) stopped working.
Here's the code in question:
window.send_to_editor = function(html) {
var classes = jQuery('img',html).attr('class');
var items = classes.split(" ");
... more stuff here
}
I've confirmed that the html variable is indeed an img html tag. Here's what firebug shows when I do a console.log of the object (console.log(jQuery('img',html));):
Object[]
context -> undefined
jquery -> "1.11.2"
length -> 0
prevObject -> Object[img.alignnone.size-full.wp-image-1234 name.jpg]
And the error it shows is classes is undefined.
I figure there's something wrong with the object I get, but this used to work recently and I'm not aware of any changes in the site that could have caused this.
I'd appreciate any input on this.
EDIT:
More info. This happens with two plugins which are supposed to be unrelated (made by different people). It happens when, after uploading an image to the server (or selecting a previously uploaded picture) you try to insert it into the post.
As I said before this error has appeared out of nowhere, it was working as intended a couple days ago. The only thing I can think of that has changed since then is the domain name, but I can't see how that could be related.
The jQuery selector always returns a jQuery object, but when the length is 0 then no elements were found matching the selector that you provided. In your example you've confirmed that nothing is selected as the length of the jQuery object is 0. Perform a check whether an element was selected like this:
var $els = jQuery('img',html),
classes;
if ($els.length) {
classes = $els.attr("class");
}
Keep in mind that your DOM query is limited by what you pass in as the html parameter. If you simply want to find the images on the page do: var $els = jQuery('img');
I finally managed to fix this; the key was parsing the html string variable into proper HTML, using jQuery.parseHTML(). Thanks to everyone who helped!

ajaxToolkit PopupControlExtender not working. Outdated?

I'm followed this tutorial but I'm getting runtime error when mouseover:
Sys.ArgumentUndefinedException: Value cannot be undefined. Parameter
name: type
The problem is in this lines of code:
string OnMouseOverScript = string.Format("$find('{0}').showPopup();", behaviorID);
string OnMouseOutScript = string.Format("$find('{0}').hidePopup();", behaviorID);
img.Attributes.Add("onmouseover", OnMouseOverScript);
img.Attributes.Add("onmouseout", OnMouseOutScript);
Any thoughts on this? My goal is to get details of gridview row when mouseover a specific column, like the demo on the referenced linked.
Solved by updating AJAX Control Toolkit to it's last version (v15.1 from March 2015)!

How can I perform a double click on an element with Protractor?

I would like to double click on an element but I could not find a way to do this in the document API. I found some references dating back to 2013 but I know things have changed a lot.
Can someone help and tell me how I can perform a double click.
Thanks
Always remember that protractor is a wrapper around webdriverjs.
doubleClick() is available in browser.actions():
browser.actions().doubleClick(element(by.id('mybutton'))).perform();
For anyone looking at this in 2019, this still works. Just know thatProtractor selectors use the Locator object to find elements. The above solution uses the webElement object. So if you're using Protractor to find your element, you'll need to do something like browser.actions().doubleClick(myElement.getWebElement()).perform();
var el=element(by.id('id'));
browser.executeAsyncScript(function() {
var evt=new MouseEvent('dblclick', {bubbles: true,cancelable: true,view: window});
var callback = arguments[arguments.length - 1];
arguments[0].addEventListener('dblclick',callback);
arguments[0].dispatchEvent(evt);
},el).then(function(){...});
await browser.actions().mouseMove(Element).doubleClick().perform();
await browser.actions().doubleClick(Element.getWebElement()).perform();
the above 2 codes works properly to double click on any element when it's visible on screen.
Here Element is
"let Element = element(by.xpath("locator"));"
Below code did not works as a msg is shown saying
"Failed: JavaScript error: arguments[0].dblclick is not a function"
whereas when checked in console similar scripts did worked to double click the item: "$($x(element(by.xpath("locator")))).dblclick()".
Will update my comment if able to find the exact JavaScript syntax to make below code run.
await browser.executeScript("arguments[0].dblclick();",
Element.getWebElement());

jquery tablesorter access internal config parameter

i'm working since hours on this issue without any success, hope to get some help here!
I use the tablesorter plugin to sort table on client side.
First of all, I set an default sort order and initalize the sorter-plugin:
var defaultSort=[[4,1]];
$("#myTable").tablesorter({sortList:defaultSort});
Which does not work (does not sort) while this works:
$("#myTable").tablesorter({sortList: [[4,1]]});
Can't see a reason for this behavior.
Later, I add columns dynamically by using getJSON. After adding a line, I have to update the tablesorter and order again (because of the new rows). For that: read the current "sortList" and try to apply this list:
$("#myTable").trigger("update");
sortOrder=$("#myTable")[0].config.sortList;
setTimeout('$("#myTable").trigger("sorton",['+sortOrder+']);',1);
But this does not work. There is no ordering.
An console.info(sortOrder) returns 4 instead of [[4,1]].
Why?
But all this code works, if the sortList is hardcoded, like:
$("#myTable").trigger("update");
setTimeout('$("#myTable").trigger("sorton",[[[4,1]]]);',1);
Thank you
try this:
var myTable = $("#myTable");
var sortOrder=myTable.trigger("update")[0].config.sortList;
setTimeout(function(){
myTable.trigger("sorton",[sortOrder]);
},1);
in your 3rd example you wrote
setTimeout('$("#myTable").trigger("sorton",['+sortOrder+']);',1);
sortOrder is having toString() called on it when you stick it in that string. That's at least one of the reasons it's not working.

Categories