1: <?php
2:
3: /**
4: * Description of Autocomplete
5: *
6: * @author SARTAJ
7: * extra == send extra controls value to server with ajax request for autocomplete.
8: * use comma separated string of id attributes
9: * <input id="txtmodel" runat="server" extra="maker,type" type="text" dfield="txtmodel" class="form-control" path="libpath/comp/bundle/Autocomplete.php" funsetForm="form2" funsetMsgName="Model" funsetMaxLen="20" />
10: * send data from server
11: * $sltmaker = $this->genFormFront->getComponent('sltcar_maker')->value;
12: $s1 = $this->Client->request('term');
13: $res = $this->dbEngine->executeQueryQuick("SELECT txtmodel FROM cartype WHERE sltcar_maker='$sltmaker' AND txtmodel LIKE '%$s1%'");
14: $d1 = array();
15: while($row = mysqli_fetch_assoc($res)){
16: $d1[] = $row['txtmodel'];
17: }
18: $this->genFormFront->txtmodel->sendData($d1);
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: //$this->setHTMLName("");
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: addFileLink($jquerypath.'themes/base/jquery.ui.all.css');
51: addFileLink($jquerypath.'themes/base/jquery.ui.accordion.css');
52: addFileLink($jquerypath.'ui/jquery.ui.core.min.js');
53: addFileLink($jquerypath.'ui/jquery.ui.widget.min.js');
54: addFileLink($jquerypath.'ui/jquery.ui.position.min.js');
55: addFileLink($jquerypath.'ui/jquery.ui.menu.min.js');
56: addFileLink($jquerypath.'ui/jquery.ui.autocomplete.min.js');
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: