So I am wanting to read a database, and then form a tree structure and put it in my webpage. I am using the destroydrop tree at the moment, and I can get it to work on its own, but if I want to build the tree and then put it in my webpage, then my page gets overwritten because it uses document.write(tree) to create the tree. I've also tried some other trees which all have the same issue. Anyone know of a tree structure that I can dynamically add to my page without overwriting what is on there? Thanks!
You can try nitobi tree. Nitobi Completeui framework has both client and server sides.
You can find samples in code repository.
Thanks much for the help. I ended up just overriding document.write as such
document.write = function(){
document.getElementById("MyDiv").innerHTML = arguments[0];
}
function getTreeStruct() {
new Ajax.Request('MainServlet', {
method: 'POST',
parameters: "action=getTreeStruct",
onSuccess: function(transport) {
d = new dTree('d');
d.add(0, -1, 'Root Element');
//contains data queried from database to be inserted into the tree.
var responseArray = transport.responseText.split("|");
//Add each element to the tree object
iterate1DArray(function(x) {
var tempArray = x.split(",");
if(tempArray[1] != undefined
&& !(tempArray[0] == 0 && tempArray[1] ==0)){
d.add(tempArray[0], tempArray[1], tempArray[2]);
}
}, responseArray);
document.write(d);
}
});
}
function iterate1DArray(func, array) {
for( var i = 0; i < array.length; i++){
array[i] = func(array[i]);
}
}
I did not include the code for the tree, but it can be found here. Just thought I would put this on here in case anyone else has this type of issue. Thanks again for all your help!
Related
So I have this dom repeat where all lessons get displayed of the class of an user which I get from a json.
<template is="dom-repeat" items="{{lessen}}">
<div><paper-button id="{{index}}" on-click="toggleDialog">{{index}} - {{item.lessen}}</paper-button></div>
</template>
Is there a way I can get the value of '{{index}}' to send to my backend via iron-ajax? I got this function:
sendAfmelding: function() {
console.log("Afmelden van les met user="+this.username);
if (this.rol == "student") {
console.log("indexnummer = "+this.$.index);
this.$.ajax2.contentType="application/json";
this.$.ajax2.body={
"username":this.username,
"lesIndex":this.$.index // <-- needs to change
};
this.$.ajax2.generateRequest();
}
}
I got everything correctly set up. Username get's send and everything, the only thing I still need is that index value. For example, if I put
"lesIndex":"1"
in my function my code will do exacly what I want but ofcourse I want the user to select the lesson himself.
I hope my question is a bit clear, thanks in advance!
I think
id$="{{index}}"
is what you want to get attribute binding instead of property binding.
See for example the 2nd-last paragraph before https://www.polymer-project.org/1.0/docs/devguide/templates.html#dom-bind
The index is exposed on the item's click event, you can access it inside toggleDialog:
toggleDialog: function (event) {
var index = event.model.index;
sendAfmelding(index); // I assumed you were calling this somewhere in here
}
And then ofcourse add the parameter to the sendAfmelding function:
sendAfmelding: function (index) {
console.log("Afmelden van les met user=" + this.username);
if (this.rol == "student") {
console.log("indexnummer = " + index);
this.$.ajax2.contentType = "application/json";
this.$.ajax2.body = {
"username": this.username,
"lesIndex": index
};
this.$.ajax2.generateRequest();
}
}
I seem to be having an issue with not properly syntaxing my code, but as I've just started out with learning I seem to be missing the error. It's a homework assignment, where I need to use an Array of JxploreFile-objects. This is the part of the code I'm having trouble with:
private JxploreFile[] getSubFolders()
{
File subFiles[];
subFiles = file.listFiles();
File subFolders[];
int p = 0;
for(int i = 0; i < subFiles.length; i++)
{
if(subFiles[i].isDirectory() == true)
{
Array.set(subFolders, p, subFiles[i]);
}
}
JxploreFile foldersToReturn[] = new JxploreFile[subFolders.length];
for(int i=0; i < subFolders.length; i++)
{
foldersToReturn[i] = new JxploreFile(subFolders[i]);
}
return foldersToReturn;
}
Specifically, the for-loop where I'm trying to add the files marked as .isDirectory into a new Array. I've also tried other methods by placing each new file coming from the subFiles Array manually into the subFolders Array by declaring indexnumbers, but this also turned out faulty. At this point I'm out of ideas and I hope there is someone who can point me out the obvious, as I'm probably missing something reallly basic.
Edit:
I'm sorry for the incomplete post, It's the first time I actually post here as I usually try to filter my own problems out of the posts of others. The error I got was indeed that 'subFolders' had not been initialized yet, which I didn't understood because on the sixth line I wrote
File subFolders[];
which as far as I know should declare the variable subFolders to become an Array, or is this where I went wrong?
Also, my question might not have been specific enough, I'm looking for what causes the error (which I didn't mention at all): why 'subFiles' wasn't initialized.
The array subFolders has not been initialized properly. In order to use the array in the Array.set method it must be initialized and allocated with a size.
An alternative approach for this is to use a List instead. Lists are good when you are working with data that is more dynamic e.g. when you do not know the size of the array. Then you can simplify your code like this:
File[] subFiles = file.listFiles();
// Create the list
List<JxploreFile> subFolders = new ArrayList<>();
// Add all the sub folders (note that "file" is a bit magic since it
// is not specified anywhere in the original post
for (File subFile : file.listFiles()) {
if (subFile.isDirectory()) {
subFolders.add(new JxploreFile(subFile));
}
}
// Return an array
return subFolders.toArray(new JxploreFile[subFolders.size()]);
You can also simplify the whole thing even further by using a Java 8 stream like this:
return Arrays.stream(file.listFiles())
.filter(File::isDirectory)
.toArray(JxploreFile[]::new);
For more info:
Creating Objects in Java
Arrays
There is no question in your post, but anyway here the problems I found in your code :
File subFolders[];
int p = 0;
for(int i = 0; i < subFiles.length; i++)
{
if(subFiles[i].isDirectory() == true)
{
Array.set(subFolders, p, subFiles[i]);
}
}
when calling Array.set you never initialized subFolders which will throw a NullPointerException.
Also, you dont need to do
if(subFiles[i].isDirectory() == true)
you can simply do
if(subFiles[i].isDirectory())
as subFiles[i].isDirectory() is already a condition.
I get the values from database to time array as follow
int[] time=Manager.playTime() ;
responsedata.put("status", "success");
responsedata.put("play", time);
And i am sending this time array to ajax in javascript file as follow
success:function(response){
$('body').css('cursor', 'default');
if(response.status == 'success'){
for( var i=0;i<response.play.length;i++){
alert("playtime---"+response.play[i]);
}
but here i am not getting the values from array .Please help me
Thanks
You responsedata seems to be a Java Map.
Try to use this in your success function
success:function(response){
$('body').css('cursor', 'default');
if(response.status == 'success'){
$.each(response.play, function (value) {
alert("playtime---"+value);
});
}
}
In your ajax you are getting whole object in response i.e. response.
You can get your play object by response i.e. response.play.
now iterate the the values from play object.
for (var i = 0; i < response.play.length; i++) {
//iterate your values here
alert(response.play[i].object_key);
//object_key is temparary name of your play object so give a proper key here.
}
I have an array which holds links. Each link has a public field called next which can hold another link, which can hold any more links ect. When I want to delete things I can do
array[x].next = array[x].next.next;
which would delete the 2nd item. But I want to be able to delete any item, so how can I get it into a form with variables? The equation would be something along these lines: If I wanna delete item n I would do
array[x](.next*(n-1)) = array[x](.next*n);
Which if n = 4 I want to exand to
array[x].next.next.next = array[x].next.next.next.next;
Hopefully my question is clear. I need to know how to do it this way as I cannot set a getter or any other code into my link class, and since Im the sole owner of my code I am not going to incorectly set my the field. Java.
As mentioned in other answers, what you're implementing is a linked list and you'd probably be best served to leverage Java's libraries.
To address the question as you posed it, a function that gives you a reference to the nth hop is probably what you're asking for
Link getLink(Link link, int hops){
Link retVal = link;
for (int i = 0; i < hops; i++){
if (link == null){
//hops is too large. Do something to indicate error
}
else{
retVal = retVal.next;
}
}
return retVal;
}
Then you could execute
getLink(array[x],2).next = getLink(array[x],4);
Or if you wanted to use reflection (not recommended since reflection is relatively expensive), then you could do something like
Link myLink = array[x];
Field next = Link.class.getField("next");
//assume proper error handling
for (int i = 0; i < hops; i++){
myLink = (Link) next.getObject(myLink);
//assume proper error handling
}
Seems like what you have there is an array of linked lists. I don't know why you're worrying about this fancy syntax expansion stuff when what would work just fine is a method that deletes the nth node, and there are tons of implementations floating around the internet.
In fact, you can look at the source for java.util.LinkedList and copy that implementation yourself, with simple modifications if you have a singly linked list. Basic formula is:
Iterate from the first node, repeatedly finding the next node until the nth node is reached
Unlink that node from the chain
Use a simple loop to step along n times. Here's some code that deletes the nth:
Link link = array[x], prev = link;
int n = ?;
for (int i = 0; i < n; i++) {
prev = link;
link = link.next;
}
prev.link = link.next;
I'm writing an XML serializer with JAXP. I'm receiving pseudo random data from a JAR and I'm building the DOM tree. I have to check if I already inserted the same Element into the tree; in order to perform this control I'm trying to use the method:
Element e = myDocument.getElementById(ao.getId());
if (e == null) {
// element is not a duplicate
access.appendChild(authorizationObject);
}else{
// element already in the tree
}
So, in every Element I create before adding them to the tree I set:
ao = a.getAuthorizationObject();
authorizationObject = myDocument.createElement("authorizationobject");
authorizationObject.setAttribute("id", ao.getId());
authorizationObject.setIdAttribute("id", true);
It can happen that in the object ao sometimes I get the same element twice (or more).
The problem is that the program always enter inside the if instruction.
You can find all the program's code here and the DTD here for your reference.
What am I doing wrong?
Thanks in advance for all your reply.
You have forgotten to append the authorizationObject to the access Element. Your code should be as follows
authorizationObject = myDocument.createElement("authorizationobject");
authorizationObject.setAttribute("id", ao.getId());
authorizationObject.setIdAttribute("id", true);
System.out.println("AO.ID = " + ao.getId());
access.appendChild(authorizationObject);
// then only this Element will be appended to the document
if (myDocument.getElementById(ao.getId()) == null ) {
I see that you have finally appended the authorization object to the document. but, it should be done prior to document.getElementById() method call
Hope this helps!