0
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<script type="text/javascript">
google.load("elements", "1", {packages: "transliteration"});
</script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
function OnLoad() {                
    var options = {
        sourceLanguage:
        google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage:
        [google.elements.transliteration.LanguageCode.HINDI],
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true
    };

    var control = new google.elements.transliteration.TransliterationControl(options);
    control.makeTransliteratable(["txtHindi"]);
    var keyVal = 32; // Space key
    $("#txtEnglish").on('keydown', function(event) {
        if(event.keyCode === 32) {
            var engText = $("#txtEnglish").val() + " ";
            var engTextArray = engText.split(" ");
            $("#txtHindi").val($("#txtHindi").val() + engTextArray[engTextArray.length-2]);

            document.getElementById("txtHindi").focus();
            $("#txtHindi").trigger ( {
                type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
            } );
        }
    });

    $("#txtHindi").bind ("keyup",  function (event) {
        setTimeout(function(){ $("#txtEnglish").val($("#txtEnglish").val() + " "); document.getElementById("txtEnglish").focus()},0);
    });
} //end onLoad function

google.setOnLoadCallback(OnLoad);
</script> 

</head>
    <body>
       English Text: <input size="40" type="text" id="txtEnglish"/> <br/>
       Hindi Text`enter code here` : <input size="40" type="text" id="txtHindi"/> 
</body>
</html>

This is my normal JS code . And it does work . But my problem is i want this code to run in yii2 .

In yii2 gridview , this is my code where i need this JS code to run , how can i pass id over there in yii2 to run this code ?

MY yii2 code is :

 [
                    'format' => 'raw',
                    'attribute' => 'title',
                    'value' => function($model, $key) {
                        if ($model->book->language == 1) {
                            $m = "<p class='n'>" . $model->book->title . "</p>";
                        } else {
                            $m = $model->book->title;
                        }
                        return $m;
                    }
                ],
2
  • I think you should add your js code inside $this->registerJs('your js code here',['position' => \yii\web\View::POS_HEAD]), at the bottom of your view page. Commented Nov 16, 2016 at 12:26
  • yes i have does that already but my question is i need this code in search , but this js is in td .. so how can i use it search field ? Commented Nov 17, 2016 at 6:31

1 Answer 1

1

In the end of your view try add following code.

<?php

$script = <<< JS

//here you write all your javascript stuff
alert();

JS;
$this->registerJs($script);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks but what i need is while searching in my searchfield , i searched with english word and the hindi word similar to that english should also be searched . For example i am searching the word HELLO and the word HELLO in hindi should also be displayed .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.