Display search results in a modal search box

I had one search box inside inside the modal body. I created one search option for modal i, but the result was also outside the modal search, but failed to display any ideas inside the modal search results?

My Modal Code:

<div id="myModal" class="modal fade" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <div id="job-title-select2" style="display:none;">
                    <div class="form-group">
                        <input type="text" id="jobsearch" name="jobsearch" class="form-control" value="" placeholder="Enter Job Title....">
                        <input type="submit" id="title-button" class="btn btn-info btn" value="Confirm">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Jquery code:

$("#jobsearch").autocomplete({
        source: function(request, response) {
                $.ajax({
                    url: "{{route('searchexistingjob')}}",
                    dataType: "json",
                    data: {term : request.term},
                    success: function(data) {
                         /////
                    }
                });
            },
        minLength: 3,
});

output to the console:

[{id: 6, value: "Analytical thinker"}]

Controller return value:

array (

0 => array ('id' => 6, 'value' => 'Analytical thinker',),)

so i need to display the value.

+4
source share
3 answers

Assuming your server side data will look like

$data = array (array ( 'id' => 6, 'value' => 'Analytical thinker'));
echo json_encode($data);

And in your ajax success, pass your data back to the ajax response sourceas shown below.

response(data);

, ajax ,

$.ajax({
    url: "{{route('searchexistingjob')}}",
    dataType: "json",
    data: {term : request.term},
    success: function(data) {
        response(data);
    }
});

: , , z-index . ,

.ui-autocomplete {
    position:absolute;
    cursor:default;
    z-index:4000 !important
}
+1

$(function(){
  $("#myModal").modal('show');
});

var availableTags = [
      {"id":1,"value":"ActionScript"},
      {"id":2,"value":"AppleScript"},
      {"id":3,"value":"Asp"},
      {"id":4,"value":"BASIC"},
      {"id":5,"value":"C"},
      {"id":6,"value":"C++"},
      {"id":7,"value":"Clojure"},
      {"id":8,"value":"COBOL"},
      {"id":9,"value":"ColdFusion"},
      {"id":10,"value":"Erlang"},
      {"id":11,"value":"Fortran"}
  ];
	$("#jobsearch").autocomplete({
		source:availableTags
	});
.ui-autocomplete-input {
  z-index: 1511;
}
.ui-autocomplete {
  z-index: 1051 !important;
}
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>

<script
  src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"
  integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E="
  crossorigin="anonymous"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<link href='https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css' rel='stylesheet'/>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div id="myModal" class="modal fade" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <div id="job-title-select2">
                    <div class="form-group">
                        <input type="text" id="jobsearch" name="jobsearch" class="form-control" value="" placeholder="Enter Job Title....">
                        <input type="submit" id="title-button" class="btn btn-info btn" value="Confirm">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
Hide result

ajax

$('#populate-dropdown').autocomplete({
    source: "{{route('searchexistingjob')}}",
})

, JSON.

0

use the code below in your success function

    obj = JSON.parse(data);

    $("#jobsearch").val(obj.value);
    $("#job-title-select2").show();
    $('#myModal').modal('show'); 
-1
source

Source: https://habr.com/ru/post/1691098/


All Articles