Untitled

 avatar
unknown
javascript
2 years ago
2.9 kB
4
Indexable
function initLocationSelect(section, name, placeholder){
	if($('#' + section + '-' + name).length){
		$('#' + section + '-' + name).select2({
	        placeholder: placeholder,
	        minimumInputLength: 3,
	        allowClear: true,
	        ajax: {
	            url: $_base + 'ajax/' + section + 's/location.php',
	            dataType: 'json',
	            type: "GET",
	            delay: 250,
	            data: function (params) {
			      	return {
				        q: params.term,
				        page: params.page,
				        action: 'search'
				      };
			    },
			    processResults: function (data) {
			    	return {
			        	results: data
			    	}
			    }
	        },
	        templateResult: function(object, container) {
	            var text = object.text;
				var term = query.term || '';
				if(object.indent) {
	                return $('<span><i class="zmdi zmdi-long-arrow-right"></i> ' + markMatch(text, term).html() + '</span>');
	            }
	            return markMatch(text, term);
	        },
	       // initSelection: function (element, callback) {
           // 	callback({ id: 1, text: 'Text' });
			//},
			language: {
				inputTooShort: function(args) {
				    // args.minimum is the minimum required length
				    // args.input is the user-typed text
				    return "Introduceti primele 3 litere";
				},
				searching: function (params) {
				  // Intercept the query as it is happening
				  query = params;

				  // Change this to be appropriate for your application
				  return 'Cautare...';
				},
				noResults: function(params){
		           return $('<span id="showParentOnMobile">Nu am gasit rezultate</span>');
		       }
			 }
	    }).on('select2:open', function(){
	    	// provizoriu TODO
	    	var valoare = $(this).val();
	    	//sconsole.log(valoare);
	    	$('.select2-search__field').val(valoare).trigger('keyup').trigger('change').val('');
	    });
	    
	    if($('#' + section + '-' + name).data('selected') != ""){
			$.ajax({
	            url: $_base + 'ajax/' + section + 's/location.php',
	            dataType: 'json',
	            type: "GET",
	            data: {
	                action: 'initSelection',
	                id: $('#' + section + '-' + name).data('selected')
	            }
	        }).done(function(data){
	            var option = new Option(data.text, data.id, true, true);
	            $('#' + section + '-' + name).append(option);
	            $('#' + section + '-' + name).trigger('change');
	        });
	    }
    }
    
   // scroll to elem pe mobil
	if ($(window).width() < 480) {  
	    $('#' + section + '-' + name).on("select2:open", function (e) {
	     	$('html, body').animate({
				scrollTop: $(this).offset().top - 5
			}, 750);
	    });
	}
}

// locations
initLocationSelect('plane', 'from', 'Plecare');
initLocationSelect('plane', 'to', 'Sosire');
Editor is loading...