in Cmultiplefileupload widget i want to call my own js function and want to track the id of the widget id
<script>
function inc_id(x)
{
alert(x);
}
</script>
$this->widget('CMultiFileUpload', array(
'name' => 'LeadRecommendations[docs_name][]',
'id'=>'LeadRecommendations_docs_name',
'model'=>$test,
'accept' => 'jpeg|jpg|gif|png|pdf|doc|docx|odt|txt|xlsx|xls|csv|zip', // useful for verifying files
//'duplicate' => 'Duplicate!', // useful, i think
'denied' => 'Invalid file type', // useful, i think
'remove'=>'[x]',
'options'=>array(
'afterFileSelect'=>'function(e, v, m){ inc_id(this.id) }',
),
but it gives the result undefined how could i get he id of this widget in my function
Related
How do you target the sub-fields inside a repeater field, so the JS applies to the sub-field when appending new groups???
Been looking, but I got really lost as to where even to start. And the JavaScript API is falling a little short on explaining how to work with repeaters.
I'm working on a form to add orders for a bakery. The form has a repeater which holds the options for each different bread order. In here the select field for TYPE OF DOUGH does much of the pre-selection for the other fields (size, shape, extras).
I've managed to target the select field of TYPE OF DOUGH to send and modify data through AJAX, but when adding another repeater row the code doesn't work.
Here's my code for php and js.
JS
jQuery(document).ready(function ($) {
// field key for TYPE OF DOUGH select field inside repeater field
var field = acf.getField("field_609b103fcd576");
field.on("change", function (e) {
var value = field.val();
console.log(value);
var data = {
"action": "get_size_options",
"type_of_dough": value,
};
$.ajax({
url: ajax_object.ajax_url,
type: "post",
data: data,
dataType: "json",
success: function (data) {
console.log(data);
},
});
});
});
PHP
// enqueue the scripts
function fw_acf_register_scripts() {
// enqueue js file with ajax functions
wp_enqueue_script(
'acf-ajax-script',
get_template_directory_uri() . '/assets/js/autopopulate.js',
['jquery']
);
wp_localize_script(
'acf-ajax-script',
'ajax_object',
['ajax_url' => admin_url('admin-ajax.php')]
);
}
add_action('acf/input/admin_enqueue_scripts', 'fw_acf_register_scripts');
// ex. function to get options for the other fields
function get_size_options() {
$meta_value = fw_get_term_id($_POST['type_of_dough'], 'dough');
// just a function to get the ID for the taxonomy from the slug,
// so it just returns a number, say 501 which is then used for the term query
$size_options = array();
$terms_args = array(
'taxonomy' => 'size',
'hide_empty' => false,
'meta_key' => 'tax_position',
'orderby' => 'tax_position',
'order' => 'ASC',
);
if ($meta_value_masa) {
$terms_args['meta_query'] = array(
array(
'key' => 'dough',
'value' => $meta_value,
'compare' => 'LIKE',
),
);
}
$terms = get_terms($terms_args);
foreach ($terms as $term) {
$size_options[$term->term_id] = $term->name;
}
wp_send_json($size_options);
die();
}
add_action('wp_ajax_get_size_options', 'get_size_options');
I am new to Yii2 Framework , I have developed a module that will upload and display file uploaded from server using Yii2 advance framework. I completed the function for uploading the file , However. In my other function that need to display the file like pdf file to jquery ui dialog.My problem is I cannot get the value of the button when I click the button to display the pdf file uploaded. Thank you guys in advanced.
// Here is Gridview code in Index where my button Exist.
<?= DynaGrid::widget([
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'ntraining_id',
'ctraining_description',
//'dtraining_datefrom',
[
'attribute'=>'dtraining_datefrom',
'filterType'=>GridView::FILTER_DATE,
'format'=>'raw',
'filterWidgetOptions'=>[
'pluginOptions'=>['format'=>'yyyy-mm-dd']
],
],
// 'dtraining_dateto',
[
'attribute'=>'dtraining_dateto',
'filterType'=>GridView::FILTER_DATE,
'format'=>'raw',
'filterWidgetOptions'=>[
'pluginOptions'=>['format'=>'yyyy-mm-dd']
],
],
'ctraining_numberhours',
'ctraining_type',
'ctraining_conducted',
//'ctraining_attachment',
[
'attribute'=>'ctraining_attachment',
'format' => 'raw',
'label' => 'View Profile',
'value' => function ($model){
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' => $model->ntraining_id,
'header' => 'raw',
]), ['TblTrainingController/Listeaffecter'], ['class' => 'btn btn-success opener', 'id'=>'opener', 'data' => $model->ntraining_id,]);
},
],
//'id',
['class' => 'yii\grid\ActionColumn'],
],
'storage'=>DynaGrid::TYPE_COOKIE,
'theme'=>'panel-info',
'gridOptions'=>[
'dataProvider' => $dataProvider,
'id' => 'grid',
'filterModel' => $searchModel,
'panel'=>['heading'=>'<h3 class="panel-title">LEARNING AND DEVELOPMENT (L&D) INTERVENTIONS/TRAINING PROGRAMS ATTENDED</h3>'],
],
'options'=>['id'=>'dynagrid-1'], // a unique identifier is important
]); ?>
Here is javascript Code
$this->registerJs("$(function(){
$('.opener').click(function(e) {
alert(e.data);
//alert($(this).val());
e.preventDefault();
ViewPDfAttach();
});
});");
Here is the image of my interface that display null when I click the button from gridview
Call to alert(e.data); means you want to get the value of variable e from function callback.
While calling alert($(this).val()); means you want to get the value from value attribute in the element, but there's no value attribute found in yours.
Based on your element structure, if you want to get the value from data attribute, you can do this:
alert($(this).attr('data'));
If your element has structure like <a data-hello="Hello there">Click me</a>, you can alert with this one:
alert($(this).data('hello'));
See here for more informations:
JQuery .data()
JQuery .attr()
i am working on simple custom module for Drupal 7, which take two numeric values and on submit display sum of two values on same page.
I have form ready, NOT completed but struggling as I am want to call calculateFunction() function when user submit form, takes parameter and sum values...
info
name = form_test
description = Small module which demo how to create input form in custom module.
core = 7.x
Module php
<?php
function form_test_menu()
{
$items['formtest'] = array(
'title' => 'Form Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_test_form'),
'access callback' => TRUE,
);
return $items;
}
function form_test_form($form,&$form_submit) {
$form['firstname'] = array(
'#title' => t('First Number'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['lastname'] = array(
'#title' => t('Second Number'),
'#type' => 'textfield',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Run Function'));
calculateFunction($form);
return $form;
}
function calculateFunction()
{
echo "sum of two numbers.....";
}
?>
Instead of calling calculateFuction() directly you need to set $form['#submit'] to an array containing the name of the functions you want to call when the form is submitted.
$form['submit'] just creates the submit form element. #submit sets which callback function to submit to.
You may also optionally set $form['#validate'] to validate the submitted data.
See the Form API tutorial at Drupal.org
The submit callback could look something like the following:
/**
* FAPI Callback for form_test_form().
*/
function calculateFunction($form, &$form_state) {
$values = $form_state['values'];
$sum = $values['first_name'] + $values['last_name'];
drupal_set_message('The sum is ' . $sum);
}
since three hours I don't found the error in jquery.
I try to refresh a div, after I've created a file
Here is my View
<?php
echo CHtml::ajaxLink('Neuen Export erstellen',
Yii::app()->createUrl('exporter/create' ),
array(
'data' => array(),
'dataType' => 'json',
'type' => 'POST',
'complete' => "js:function(html){
$('#export-grid').fadeOut().fadeIn();
}",
'success' => "js:function(html){
$('#export-grid').replaceWith();
}"
),
array(
'class' => 'c2a_gray alignright',
'style' => 'font-size: 12px',
)
);
?>
** My Controller **
public function actionCreate()
{
// createfile();...
// do some stuff
$this->renderPartial('//users//exporter//_tmo', true, true);
}
complete Option works in ajaxLink function very well
but if I put alert(html) inside complete I got "Object object"
I don't know how to update export-grid with the new content.
please help me!
thx!
Yes if you try to alert html then it will return Object.
Please try alert(html.responseText).
I'm having some troubles printing a 'node/add' form in a lightbox.
I have in my custom.module a hook_menu like this:
$items['get-form/%'] = array(
'title' => t('Get a form'),
'description' => t('Get form'),
'page callback' => '_get_form',
'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
... where % is the id, like "story_node_form".
Then, I have the callback function like this:
function _get_form($form_id){
module_load_include('inc', 'node', 'node.pages');
if (strpos($form_string, "_node_form")){
//Test if the form is a <type>_node_form. Is the node/add/<type>
$content_type = explode("_node_form", $form_id)[0];
print drupal_render(node_add($content_type));
}
The forms shows right, in the lightbox. The problem is that the javascript of the form (wysiwyg, node references, term references, ...) doesn't work.
I tried to execute Drupal.attachBehaviors(), Drupal.attachBehaviors(document) and Drupal.attachBehaviors("#story-node-form") but nothing seems to work.
Anyone can help?
This code should do the trick:
function _get_form($form_id){
global $user;
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => 'ENTER NODE TYPE HERE',
'language' => LANGUAGE_NONE
);
$form_state['build_info']['args'] = array($node);
form_load_include($form_state, 'inc', 'node', 'node.pages');
echo drupal_render(drupal_build_form($form_id, $form_state));
}
You will need to get the node type as well as the form_id for this to work but that shouldn't be to difficult.