1

Hi I am a newbie in rails.

I have implemented a timezone for a website and I have made a drop down box via javascript.

The problem I am having is I am able to select the values but I am not able to display the selected value in drop down.. The value in the dropbox is constant..

<div class='select-drz' ng-controller="AppController">
    <a class='btn' id='drz' timezone> TESTING <span class='arrow'></span></a>//want to implement dynamic code here
</div>

Also I am getting the value for dropdown by implementing cookie like the below code:-

Arab.controller('AppController', ['$scope',  '$rootScope', '$route', 'userAPI', 'recoAPI', 'autoAPI','$location',  function($scope,  $rootScope, $route,  userAPI, recoAPI, autoAPI,$location){

    // array of API calls & pattern string stored
    $scope.autoComplete = {
        data: [],
        lastQuery: '' ,
        previousQueries:[]
    };

    $scope.timezone_as_per_city = [{city: '(GMT+4:00) Abu Dhabi' , timezone: '240',number: '0',gmt: '+4.0'},  / Timezone to be implemented
        {city: '(GMT+3:30) Tehran',timezone: '210',number: '1',gmt: '+3.30'},
        {city: '(GMT+3:00) Kuwait',timezone: '180',number: '2',gmt: '+3.0'},{city: '(GMT+3:00) Riyadh',timezone: '180',number: '3',gmt: '+3.0'},
        {city: '(GMT+2:00) Amman',timezone: '120',number: '4',gmt: '+2.0'},{city: '(GMT+2:00) Beirut',timezone: '120',number: '5',gmt: '+2.0'}
    ];

    if($.cookie('timezone_city'))
    {
        $scope.city = $scope.timezone_as_per_city[$.cookie('number')];
        //$.cookie('timezone_city',JSON.stringify($scope.timezone_as_per_city[$.parseJSON($.cookie('timezone_city')).number]), {expires:365});
        $.cookie('timezone_city',$scope.timezone_as_per_city[$.cookie('number')].city, {expires:365});
        $.cookie('timezone',$scope.timezone_as_per_city[$.cookie('number')].timezone, {expires:365});
        $.cookie('number',$.cookie('number'), {expires:365});
        $.cookie('gmt',$scope.timezone_as_per_city[$.cookie('number')].gmt, {expires:365});
    }
    else
    {
     //   $.cookie('timezone_city',JSON.stringify({city:'(GMT+3:00) Riyadh',timezone: '180',number: '0',gmt: '+3.0'}), {expires:365});
        $.cookie('timezone_city','(GMT+3:00) Riyadh', {expires:365});
        $.cookie('timezone','180', {expires:365});
        $.cookie('number','0', {expires:365});
        $.cookie('gmt','+3.0', {expires:365});
        $scope.city = $scope.timezone_as_per_city[3];
    }
    $scope.setTimezone = function(city,timezone,number,gmt){
        //$.cookie('timezone_city',JSON.stringify(city), {expires:365});
        $.cookie('timezone_city',city, {expires:365});
        $.cookie('timezone',timezone, {expires:365});
        $.cookie('number',number, {expires:365});
        $.cookie('gmt',gmt, {expires:365});
        $route.reload("/#!" + $location.path());
    };

I am getting the perfect value in the cookie.

The javascript file is as follows:-

<li ng-repeat="l in timezone_as_per_city"  >
        <span >
          <label for="checkbox{{l.city.replace(' ','')}}" >
            {{ l.city }}
          </label>
            <div class="custom-checkbox">
              <input ng-click="setTimezone(l.city,l.timezone,l.number,l.gmt)" id="checkbox{{l.city.replace(' ','')}}" />

            </div>
        </span>
      </li>

The only query I am having is that the value or the city selected by the cookie needs to be displayed instead of testing ..

Please help me out am stuck on it

  • 2
    I guess you should tag this query with `jQuery` rather than `ruby` and `ruby-on-rails` as your problem seems to boil down to set the default value of a dropdown in JavaScript. Please confirm if that is the case. – Patru Apr 30 '14 at 07:28
  • @Patru can't we set the default value via ruby code in erb..I am not very aware but have read it is possible –  Apr 30 '14 at 07:30
  • Of course it is possible to set the value attribute of a dropdown `select` select in `erb`, but that does not make any sense if you are building the contents of the select box dynamically. You seem to know much more than I do about JavaScript and it should not be a problem to set the value there. – Patru Apr 30 '14 at 07:39
  • @Patru I think it will take a very long process for me to put the selected value via javascript as I will have to pass it through hidden value..But is there no way to get the value of the cookie and then display the same value..I mean i am getting the required value in $.cookie('timezone_city') but dont know how to display it in html –  Apr 30 '14 at 07:46
  • there is nothing lengthy about that, a quick Google search revealed [this question](http://stackoverflow.com/questions/78932/how-do-i-programatically-set-the-value-of-a-select-box-element-using-javascript) which shows you how to set a value in JavaScript. As you already have the value you want to set the rest should be simple. If it is not you should post the HTML resulting from your page load. – Patru Apr 30 '14 at 07:59
  • First, this doesn't look like a dropdown to me, but rather like a list of checkboxes. I might be mistaken though. Second, this has been answered [here](http://stackoverflow.com/a/19917193/1641070) and [here](http://stackoverflow.com/a/17815162/1641070) – Lukas_Skywalker Apr 30 '14 at 08:01
  • How are you setting the cookie? Looks like PHP or JS code to me? – Richard Peck Apr 30 '14 at 08:31
  • @RichPeck I am setting cookie via javascript here..am actually using angular js and ruby on rails.I just want to display the value selected via html..and so far not able to do so –  Apr 30 '14 at 09:24
  • Oh thanks! I didn't realize it was angularjs -- I thought you were mixing Rails & Javascript code! – Richard Peck Apr 30 '14 at 09:25
  • @Lukas_Skywalker..The problem is that i am not using ng-options in my case..But thanks for the link..will check it out –  Apr 30 '14 at 09:27
  • @RichPeck Any idea or solutions to my question. –  Apr 30 '14 at 09:28
  • Sorry buddy - I don't know angular right now! – Richard Peck Apr 30 '14 at 10:30
  • Oh, all this JavaScript will end up being HTML? Then you should probably post the relevant portion of HTML you end up with. Knowing little about angularjs I can only guess, maybe setting `ng-model` as in [this example](https://docs.angularjs.org/api/ng/directive/ngValue) could help? – Patru Apr 30 '14 at 15:38

0 Answers0