如何从表中提取JSON并从另一个表中排序ID列表?

时间:2013-01-18 20:11:07

标签: jquery ruby-on-rails jquery-ui hash

我有一个JSON字符串,显示不同优先级的排名,json的关键字是排名的数字,该键的值是优先级表中优先级的ID。例如{“1”:2,“2”:3,“3”,1}

我将这个JSON存储在mysql的PRIORITY_RANKS列中,用于教练,我正在使用rails应用程序中的COACHES / EDIT页面,我需要能够按照顺序将列表中的优先级排序到列表中。 JSON字符串表示不是在mysql中找到它们的顺序。

不知何故,我需要解析JSON字符串并相应地对优先级进行排序。我正在使用jquery UI来进行可排序的功能。下面是一个列表的样本。如果您需要更多信息,请告诉我。我仍然只是一个新手的经验哈哈!

查看/编辑                                                 `

<td style="padding:20px;">
                        <label class="pc3">Drag to Rank Priorities for Coach</label>
                        <p class="priochecker">Uncheck to disable Priorities</p>
                        <div id="rightlist">
                            <input type="hidden" name="currentranks" id="currentranks"></input>
                            <ul id="sortable" style="list-style:none;">
                                <% @priorities.each do |x, count| %>
                                    <li class="digit"><input class="priobox" type="checkbox" id="<%= x.id %>" name="<%= x.name %>" value="<%= x.id %>" onclick="removeval(this,<%= x.id %>);" checked>
                                    <strong>Rank <span class='prilistid'></span> <%= x.name %></strong></li>
                                <% end %>   
                            </ul>
                        </div>
                        </td>
                    </tr>
                </table>
                </div>
                <input type="hidden" name="coach[priority_ranks]" id="hash" value="{'1':1,'2':2,'3':3}"></input>



COACHES_CONTROLLER

  def edit
    #@coach = @current_coach
    @locations = Location.find(:all, :order => "name")
    @levels = Level.find(:all, :order => "value")
    @coach = Coach.find(params[:id])
    @ranks = Array.new
    @current_site = @coach.sites
    @priority_ranks = @coach.priority_ranks
    @priorities = Priority.find(:all, :conditions => { :deleted => false })
    siteObj = Site.all

    @sites = {}

    @sites['All Sites'] = -1

    siteObj.each do |t|
      @sites[t.name] = t.id
    end
  end

0 个答案:

没有答案