在django-smart-choices中排序

时间:2015-09-04 20:00:29

标签: python django

我使用django-smart-choices实现了相互依赖的下拉列表。默认情况下,下拉列表中的条目按字母顺序排序,这很好。但是,我需要输入文本"其他"在下拉列表的最后显示。

在django-smart-choices中有一种简单的方法吗?

1 个答案:

答案 0 :(得分:0)

我最后只是在js中写了一些快速的东西。这段代码需要清理,但它可以解决问题:)

 <script>
        $(document).ready(function(){
          $('#id_subcategory').change(function(e){

              var deletedOtherVal = 0
              var select=document.getElementById('id_subcategory');

              var selectedValue = $('#id_subcategory').find(":selected").text();

              if (selectedValue != "Other"){
                for (i=0;i<select.length;  i++) {
                   if (select.options[i].text == "Other" ) {
                     deletedOtherVal = select.options[i].value  
                     select.remove(i);
                   }
                }

                select.options[select.options.length] = new Option('Other', deletedOtherVal);
              }
          });

          // And now fire change event when the DOM is ready
          $('#id_subcategory').trigger('change');
          });
      </script>
相关问题