Guest User

Untitled

a guest
May 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <?=
  2. $form->field($model, 'address')->widget(Select2::className(), [
  3. 'options' => ['placeholder' => 'Inserta an address '],
  4. 'pluginOptions' => [
  5. 'allowClear' => true,
  6. 'minimumInputLength' => 3,
  7. 'ajax' => [
  8. 'url' => Url::to(['register/addresses']),
  9. 'dataType' => 'json',
  10. 'data' => new JsExpression('function(params) { return {q:params.term}; }')
  11. ],
  12. 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
  13. 'templateResult' => new JsExpression('function(address) { return address.name; }'),
  14. 'templateSelection' => new JsExpression('function (address) { return address.name; }'),
  15. ],
  16. 'pluginEvents' => [
  17. "select2:select" => "function(e) {
  18. // some function
  19.  
  20. }",
  21. ],
  22. ]);
  23. ?>
  24.  
  25. 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
  26. 'templateResult' => new JsExpression('function(address) { return address.name; }'),
  27. 'templateSelection' => new JsExpression('function (address) { return address.name; }'),
  28.  
  29. public function actionAddresses($q = NULL, $id = NULL)
  30. {
  31.  
  32. Yii::$app->response->format = Response::FORMAT_JSON;
  33.  
  34. $results = array();
  35. if (!is_null($q)) {
  36.  
  37. $geocoder = new GeocodingClient();
  38. $request = $geocoder->lookup(['address' => $q]);
  39. $counter = 1;
  40. foreach ($request['results'] as $key => $value) {
  41. $results['results'][] = [
  42. 'id' => $counter,
  43. 'text' => $value['formatted_address'], // Instead of 'name'
  44. 'coordinate' => $value['geometry']['location']
  45. ];
  46. $counter++;
  47. }
  48. }
  49. return $results;
  50. }
  51.  
  52. <?=
  53. $form->field($model, 'address')->widget(Select2::className(), [
  54. 'initValueText' => $model->address,
  55. 'value' => $model->address,
  56. 'options' => ['placeholder' => 'Inserta an address '],
  57. 'pluginOptions' => [
  58. 'allowClear' => true,
  59. 'minimumInputLength' => 3,
  60. 'ajax' => [
  61. 'url' => Url::to(['register/addresses']),
  62. 'dataType' => 'json',
  63. 'data' => new JsExpression('function(params) { return {q:params.term}; }')
  64. ],
  65. 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
  66. 'templateResult' => new JsExpression('function(address) { return address.name; }'),
  67. 'templateSelection' => new JsExpression('function (address) { return address.name; }'),
  68. ],
  69. 'pluginEvents' => [
  70. "select2:select" => "function(e) {
  71. // some function
  72.  
  73. }",
  74. ],
  75. ]);
  76. ?>
  77.  
  78. <?php
  79. $product = $model->product_id ? Product::findOne($model->product_id)->name : 'select ....';
  80. echo $form->field($model, 'product_id')->widget(Select2::classname(), [
  81. 'initValueText' => $product, // set the initial display text
  82. // 'options' => ['placeholder' => 'select ....'],
  83. 'pluginOptions' => [
  84. 'allowClear' => true,
  85. 'minimumInputLength' => 3,
  86. 'ajax' => [
  87. 'url' => Url::to(['list']),
  88. 'dataType' => 'json',
  89. 'data' => new JsExpression('function(params) { return {q:params.term}; }')
  90. ],
  91. 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
  92. 'templateResult' => new JsExpression('function(product_id) { return product_id.name; }'),
  93. 'templateSelection' => new JsExpression('function (product_id) { return product_id.name; }'),
  94. ],
  95. ]);?>
  96.  
  97. $model->id = 1; // or if arrays [1,3,4,5]
  98. $username = User::find()->select('name')->where('id' => $model->id)->scalar();
  99.  
  100. $form->field($model, 'username')->widget(Select2::className(), [
  101. 'initValueText' => $username;
  102. ...
Add Comment
Please, Sign In to add comment