Blame view

frontend/modules/map/widgets/search/views/SearchString.php 1.76 KB
d1f8bd40   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  <?php
  use yii\helpers\{
      Html, Url
  };
  use frontend\themes\defaults\assets\AppAsset;
  
  
  $asset = AppAsset::register($this);
  echo Html::beginTag('div', ['class' => 'search-block'])
      . Html::beginForm(Url::to(['/map/find/index']), 'get', ['id' => 'search-form', 'class' => 'search-form'])
      . Html::input('text', 'condition', '', [
          'class' => 'search-input', 'autocomplete' => 'off', 'id' => 'search-input', 'palceholder' => 'пошук'
      ])
      . Html::button('', ['class' => 'search-input-btn', 'type' => 'submit'])
      . Html::tag('ul', false, ['class' => 'res-search'])
      . Html::endForm()
      . Html::endTag('div');
  
  $url = Url::toRoute('/map/find/ajax');
  $token = Yii::$app->request->getCsrfToken();
  $script = <<< JS
  
   // $('#search-query').bind("keypress", function(e) {
   //     if (e.keyCode == 13) {
   //         e.preventDefault();
   //
   //         var condition = $("#search-query").val();
   //         condition = condition.replace(/\ /g, '_');
   //         window.location.href = "/find/" + condition;
   //         return false;
   //     }
   // });
  
      $('#search-input').keyup(function(e) {
        if ($("#search-input").val().trim().length > 1){
            $.post({
                  url: '$url',
                  type: 'post',
                  data: {
                      condition: $("#search-input").val().trim() ,
                      _csrf : '$token'
                  }, success: function (data) {
                      $('.res-search').html(data.result);
                  }
              });
        }
        else{
          $('.res-search').html('');
        }  
      });
      $('#search-form').on('submit', function (e) {
          if ($("#search-input").val().trim().length < 1){
               e.preventDefault();
          }
      });
      
  JS;
  $this->registerJs($script, yii\web\View::POS_READY);