select 2 jquery example with ajax

Example 1: select2 server side

$('#payment_method').select2({
	placeholder: 'Select a option',
	ajax: {
			url: 'ajax_get_url',
            dataType: 'json',
            type: 'GET',
            processResults({ data }) {
				return {
					results: $.map(data, function (item) {
						return {
							text: item.name,
                            id: item.id,
                            }
						})
					}
				}
			}
		});

Example 2: jquery select2 multiple select all

$("#checkbox").click(function(){
    if($("#checkbox").is(':checked') ){
        $("select > option").prop("selected","selected");
    }else{
        $("select > option").removeAttr("selected");
     }
});

Tags:

Misc Example