Question

I am trying to add a table to a webview in Cocoa using native Objective C.

I created a new cocoa application and added a webview to the window.

-(id) initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self setEditable:YES];
        self.UIDelegate = self;
    }
    return self;
}

-(void) webView:(WebView *)sender mouseDidMoveOverElement:(NSDictionary *)elementInformation modifierFlags:(NSUInteger)modifierFlags {
   // NSLog(@"mouse over works");
}

-(void)webView:(WebView *)webView willPerformDragDestinationAction:(WebDragDestinationAction)action forDraggingInfo:(id<NSDraggingInfo>)draggingInfo
{
    NSLog(@"Dragging also works");
    [self insertTableInWebView];
}

-(void)insertTableInWebView
{
    DOMDocument *domDocument = [[self mainFrame] DOMDocument];
    DOMHTMLElement *tableElement = (DOMHTMLElement *)[domDocument createElement:@"div"];

    NSString *tableString = @"<table> <tr> <td>This is a row </td </tr> </table>";
    tableElement.innerHTML = tableString;

    [self replaceSelectionWithNode:tableElement];
    [self setNeedsDisplay:YES];
}

At the moment, this does not do anything. How do I fix this ?

No correct solution

OTHER TIPS

try something like this

-(void)appendTagToBody:(NSString *)tagName InnerHTML:(NSString *)innerHTML
{


    @try {
        // Gets a list of all <body></body> nodes.

        DOMNodeList *bodyNodeList = [[[self mainFrame] DOMDocument] getElementsByTagName:@"body"];

        // There should be just one in valid HTML, so get the first DOMElement.
        DOMHTMLElement *bodyNode = (DOMHTMLElement *) [bodyNodeList item:0];

        // Create a new element, with a tag name.
        DOMHTMLElement *newNode = (DOMHTMLElement *) [[[self mainFrame] DOMDocument] createElement:tagName];

        // Add the innerHTML for the new element.
        [newNode setInnerHTML:innerHTML];

        // Add the new element to the bodyNode as the last child.
        [bodyNode appendChild:newNode];


        [newNode scrollIntoView:YES];


    }
    @catch (NSException *exception) {

    }
}

also if you wanted to append specific to a div, probably you need to change getElementsByTagName to getElementsById

Its working for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top