Skip to content

Commit 0810e7b

Browse files
committed
UITable: added a utility function that converts cell [r,c] to JS ids.
This was tested on tested on R2018a. The code below demonstrates what the new function does: ```matlab % Create figure: uiFig = uifigure('Position', [100 100 800 130]); mlapptools.setTimeout(uiFig,15); hT(1) = uitable(uiFig,'Data', magic(3), 'Position', [020 020 300 80]); hT(2) = uitable(uiFig,'Data', magic(4), 'Position', [350 010 400 100]); % Get IDs: ID1 = mlapptools.getTableCellID(hT(1), 1:3, 1:3 ); ID2 = mlapptools.getTableCellID(hT(2), 1:4, 3*ones(1,4)); pause(5); % Wait to ensure tables are ready. % Apply styles: hWW = mlapptools.getWebWindow(uiFig); arrayfun(@(x)mlapptools.setStyle(hWW, 'background', 'red', x), ID1); arrayfun(@(x)mlapptools.setStyle(hWW, 'background', 'yellow', x), ID2); ```
1 parent d021c62 commit 0810e7b

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

mlapptools.m

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,50 @@ function fontWeight(uiElement, weight)
171171
% "Clear" the temporary JS variable
172172
win.executeJS('W = undefined');
173173
end % getParentNodeID
174+
175+
function [ID_obj] = getTableCellID(hTable, r, c)
176+
% This method returns one or more ID objects, corresponding to specific cells in a
177+
% uitable, as defined by the cells' row and column indices.
178+
%% Constants:
179+
TABLE_CLASS_NAME = 'matlab.ui.control.Table';
180+
CELL_ID_PREFIX = {'variableeditor_views_editors_UITableEditor_'};
181+
%% Input tests:
182+
assert( isa(hTable, TABLE_CLASS_NAME) && ishandle(hTable), 'Invalid uitable handle!');
183+
nR = numel(r); nC = numel(c);
184+
assert( nR == nC, 'The number of elements in r and c must be the same!');
185+
sz = size(hTable.Data);
186+
assert( all(r <= sz(1)), 'One or more requested rows are out-of-bounds!');
187+
assert( all(c <= sz(2)), 'One or more requested columns are out-of-bounds!');
188+
%% Computing the offset
189+
% If there's more than one uitable in the figure, IDs are assigned
190+
% consecutively. For example, in the case of a 3x3 and 4x4 arrays,
191+
% where the smaller table was created first, IDs will assigned as follows:
192+
%
193+
% [ 0 1 2 [ 9 10 11 12
194+
% 3 4 5 13 14 15 16
195+
% 6 7 8] 17 18 19 20
196+
% 21 22 23 24]
197+
%
198+
% ... which is why if want to change the 2nd table we need to offset the
199+
% IDs by the total amount of elements in all preceding arrays, which is 9.
200+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201+
% Get siblings (flipping to negate that newer children appear first)
202+
hC = flip(hTable.Parent.Children);
203+
% Find which of them is a table:
204+
hC = hC( arrayfun(@(x)isa(x, TABLE_CLASS_NAME), hC) );
205+
% Find the position of the current table in the list, and count the elements of
206+
% all preceding tables:
207+
offset = sum(arrayfun(@(x)numel(x.Data), hC(1:find(hC == hTable)-1)));
208+
% Compute indices, r and c are reversed due to row-major ordering of the IDs
209+
idx = sub2ind(sz, c, r) + offset - 1; % -1 because JS IDs are 0-based
210+
idc = strcat(CELL_ID_PREFIX, num2str(idx(:)));
211+
% Preallocation:
212+
ID_obj(nR,1) = WidgetID;
213+
% ID array population:
214+
for indI = 1:numel(idx)
215+
ID_obj(indI) = WidgetID(mlapptools.DEF_ID_ATTRIBUTE, idc{indI} );
216+
end
217+
end % getTableCellID
174218

175219
function [win, widgetID] = getWebElements(uiElement)
176220
% A method for obtaining the webwindow handle and the widget ID corresponding
@@ -488,7 +532,7 @@ function unlockUIFig(hUIFig)
488532
ww = arrayfun(@mlapptools.getWebWindow, hUIFigs);
489533
warning(warnState); % Restore warning state
490534
hFig = hFigs(hWebwindow == ww);
491-
end % figFromWebwindow
535+
end % figFromWebwindow
492536

493537
function [ID_obj] = getWidgetID(win, data_tag)
494538
% This method returns a structure containing some uniquely-identifying information
@@ -667,6 +711,12 @@ function waitTillWebwindowLoaded(hWebwindow, hFig)
667711
hWebwindow.executeJS('require(["dojo/ready"], function(ready){});');
668712
end
669713
end % waitTillWebwindowLoaded
714+
715+
function htmlRootPath = getFullPathFromWW(win)
716+
% Get a local href value, e.g. from an included main.css
717+
href = win.executeJS('document.body.getElementsByTagName("link")[0].href');
718+
htmlRootPath = win.executeJS(['var link = document.createElement("a"); link.href = ' href ';']);
719+
end
670720

671721
end % Private Static Methods
672722

0 commit comments

Comments
 (0)