2014年05月

Detecting size of element before inserting it into DOM

I'm writing (using pure JavaScript = no JQuery) a custom control which is a container that collapses it's contents when they span multiple lines and show only the first one. When you click a button in the control (down arrow), the container expands all of it's content. If content spans only 1 line, I don't show the button at all and it behaves as a regular container.



The 2 pictures below show the control collapsed (upper one) and expanded (bottom one).







The problem I face is how do I know when to show the button and when not = how to detect if content is multiline or spans only a single line. My approach would be getting the height of the element, then collapsing it and seeing if the height changed (if it did, then it must be multiline).



The problem with that approach is inside my function createCollapsingPanel() the element I'm creating hasn't been yet appended to the DOM - the function returns the element, which will only then be appended to DOM (illustrated by Architecture 1 below). If that would concern only that 1 function, I could of course move appending inside the function (Architecture 2 below). Unfortunately that is the way I wrote all of my functions (they return elements, which are only then appended into parent elements in the outer functions that called them) and so that would mean re-writing other parent functions that in the end call this one, in general my whole architecture of functions.





Architecture 1



function createOuterElement() {
var outerElement = document.createElement(...);

outerElement.appendChild(createInnerElement());
/* Do other things with outerElement */

return outerElement;
}

function createInnerElement() {
var innerElement = document.createElement(...);

/* Do things with innerElement */

return innerElement;
}


Architecture 2



function createOuterElement(parentElement) {
var outerElement = document.createElement(...);

parentElement.appendChild(outerElement);
createInnerElement(outerElement);
/* Do other things with outerElement */
}

function createInnerElement(parentElement) {
var innerElement = document.createElement(...);

parentElement.appendChild(innerElement);
/* Do other things with innerElement */
}




Main Question



Any ideas on how to deal with that in my current architecture (Architecture 1)? Maybe there's an event I could attatch to the control element and it would get invoked after the element is inserted into DOM and browser styles are calculated for it?



Additional Question



Is writing functions in Architecture 1 considered a bad practice? I'm pretty new to non-basic JavaScript, so am unsure what good practices are.



Answers

you could always have the inner container be multiline and just clip it with the outer container.



on the outer container use:




overflow: hidden
height:somevalue


on the inner container use:




position: absolute
height: auto


put the span's inside the inner container.



you can now measure the inner containers height independently from the outer ones.



Answers

you can use a hidden container in the dom to measure element sizes. the hidden container can be a simple div in the dom like:



   ...
<body>
<div id="measurementContainer" style="display:none;"></div>


a measuring function could than:




append the element to the measurement container (div)
measure the element
and remove it from the measurement container again


for example:



    function measureElementWidth(element) {
var measurementContainer = document.getElementById(measurementContainer);
measurementContainer.appendChild(element);

var width = element.clientWidth;

measurementContainer.removeChild(element);

return width;
}




alternatively you can use MutationObservers and get notified if an element is added to the dom. check if all needed browsers support this.





or even simpler: just call your sizing function after you append your elements to the dom.





Selecting multiple fields using If condition, with left join in mysql - throwing error

Any help is appreciated --



1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as receiverimage, re.fName as receiverfName , re.lName as receiverlName , re.add' at line

SELECT co.*, 
if(( co.senderid = 1) , ( re.image as receiverimage, re.fName as receiverfName , re.lName as receiverlName , re.address as receiverAddress , re.city as receivercity , re.state as receiverstate, re.country as receivercountry, re.zipCode as receiverzip),(se.address as senderAddress,se.city as sendercity , se.state as senderstate , se.country as sendercountry , se.zipCode as senderzip , se.image as senderimage ,se.fName as senderfName, se.lName as senderlName ))
FROM contacts as co
LEFT JOIN users as se ON( se.id = co.senderid )
LEFT JOIN users as re ON( re.id = co.receiverid )
where (senderid = 1 OR receiverid = 1)


-- is that selecting multiple fields on join table is not valid with or something else ?



I am unable to figure out the exact reason , basic prob. here is to select few fields based on if condition from user table based on join.



Answers

You cannot use aliases in the if statement it seems. I have just tested your query, and removing the aliases, resolves the issue.



if(( co.senderid = 1) , ( re.image, re.fName, re.lName, re.address, re.city, re.state, re.country, re.zipCode),(se.address,se.city, se.state, se.country, se.zipCode, se.image,se.fName, se.lName )) AS mydata



Edit: You should also add an alias to your entire IF so you don't get the expression as a column name :)



Edit 2: Sorry, I wrote too soon. You cannot select multiple rows like this in a select. You will have to write a subquery if you want that data or put an if for every column you need.



IF(co.senderid = 1, re.image, se.address,) as col1
IF(co.senderid = 1, re.fName, se.city) as col2
IF(co.senderid = 1, re.lName, se.state) as col3
IF(co.senderid = 1, re.address, se.country) as col4


And so on... You get the point :)





How can I capture a table row press action in Swift WatchKit?

I currently have a table with some dynamic rows. When I run the probject the rows display on the screen and even have a press animation but xcode won't let me wire up the table row as an IBAction to its controller. I can't use a segue in this instance, it needs to be like a button press but preferably on the whole table rown I'd rather not insert a button into it. Any help appreciated, thanks!



Answers

You want to override the table:didSelectRowAtIndex function. It is a method on WKInterfaceController.



override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {

//Handle row selection
}




Java program for count of number of words in table

I have written Java code that finds the no.of words in the table.



It is working within the loop, but the value becomes 0 once it exits the loop.



Please help me to get correct value.



int f=0;
int fc=0;
Statement sta4=connection.createStatement();
ResultSet rs4=sta4.executeQuery("select * from db1");
while(rs4.next())
{
word[f]=rs4.getString(1).replace("$", "");
System.out.println(" sentece ="+word[f]);
fc= countWords(word[f]);
System.out.println("The sentence has "+ fc + " words");
f++;
}
System.out.println(" count =" + fc);


Answers

int f=0;
int fc=0;
Statement sta4=connection.createStatement();
ResultSet rs4=sta4.executeQuery("select * from db1");
while(rs4.next())
{
word[f]=rs4.getString(1).replace("$", "");
System.out.println(" sentece ="+word[f]);
int fcTemp= countWords(word[f]);
System.out.println("The sentence has "+ fcTemp + " words");
fc += fcTemp;
f++;
}
System.out.println(" count =" + fc);


Use this. you werent adding up fc





General TableView Questions - Swift

So I'm having a little trouble understanding how multiple segue between multiple view controllers (specifically multiple table views) on a general level. Even after looking at responses and trying to search for examples of table views connecting to different table views, I still couldn't find a solid explanation and was wondering if someone could clarify for me. I'm not entirely sure of the whole logic and vertability of what is legal or illegal, so please take my two scenarios with a grain of salt and let me know if I got some assumptions wrong!





I have two potential scenarios trying to achieve different table view cells for each row in the "Table View Controller" - examples of different cells is highlighted in the box in blue on the right.



The first scenario, I was thinking was to make three different types of prototype cell and depending on whether I can check which indexPath the "Table View Controller" was selected, I can create the appropriate cell to make from the "List of Subs View Controller" by using the cell identifiers.



The second, I was planning to make three different and seperate view controllers depicted in the image by "View Controller #1 / #2 / #3" and create one prototype cell that is appropriate to each row. I'm little hesistant about this approach considering that the cells in "Main View Controller" is all the same and it seems a bit redundant to create seperate view controllers. This way, I could tell what each "List of Subs View Controller" cells type would be simply by the segue identifier.



If Scenario #1 is correct, how would I get the cell identifier from the "Table View Controller" when I'm currently viewing "List of Subs View Controller" in Swift?



Thank you in advance!



Answers

As far as I understand, both scenarios would work.




If Scenario #1 is correct, how would I get the cell identifier from the "Table View Controller" when I'm currently viewing "List of Subs View Controller" in Swift?




In your didSelectRowAtIndexPath



func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
...
let cell = tableView.cellForRowAtIndexPath(indexPath)!
let reuseIdentifier = cell.reuseIdentfier
...
}


(Beware, optional handling is not optimal in the code above, I leave this to you)



On the other hand, I am not sure that using the reuseIdnetifier in that way is the best thing to do.



I would represent the cell type in your model, then use that information to decide what to segue to.





↑このページのトップヘ