2

This is a small part of the JSON that i use (which has the same structure)..so i need to get all the ship names from GOD and LEG and use them at JQuery autocomplete

{
  "GOD": {
  "name": "This is a test",
  "code": "GOD",
        "ships": [
            {
                "layout": "normal",
                "type": "Destroyer",
                "name": "Ship George"
            },
            {
                "layout": "normal",
                "type": "Airship",
                "name": "The strong one"
            }
        ]
    },
    "LEG": {
        "name": "Limited God",
        "code": "LEG",
        "ships": [
            {
                "layout": "normal",
                "type": "bad",
                "name": "Blair witch"
            },
            {
                "layout": "normal",
                "type": "the worst",
                "name": "New era"
            }
        ]
    }
}

The problem is that i want to display in autocomplete only the "ships" names. The code that i use for autocomplete is :

$("#autocomplete").autocomplete({
    source: function (request, response) {
        $.ajax({
            dataType: "json",
            data: {
                term: request.term,
            },
            type: 'Get',
            contentType: 'application/json; charset=utf-8',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            cache: true,
            url: 'all.json',
            success: function (data) {
                var array = $.map(data, function (set) {
                    return {
                        label: set.name,
                        value: set.name
                    }
                });

                //call the filter here
                response($.ui.autocomplete.filter(array, request.term));
            },
            error: function (data) {
            }
        });
    },
    minLength: 2,
    open: function () { },
    close: function () { },
    focus: function (event, ui) { },
    select: function (event, ui) {
        $( "#card" ).val( ui.item.label );
        //$( "#description" ).html( ui.item.text );
        $( "#multiverseid" ).val( ui.item.multi );
        $( "#project-icon" ).attr( "src", "http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=" + ui.item.multi + "&type=card" );
    }
});

With that code above i get as autocomplete the values of : GOD and the LEG

if i change the line to :

var array = $.map(data.GOD.ships, function (set) {

then i get all the ships specifically of the GOD

What i need is to get autocomplete suggestions with all the ship names of both GOD and LEG (and others "GODS" and "LEGS" that there are)

7
  • just a thought, what if you put data.GOD.ships and then in the same array data.LEG.ships that way array will be populated with both value.... Commented Oct 13, 2015 at 12:37
  • The thought is not bad, but how will i implement that inside JQuery autocomplete, using $.map or other function... Commented Oct 13, 2015 at 12:39
  • what value request.term holds? Commented Oct 13, 2015 at 12:40
  • It's the value in the text field (while user typing, autocomplete functions)... Commented Oct 13, 2015 at 12:43
  • I am putting code which is just to let you know what I am thinking, its not tested you can try. I will remove it as an answer. But I am sure there will be a better way. Commented Oct 13, 2015 at 12:45

1 Answer 1

1

Success could be like this :

  var array = $.map(data.GOD.ships, function (set) {
                        return {
                            label: set.name,
                            value: set.name
                        }
                    });
                 var   array1 = $.map(data.LEG.ships, function (set) {
                        return {
                            label: set.name,
                            value: set.name
                        }
                    });
              var outputArray = $.merge(array, array1);                
             console.log(outputArray)
            //call the filter here
            response($.ui.autocomplete.filter(outputArray, request.term));
        },
        error: function (data) {
        }

EDIT 1:

var keys = Object.getOwnPropertyNames ( data )
var outputArray;

$.each(keys,function(ele, val){

    var array = $.map(data[val].ships, function (set) {
                        return {
                            label: set.name,
                            value: set.name
                        }
                    });     
      if(ele == 0)
           outputArray = $.merge([], array);
      else{
           outputArray = $.merge(outputArray, array);
       }  
});
response($.ui.autocomplete.filter(outputArray, request.term));
},
error: function (data) {
}

EDIT 2:

var keys = Object.getOwnPropertyNames ( data )
var outputArray;

$.each(keys,function(ele, val){

    var array = $.map(data[val].ships, function (set) {
                        return {
                          label: set.name + "(" +keys[ele] + ")",
                        value: set.name + "(" +keys[ele] + ")"
                        }
                    });     
      if(ele == 0)
           outputArray = $.merge([], array);
      else{
           outputArray = $.merge(outputArray, array);
       }  
});
response($.ui.autocomplete.filter(outputArray, request.term));
},
error: function (data) {
}
Sign up to request clarification or add additional context in comments.

11 Comments

works...but now i need to create these arrays and merge them somehow automatically! (as they are hundred and not only 2...)!! Thank you
do u mean to say there are more keys than just GOD and LEG? or does GOD and LEG have more children's?
There are more GOD and LEG (about a hundred and more) that have ships inside. So we need for example a loop that will grab this automatically through the json file and create array1,array2,array3 and then merge them with the way above
need to see the JSON sample could you post a sample; not complete but at least to cover the entire hierarchy?
Excellent mate! Thank youuuu, i will vote up your solution when i go reputation 15, you are just amazing!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.