How to use complex json as source for autocomplete
I have a fairly complex json object and I would like to combine arrays of names as source for an autocomplete. My situation is similar to that of a school, so to make it simple I'll make a structure similar to that of classes in a school. I want to get teachers names as source for this input. Any ideas on how to go about doing this? Here is my example:
- <script type="text/javascript">
- var mynames = { "schoolyear": [
- {"year":1, "group": [
- {"value":1, "schoolday":"Tuesday", "teachers_names": {"value":["Mr. A"]} },
- {"value":2, "schoolday":"Saturday", "teachers_names": {"value":["Mrs. B"]} },
- {"value":3, "schoolday":"Saturday", "teachers_names": {"value":["Mr. C","Mrs. D"]} }
- ] },
- {"year":2, "group": [
- {"value":1, "schoolday":"Tuesday", "teachers_names": {"value":["Mr. E"]} },
- {"value":2, "schoolday":"Tuesday", "teachers_names": {"value":["Mrs. F","Mr. G"]} },
- {"value":3, "schoolday":"Saturday", "teachers_names": {"value":["Mrs. H","Mr. I"]} }
- ] },
- {"year":3, "group": [
- {"value":1, "schoolday":"Saturday", "teachers_names": {"value":["Mrs. J"]} },
- {"value":2, "schoolday":"Tuesday", "teachers_names": {"value":["Mr. K","Mrs. L"]} }
- ] },
- {"year":4, "group": [
- {"value":1, "schoolday":"Saturday", "teachers_names": {"value":["Mr. M","Mrs. N","Mr. O"]} }
- ] },
- {"year":5, "group": [
- {"value":1, "schoolday":"Thursday", "teachers_names": {"value":["Mrs. P","Mr. Q","Mrs. R","Mrs. S","Mr. T"]} }
- ] },
- {"year":6, "group": [
- {"value":1, "schoolday":"Saturday", "teachers_names": {"value":["Mrs. U","Mr. V"]} }
- ] },
- ] }
- $(document).ready(function(){
- $("input#teachernames").autocomplete({
- source: mynames.schoolyear, // how should I access "teachers_names" and build strings from the arrays?
- });
- });
- </script>
- <label for="teachernames">Select teachers:</label><input name="teachernames" id="teachernames" />
Based on the above json object, I would like my autocomplete to look something like this:
┌─────────────────┐
Select teachers: │ M │
_________________└─────────────────┘_____
| Mr. A |
| Mrs. B |
| Mr. C, Mrs. D |
| Mr. E |
| Mrs. F, Mr. G |
| Mrs. H, Mr. I |
| Mrs. J |
| Mr. K, Mrs. L |
| Mr. M, Mrs. N, Mr. O |
| Mrs. P, Mr. Q, Mrs. R, Mrs. S, Mr. T |
| Mrs. U, Mr. V |
-----------------------------------------
Is there any better way of structuring the json object? How would I access such an object to fill the autocomplete with the name arrays?
John R. D'Orazio