views:

30

answers:

1

I'm trying to learn how to use jQuery FlexBox. Getting confused:

  1. My understanding is that as the user is typing into the FlexBox, what he types will be sent to the server through ajax. I'm using Django on the server side to do autocomplete, but how do I actually retrieve what the user has typed? Is the query being sent as a parameter? If so, what parameter is it? If anyone could share some code examples on how to do this it'd be greatly appreciated.

  2. How do I specify the name of the FlexBox? In the documentation it is stated: "Add an empty div to the tag wherever you want the FlexBox to appear: <div id="fb"></div>". But I'd need to give that FlexBox a name so that when the form is submitted, I could process the user input in the FlexBox, right? So how do I give each FlexBox a name?

Thanks

+1  A: 

The params being sent look like this:

var params = {
  q: q,
  p: p,
  s: pageSize,
  contentType: 'application/json; charset=utf-8'
}

So you're looking for q to get the query, p to get the page number and s to get the page size.


For the second question:
You can give it any ID you want, as long as you use the same ID in your selector, for example:

<div id="anything"></div>

And matching jQuery to populate that <div>:

$("#anything").flexbox('results.html');

It'll already generate a hidden input with the a name matching the ID, that is what you're looking for server-side. For example with the above code, this gets generated in the DOM immediately before that <div id="anything"></div>:

<input type="hidden" id="anything_hidden" name="anything" value="selectedVal" />
Nick Craver
Thank you. What about autocomplete? If the user type "au" in the FlexBox, how do I retrieve that on the server side? Is "au" being sent as "q" or some other parameter?
Continuation
@Continuation - Yup, it's being sent as `q`, so just look for that on the request.
Nick Craver
Ah just saw your edited answer. Thank you very much!
Continuation
@Continuation - welcome!
Nick Craver
@Nick - say my list of autocomplete suggestions is really long, do I need to send them all to the browser, or can I just send the first 10 (or whatever the page size is)? And if I can send my results page by page, how do I tell FlexBox how many total results are there? And can I *not* tell FlexBox the total number of results?
Continuation
@Continuation - Yes you can send as many as you want, when calling flexbox set the page size like this: `.flexbox({ paging: { pageSize: 10 } });` (see full paging options at the bottom of the flexbox homepage). You don't need to tell it how many, just have your results server side return `s` results starting on the `p` page, so for example if `s` is 10 and `p` is 2, send results 11-20 (or 11-whatever), make sense?
Nick Craver
@Nick - Thanks again. I think I understand. But I'm still somewhat confused. Say I have a total of 59 autocomplete results, and I want to do the "Displaying 1-10 of 59 results" I saw on the demo. One of the paging options is "summaryTemplate: 'Displaying {start}-{end} of {total} results'". I'm not sure how to use that. Should I pass 59 as "total" to FlexBox? If so how exactly do I do that?
Continuation