2016-06-21 8 views
1

, wie Ansichtsvariablenwert in Javascript ändern. Ich meine, wenn ich April aus der Dropdown-Tabelle wähle, werden nur die Daten des Aprils angezeigt, und die aktuellen Tabellendaten werden verschwinden und aktualisiert. Wie kann ich es tun? Helfen Sie mir
Mein JSwie man Ansichtsvariable in js der Schienen ändert

:javascript 
    var updateMonth = function(){ 
     var month_id = $("#select_other_month").val(); 
     console.log(month_id) 
    } 

hier ist mein Tag select (Dropdown)

%select{:name => "options", :id=>"select_other_month",:onchange=>"updateMonth()"} 
      %option.placeholder{:disabled => "", :selected => "", :value => ""} see other months 
      %option{:value => "0"} this month 
      [email protected]_month - 1.month 
      %option{:value => "5"}=a.strftime('%Y-%m') 
      [email protected]_month - 2.month 
      %option{:value => "4"}=b.strftime('%Y-%m') 
      [email protected]_month - 3.month 
      %option{:value => "3"}=c.strftime('%Y-%m') 
      [email protected]_month - 4.month 
      %option{:value => "2"}=d.strftime('%Y-%m') 
      [email protected]_month - 5.month 
      %option{:value => "1"}=e.strftime('%Y-%m') 

und meine Tabelle sieht wie folgt aus

.table{:id=>"time_table"} 
     %table{:border=>"1"} 
      %thead 
       %tr 
        %th No 
        %th Name 
        -(@[email protected]_end_of_month).each do |day| 
         -if (day.saturday? || day.sunday?)==false 
          %th=day.strftime("%d") 
        %th Total days 
        %th Total time 

ich meine @xmonth wollen ändern vairable aus JS
Hinweis: @this_month = Date.today

+0

Sie möchten frische Daten vom Server abrufen und auffüllen? – Kumar

+0

Ja, ich bin. Was sollte ich jetzt tun? –

Antwort

1
var updateMonth = function(){ 
    var month_id = $("#select_other_month").val(); 
    $.ajax({ 
    url: '/your_controller/its_method', //make sure to add this path to routes file 
    data: { 
     month_id: month_id 
    }, 
    success: function(response){ 
     $("#target_area").html(response); 
    }, 
    error: function(error_res){ 
     console.log(error_res); 
    } 
    }); 
} 

Ihrer Ansicht Datei eine ID hinzufügen, so können wir ihren Inhalt mit der neuen Antwort ersetzen wir bekommen

%thead{:id => "target_area"} 
    %tr 
    %th No 
    %th Name 
    ... 

In Ihrem Controller

class YourController 
    def your_method 
    month_id = params[:month_id] 
    #update your @xmonth by month_id 
    #your new information should be inside @xmonth object 
    respond_to do |format| 
     format.html {render partial: 'your_controller/your_method/your_partial.haml.erb' } 
    end 
    end 
end 

Ihre Teil-Datei erstellen _your_partial.haml.erb mit der Inhalt, der ersetzt werden muss

%tr 
    %th No 
    %th Name 
    -(@[email protected]_end_of_month).each do |day| 
    -if (day.saturday? || day.sunday?)==false 
    %th=day.strftime("%d") 
    %th Total days 
    %th Total time 

Der Inhalt des Teils wird in Ihrer Ansicht durch den Inhalt von #target_area ersetzt, wenn Sie eine Erfolgsantwort erhalten. Siehe die updateMonth-Funktion.

Hoffe, das hilft

+0

Vielen Dank für die Beantwortung meiner Frage. Aber ich habe Probleme mit deinem Rat. Die Fehlermeldung steht auf meiner Frage. –

+0

@NeoTeo Beim partiellen Rendering von 'format.html' nicht' _' verwenden, da es automatisch für Sie hinzugefügt wird. – Kumar

+0

Es funktioniert! Danke für deinen großen Rat: D –