2017-04-21 2 views
0

나는 취미가 개발자입니다. 새로운 Select/Edit Rails 양식에서 기본 Select2 ajax를 사용하고 있으며 편집 양식에서 다시 채우는 방법을 알 수 없습니다. song_creditor_1은 문자열이지만 팬 모델의 ID로 입력됩니다.편집 양식의 Select2 필드 채우기

양식 :

<%= f.input :song_creditor_1, label: 'Song Creditor 1', as: :select, input_html: { id: "song_creditor_1", include_hidden: false } %> 

스크립트

function templateDropDownFormat (item) { 
    return item.text; 
    } 

    function templateSelectionFormat (item) { 
    return item.text; 
    } 

    $("#song_creditor_1").select2({ 
    placeholder: "Search", 
    ajax: { 
     url: "/songs", 
     dataType: 'json', 
     delay: 250, 
     data: function (params) { 
     return { 
      q: params.term, // search term 
      page: params.page 
     }; 
     }, 

     processResults: function (data, params) { 
     // parse the results into the format expected by Select2 
     // since we are using custom formatting functions we do not need to 
     // alter the remote JSON data, except to indicate that infinite 
     // scrolling can be used 
     params.page = params.page || 1; 
     return { 
      results: $.map(data, function(cnut) { 
       return { 
       text: cnut.name + ", " + cnut.city + ", " + cnut.country + " (" + cnut.account_type + ")", 
       id: cnut.id, 
       }; 
      }), 
      pagination: { 
      more: (params.page * 30) < data.total_count 
      } 
     }; 
     }, 
     cache: true 
    }, 
    escapeMarkup: function (markup) { return markup; }, // let our custom formatter work 
    minimumInputLength: 1, 
    templateResult: templateDropDownFormat, // omitted for brevity, see the source of this page 
    templateSelection: templateSelectionFormat// omitted for brevity, see the source of this page 
    }); 

답변

0

글쎄, 난 그냥 취미 요, 이것은 내 첫 번째 웹 사이트, 그래서이 답변은 아니지만, 꽤 나는 그것을 해결하는 방법이 있어요 . 문제는 양식에 오류가있는 경우 필드의 데이터를 다시 채워야했기 때문에 '작성'중에 특정 속성을 꺼내서 @ creditor1이라고했습니다.

var $select1 = $('#song_creditor_1'); 

    function templateDropDownFormat (item) { 
    return item.text; 
    } 

    function templateSelectionFormat (item) { 
    return item.text; 
    } 

    $select1.select2({ 
    ajax: { 
     url: "/songs", 
     dataType: 'json', 
     delay: 250, 
     data: function (params) { 
     return { 
      q: params.term, // search term 
      page: params.page 
     }; 
     }, 

     processResults: function (data, params) { 
     // parse the results into the format expected by Select2 
     // since we are using custom formatting functions we do not need to 
     // alter the remote JSON data, except to indicate that infinite 
     // scrolling can be used 
     params.page = params.page || 1; 
     return { 
      results: $.map(data, function(cnut) { 
       return { 
       text: cnut.name + ", " + cnut.city + ", " + cnut.country + " (" + cnut.account_type + ")", 
       id: cnut.id, 
       }; 
      }), 
      pagination: { 
      more: (params.page * 30) < data.total_count 
      } 
     }; 
     }, 
     cache: true 
    }, 
    escapeMarkup: function (markup) { return markup; }, // let our custom formatter work 
    minimumInputLength: 1, 
    templateResult: templateDropDownFormat, // omitted for brevity, see the source of this page 
    templateSelection: templateSelectionFormat// omitted for brevity, see the source of this page 
    }); 


<% if params[:action] === "edit" && @creditor1_real || params[:action] === "update" && @creditor1_real %> 
    var $option1 = $('<option selected><%= @creditor1_real.name %>, <%= @creditor1_real.city %>, <%= @creditor1_real.country %></option>').val("<%= @creditor1_real.id %>"); 

    $select1.append($option1).trigger('change'); 

<% elsif params[:action] === "create" && @creditor1 || params[:action] === "new" && @creditor1 %> 
    var $option1 = $('<option selected><%= @creditor1.name %>, <%= @creditor1.city %>, <%= @creditor1.country %></option>').val("<%= @creditor1.id %>"); 

    $select1.append($option1).trigger('change'); 

<% else %> 
    var $option1 = $('<option selected>Search</option>'); 

    $select1.append($option1).trigger('change'); 
<% end %>