How to open HTML Link in uitable directly

12 visualizaciones (últimos 30 días)
Song
Song el 11 de Sept. de 2014
Comentada: Geoff Hayes el 12 de Sept. de 2014
Hello, I try the following code
h = figure;
uitable(h, 'data', {['<html><a href=http://www.google.com">Google</a></html>']})
The link is correctly inserted into uitable, but if I put mouse on the link in table, the cursor doesn't change and click on it doesn't open the browser. It there a simpler way to do this?
Maybe one can implement it in the cellSelectionCallback ... but I'd prefer simple one
thanks

Respuestas (1)

Geoff Hayes
Geoff Hayes el 11 de Sept. de 2014
Song - the uitable just displays string (or numeric) information in the cell. So while you may have formatted with HTML code to look like a link that the user can click on to open a browser, it won't unless you add additional code. As you have indicated, it is in the CellSelectionCallback that you will have to add some code to do this work. Suppose that all of your links are formatted as in the above example
tableHandle = uitable(h, 'data', ...
{['<html><a href=http://www.google.com>Google</a></html>']; ...
['<html><a href=http://www.mathworks.com>Mathworks</a></html>']});
Note the only difference to your code (besides the additional link/cell entry) are that the handle to the uitable is saved to tableHandle and I've removed the double quote after google.com. Set the cell selection callback to the table as
set(tableHandle,'CellSelectionCallback',@tableSelCallback);
and now define the logic for the callback (this code can follow the previous code as is)
function tableSelCallback(hObject,eventData)
% get all links/cells from the table
links = get(hObject,'Data');
% assuming single column so just need the first index to get the
% selected link/cell
selectedLink = links{eventData.Indices(1)};
% build the url - find where in the string we have http
strtHttpIdx = strfind(selectedLink,'http');
% if non-empty index for http, then find where the url end
if ~isempty(strtHttpIdx)
% we know that the > terminates the url so find all indices to this
% character
bracketIdcs = strfind(selectedLink,'>');
% we just want the one index that corresponds to that which is greater
% than the index for http; note how we subtract one since our url does
% not include '>' only the last character before it
endHttpIdx = bracketIdcs(find(bracketIdcs>strtHttpIdx,1))-1;
% open the url in the browser
web(selectedLink(strtHttpIdx:endHttpIdx),'-browser');
end
And that is it. The above code makes assumptions concerning the number of cells in the table (one column only) and the format of the URLs in the cells (http always exists, and the URL is terminated by the '>' bracket).
Try the above and see what happens!
  2 comentarios
Song
Song el 12 de Sept. de 2014
Thanks Geoff for your idea.
This solution is a working one, but I just need more. E.g. clicking on the blank area in table cell can call the function @tableSelCallback, but putting on the mouse onto the html link in cell can change the mouse cursor to a hand and then clicking on it opens the browser.
Geoff Hayes
Geoff Hayes el 12 de Sept. de 2014
Yes, Song, clicking on a blank area in the table cell will call the tableSelCallback function. But because of the code that checks for the presence of the 'http' substring, the remainder of the code will be ignored.
As for changing the mouse cursor to a hand while hovering over the cell item that contains an URL, that should be raised as a separate question.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by