| 1: | <?php
|
| 2: |
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: |
|
| 20: | include_once(SphpBase::sphp_settings()->slib_path . "/comp/bs/form/TextField.php");
|
| 21: | class Autocomplete extends \Sphp\comp\form\TextField {
|
| 22: |
|
| 23: | private $url = "";
|
| 24: | private $synccomp = '';
|
| 25: | private $minlen2 = 2;
|
| 26: | public $valuehid = "";
|
| 27: |
|
| 28: | protected function oninit() {
|
| 29: | parent::oninit();
|
| 30: |
|
| 31: | $this->url = getEventURL($this->name . '_autocomplete');
|
| 32: | }
|
| 33: |
|
| 34: | public function fu_setURL($val) {
|
| 35: | $this->url = $val;
|
| 36: | }
|
| 37: |
|
| 38: | public function sendData($val) {
|
| 39: | SphpBase::JSServer()->addJSONBlock('js', 'proces', '
|
| 40: | ' . SphpBase::sphp_api()->getJSArray("data", $val) . '
|
| 41: | var term = "' . SphpBase::sphp_request()->post('term') . '" ;
|
| 42: | ' . $this->name . '_cache[term] = data;
|
| 43: | ' . $this->name . '_response(data);
|
| 44: | ');
|
| 45: | }
|
| 46: |
|
| 47: | protected function onjsrender() {
|
| 48: | parent::onjsrender();
|
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: |
|
| 59: | $str1 = '';
|
| 60: | if($this->element->hasAttribute('extra')){
|
| 61: | $extra = explode(',',$this->getAttribute('extra'));
|
| 62: | foreach ($extra as $key => $val) {
|
| 63: | $str1 .= 'request["'. $val .'"] = $("#'. $val .'").val();';
|
| 64: | }
|
| 65: | }
|
| 66: | addHeaderJSFunction($this->name . '_autocomplete', "function " . $this->name . '_autocomplete(request){ ', "
|
| 67: | clearTimeout(this.tmr1); this.tmr1 = setTimeout(function(){
|
| 68: | $str1
|
| 69: | getURL('$this->url',request);
|
| 70: | },800);
|
| 71: | }");
|
| 72: |
|
| 73: | addHeaderJSCode($this->name, ' window["' . $this->name . '_cache"] = {}; window["' . $this->name . '_response"] = null;');
|
| 74: | addHeaderJSFunctionCode('ready', $this->name, '
|
| 75: | $("#' . $this->name . '").autocomplete({
|
| 76: | minLength: ' . $this->minlen2 . ',
|
| 77: | source: function( request, response ) {
|
| 78: | var term = request.term;
|
| 79: | if ( term in ' . $this->name . '_cache ) {
|
| 80: | response(' . $this->name . '_cache[term]);
|
| 81: | return;
|
| 82: | }
|
| 83: | ' . $this->name . '_response = response;
|
| 84: | ' . $this->name . '_autocomplete(request);
|
| 85: |
|
| 86: | },
|
| 87: | select: function(event,ui){
|
| 88: | $("#'. $this->name .'1").val(ui.item.label);
|
| 89: | }
|
| 90: | });
|
| 91: | ');
|
| 92: | if($this->valuehid == '') $this->valuehid = $this->value;
|
| 93: | $this->addPreTag('<input id="'. $this->name .'1" name="'. $this->name .'1" type="hidden" value="'. $this->valuehid .'" />');
|
| 94: | }
|
| 95: |
|
| 96: | }
|
| 97: | |