I want to style the field and its dropdown of jQuery autocomplete, but the way around is only through the controller I suppose because there, I'm able to concat the variables I need and send it to the jQuery autocomplete on the Blade file. But concatenating only echoes out the variables as a field, I wish to style the output like it is on the image attached to this post. How do I achieve this?
This is the controller:
public function SelectLocationPlaces(Request $userAdressRequest)
{
$term = $userAdressRequest->term;
$data = LocationPlaces::where('name', 'LIKE', '%'.$term.'%')->take(10)->get();
$countries = Country::all();
$countryFlag = CountryFlag::all();
$results = array();
foreach ($data as $key => $value) {
foreach ($countries as $key => $values) {
if($value->country_id === $values->iso_alpha2){
$countrySelected = $values->name;
$selectedCapital = $values->capital;
}
}
foreach ($countryFlag as $key => $valueFlag) {
if($countrySelected === $valueFlag->name){
$countryPerFlag = $valueFlag->flag;
$countryCurrency = $valueFlag->currency;
}
}
$results[] =
**
['value'=>$value->name .$value->timezone_id .$countrySelected, ];
**
}
return Response::json($results);
}
This is the Blade file:
<DIV align="center"><SPAN class="div-header">USER ADDRESS</SPAN></DIV>
<div>{!! Form::text('city_town', Input::old('city_town'), ['class'=>'css-classes', 'placeholder'=>'area/city', 'id'=>'city_town']) !!}</div>
<div>{!! Form::text('state_province_region', Input::old('state_province_region'), ['class'=>'css-classes', 'placeholder'=>'state/province', 'id'=>'state_province_region']) !!}</div>
<script type="text/javascript">
$('#city_town').autocomplete({
source: '{{ url('json/redirects') }}',
minLength: 1,
autofocus: true,
select: function(e, ui){
$('#state_province_region').val(ui.item.state_province_region);
$('#name').val(ui.item.name);
}
});
</script>
Related
My files so far are:
system.xml
<field id="generate_csv" translate="label" type="button" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Generate Categories CSV</label>
<frontend_model>Vendor\Module\Block\Adminhtml\System\Config\Button</frontend_model>
</field>
button.php
<?php
namespace [Vendor]\[Module]\Block\Adminhtml\System\Config;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Data\Form\Element\AbstractElement;
class Button extends Field
{
protected $_template = 'Vendor_Module::system/config/button.phtml';
public function __construct(
Context $context,
array $data = []
){
parent::__construct($context, $data);
}
public function render(AbstractElement $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}
protected function _getElementHtml(AbstractElement $element)
{
return $this->_toHtml();
}
public function getAjaxUrl()
{
return $this->getUrl('[router]/Export/CategoryExport');
}
public function getButtonHtml()
{
$button = $this->getLayout()->createBlock(
'Magento\Backend\Block\Widget\Button'
)->setData(
[
'id' => 'generate_csv',
'label' => __('Generate CSV')
]
);
return $button->toHtml();
}
}
button.phtml
<script>
require([
'jquery',
'prototype'
], function ($) {
$('#generate_csv').click(function () {
var params = {};
new Ajax.Request('<?php echo $block->getAjaxUrl() ?>', {
parameters: params,
loaderArea: false,
asynchronous: true,
onCreate: function () {
$('#custom_button_response_message').text('');
},
onSuccess: function (transport) {
var resultText = '';
if (transport.status > 200) {
resultText = transport.statusText;
} else {
var response = JSON.parse(transport.responseText);
resultText = response.message
}
$('#generate_csv_response_message').text(resultText);
}
});
});
});
</script>
<?php echo $block->getButtonHtml(); ?>
<p>
<span id="generate_csv_response_message"></span>
</p>
controller
<?php
namespace [Vendor]\[Module]\Controller\Adminhtml\Export;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Catalog\Model\CategoryFactory;
class CategoryExport extends Action
{
protected $resultJsonFactory;
/**
* #param Context $context
* #param JsonFactory $resultJsonFactory
*/
public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
Filesystem $filesystem,
DirectoryList $directoryList,
CollectionFactory $categoryCollectionFactory,
CategoryFactory $categoryFactory
){
$this->resultJsonFactory = $resultJsonFactory;
$this->filesystem = $filesystem;
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->categoryFactory = $categoryFactory;
parent::__construct($context);
}
/**
* #return Json
*/
public function execute()
{
$message = 'Success';
$data[] = [
'id' => __('Entity ID'),
'name' => __('Name'),
'level' => __('Level')
];
$categoryCollection = $this->categoryCollectionFactory->create();
$categoryCollection->addAttributeToSelect('*');
$filepath = 'export/customerlist.csv';
$this->directory->create('export');
$stream = $this->directory->openFile($filepath, 'w+');
$stream->lock();
$header = ['Id', 'Name', 'Level'];
$stream->writeCsv($header);
//$categoryArray = array();
foreach ($categoryCollection as $category) {
$data = [];
$data[] = $category->getId();
$data[] = $category->getName();
$data[] = $category->getLevel();
$stream->writeCsv($data);
}
/** #var Json $result */
$result = $this->resultJsonFactory->create();
return $result->setData(['message' => $message]);
}
}
This creates a csv file in var/export directory but I cannot download it
Can anyone help explain how to modify the code so that it will automatically create and down the csv.
I have tried following various examples on the web but none of them have worked.
Please help me out
I'm trying to make a dependend dropDownList in Yii2. I'm trying to use DepDrop Widget, but I can't understand how to edit the code according to my situation. I have 1 model and inside of it I need to make category dropdown list and according to the category_id, the next dropDownList should be Item. (F.e If I select category 1, the Item should be Item1 and so on).
I guess that extension only can do dropdowns of the same model? I'm new to Yii2, so.
My view file:
<div class="site-create">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'code') ?>
<?= $form->field($category, 'id')->dropDownList($category, ['id'=>'category-id']); ?>
<?= $form->field($item, 'subcat')->widget(DepDrop::Item(), [
'options'=>['id'=>'item-id'],
'pluginOptions'=>[
'depends'=>['category-id'],
'placeholder'=>'Select...',
'url'=>Url::to(['/site/subcat'])
]
]); ?>
My $model is different model from $category and $item. I set those variables to use a different model in the action
Here is my action:
public function actionSubcat() {
$category = new Category();
$item = new Item();
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = self::getSubCatList($cat_id);
echo Json::encode(['output'=>$out, 'selected'=>'']);
return $this->render('create', [
'category' => $category,
'item' => $item,
]);
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
}
Now I'm getting the error message that the $category variable is undefined. Could someone explain me what I'm doing wrong?
Actually you dont need any Widgets or Plugins. In yii2 you can easily do Dependent Dropdown list here is example:
echo $form->field($search,'category')->dropDownList(
ArrayHelper::map(
\app\models\Category::find()->all(),
'id','name'
),
[
'prompt' => Yii::t('app','choose_city'),
'onchange'=>'
$.get( "'.Yii::$app->urlManager->createUrl('site/dropdown?id=').'"+$(this).val(), function( data ) {
$( "select#subcat" ).html( data );
})'
]
);
echo $form->field($search, 'sub_category', ['inputOptions'=>['id'=>'subcat',]])->dropDownList([]);
after that you need to create action for returning action. Example:
public function actionDropdown($id)
{
$countPosts = \app\models\SubCategory::find()
->where(['category_id' => $id])
->count();
$posts = \app\models\SubCategory::find()
->where(['category_id' => $id])
->orderBy('name ASC')
->all();
echo "<option value=''>-</option>";
if($countPosts>0){
foreach($posts as $post){
echo "<option value='".$post->id."'>".Yii::t('app',$post->name)."</option>";
}
}
}
thats all.
i suggest using knockoutjs for doing this. i answered a similar question here
knockout example is here (category > product)
html:
<select data-bind="options: data, value: selected, optionsText: 'title', optionsCaption: '---'"></select>
<select data-bind="options: dependent"></select>
javascript:
<script>
function viewmodel() {
var self = this;
this.data = [
{ title: 'title a', items: ['a1', 'a2', 'a3'] },
{ title: 'title b', items: ['b1', 'b2', 'b3'] },
{ title: 'title c', items: ['c1', 'c2', 'c3'] }
];
this.selected = ko.observable();
this.dependent = ko.computed(function() {
return ((self.selected() || {}).items || []);
});
}
$(document).ready(function() {
ko.applyBindings(new viewmodel());
});
</script>
you can still use $form->field, just add 'data-bind' attribute as inputOption.
if you want to use the field's default values, Json::encode those key-value-pairs and create the viewmodel like this:
ko.applyBindings(new viewmodel(<?= Json::encode($data) ?>));
using those parameters is javascript manual labour
<?= $form->field(**$category**, 'id')->dropDownList(**$category_dropdown**, ['id'=>'category-id']); ?>
Both the names are not same:
$category = your model
$category_dropdown = arrayhelper variable in your action
like below
$transactioncategory = ArrayHelper::map(Transactioncategory::find()->all(), 'id', 'transaction_category');
I have this function in Laravel 5.1
public function calcolaKilometri()
{
$partenza = Input::get('partenza');
$destinazione = Input::get('destinazione');
$distanceMatrix = new DistanceMatrix(new Client(), new GuzzleMessageFactory());
$response = $distanceMatrix->process(new DistanceMatrixRequest(
[$partenza],
[$destinazione]
));
foreach ($response->getRows() as $row) {
foreach ($row->getElements() as $element) {
$distance = $element->getDistance();
$text = $distance->text;
$value = $distance->value;
$data = ['text' => $text, 'value' => $value];
return \Response::json($data);
}
}
}
need to return to Ajax JSON data but this function return plain HTML response, because we are in a forech loop. How i can do the trick?
I'm not sure I fully understand what you're saying but I assume you want to return all of the data found when looping through your result set in a single JSON response.
Try something like this:
// Your previous code...
// Initialise a $data array here, that we're going to fill with data
$data = [];
foreach ($response->getRows() as $row) {
foreach ($row->getElements() as $element) {
$distance = $element->getDistance();
$text = $distance->text;
$value = $distance->value;
// Append the new set of data to your array
$data[] = ['text' => $text, 'value' => $value];
}
}
// Return the data as JSON only when we've filled it with everything
return response()->json($data);
Try This...
$json = json_encode($data);
return \Response::json($json);
Solved using sessions. If someone have the same issue:
public function calcolaKilometri()
{
$partenza = Input::get('partenza');
$destinazione = Input::get('destinazione');
$distanceMatrix = new DistanceMatrix(new Client(), new GuzzleMessageFactory());
$response = $distanceMatrix->process(new DistanceMatrixRequest(
[$partenza],
[$destinazione]
));
foreach ($response->getRows() as $row) {
foreach ($row->getElements() as $element) {
$text = $element->getDistance()->getText();
$value = $element->getDistance()->getValue();
\Session::put('testo', $text);
\Session::put('valore', $value);
}
}
// Return the data as JSON only when we've filled it with everything
$testo = \Session::get('testo');
$valore = \Session::get('valore');
$result = ['text' => $testo, 'value' => $valore];
return \Response::json($result);
}
I am returning a JSON multi array from PHP via JQuery Ajax, and want to determine which array contains a value in order to access values in that array (likewise for the other arrays - up to three arrays altogethebeing returned.
$.ajax({
type: "POST",
url: "scripts/get_model_imaging_report.php",
data: {
case_id:caseId,
level:currentLevel
},
dataType: "json",
success: function(returnedData) {
}
});
PHP;
$case = $_POST['case_id'];
$level = $_POST['level'];
$query = "SELECT * FROM model_image_report WHERE case_fk = '$case' AND level = '$level'";
$result = mysql_query($query, $connection) or die(mysql_error());
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = array(
'image_report_type' => $row['image_report_type'],
'subject' => $row['subject'],
'clinical' => $row['clinical'],
'study_type' => $row['study_type'],
'report' => $row['report'],
'comment' => $row['comment'],
'image' => $row['image'],
'image_annotated' => $row['image_annotated']
);
}
echo json_encode($data);
The key I need to check is 'image_report_type' and the values can be 'nuclear', 'radiology' and 'ultrasound'.
Then I need to access values in those arrays where 'image_report_type' == 'nuclear' for example.
The JSON array looks like:
[{"image_report_type":"nuclear","subject":"Brain Scan","clinical":"Clinical","study_type":"nuclear image scan.","report":"Report","comment":"Comment","image":"original_image_case_3_level_1_nuclear.jpg","image_annotated":"annotated_image_case_3_level_1_nuclear.png"},{"image_report_type":"ultrasound","subject":"Brain Scan","clinical":"Clinical","study_type":"Ultrasound image scan.","report":"Report","comment":"Comment","image":"original_image_case_3_level_1_nuclear.jpg","image_annotated":"annotated_image_case_3_level_1_nuclear.png"}]
You can accomplish this with the jQuery each function.
function(returnedData) {
$.each(returnedData, function(i,o) {
if (o.image_report_type == "nuclear") {
//Do Something
}
});
}
Here is a jsFiddle Demonstrating this technique
Try this http://jsfiddle.net/mN4LF/3/
Javascript
function findIndexOf(data, searchField, searchValue){
var result = new Array();
for(var i = 0; i < data.length; i++){
if(data[i][searchField] == searchValue){
result.push(i);
}
}
return result;
}
i would like to output dynamic generated html content instead of the translatable message but i can't make it work:
function custom_logo_module_block_view($delta = '') {
// don't worry about switch($delta) logic
// perform some operations and then display some generated html
// (maybe use the template(...) function)
// works fine but i'd like to print html
$block['content'] = t('No content available.');
return $block;
}
how can i print out generated html into a block?
i can't find any solutions or code examples. i think i might be pointing towards the wrong direction so best practice suggestions are welcome.
function custom_logo_module_block_view($delta = '') {
$block = array();
if ($delta == 'example') {
$block = array(
'subject' => t('Active users list'),
'content' => example_block_content()
);
}
return $block;
}
function example_block_content() {
// Query for active users from database
$users = db_select('users', 'u')
->fields('u', array('uid', 'name'))
->condition('u.status', 1)
->execute()
->fetchAll();
// Prepare items for item list
$items = array();
foreach ($users as $user) {
$items[] = l($user->name, "user/{$user->uid}");
}
$output = t('No active users available.');
if (!empty($items)) {
$output = theme('item_list', array('items' => $items));
}
return $output;
}
Update regarding your comments...
As far as I understand by some result you mean generated data from database. In this case you can try something like this:
function example_block_content() {
// Query for active users from database
$users = db_select('users', 'u')
->fields('u', array('uid', 'name'))
->condition('u.status', 1)
->execute()
->fetchAll();
$output = '';
foreach ($users as $user) {
$output.= '<div>'. $user->name .'</div>';
}
$output = "<div>Hello World". $output ."</div>";
return $output;
}
This will give you the following output:
<div>Hello World
<div>admin</div>
<div>ndrizza</div>
<div>Vlad Stratulat</div>
...
</div>
Or you can try:
function custom_logo_module_block_view($delta = '') {
$block = array();
if ($delta == 'example') {
$block = array(
'subject' => t('Active users list'),
// this will return "Hello World + some result" text inside <div>
'content' => "<div>Hello World + some result</div>"
);
}
return $block;
}
Both of this ways are working but they are not the right ways. The right way to generate content is in my first answer. Read more about theming in Drupal.