使用 JavaScript(Using JavaScript)

优质
小牛编辑
127浏览
2023-12-01

描述 (Description)

您可以使用JavaScript App方法启用和禁用sortable,如下所示 -

  • myApp.sortableOpen(sortableContainer) - 用于在指定的可排序容器上启用排序模式。

  • myApp.sortableClose(sortableContainer) - 用于在指定的可排序容器上禁用排序模式。

  • myApp.sortableToggle(sortableContainer) - 用于在指定的可排序容器上切换排序模式。

上述方法接受sortableContainer参数,该参数是可选的HTMLElement或可排序容器的string

例子 (Example)

以下示例使用Framework7中的JavaScript指定HTML元素的排序 -

<!DOCTYPE html>
<html>
   <head>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, 
         maximum-scale = 1, minimum-scale = 1, user-scalable = no, minimal-ui" />
      <meta name = "apple-mobile-web-app-capable" content = "yes" />
      <meta name = "apple-mobile-web-app-status-bar-style" content = "black" />
      <title>Sortable using JavaScript</title>
      <link rel = "stylesheet" 
         href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.min.css" />
      <link rel = "stylesheet" 
         href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.colors.min.css" />
   </head>
   <body>
      <div class = "views">
         <div class = "view view-main">
            <div class = "navbar">
               <div class = "navbar-inner">
                  <div class = "center sliding">Sortable using JavaScript</div>
               </div>
            </div>
            <div class = "pages navbar-through">
               <div data-page = "index" class = "page navbar-fixed">
                  <div class = "page-content">
                     <div class = "content-block">
                        <p><a href = "#" class = "open-sortable">Enable sortable</a></p>
                        <p><a href = "#" class = "close-sortable">Disable sortable</a></p>
                        <p><a href = "#" class = "toggle">Toggle sortable</a></p>
                     </div>
                     <div class = "list-block sortable">
                        <ul>
                           <li>
                              <div class = "item-content">
                                 <div class = "item-media"><i class = "icon icon-form-name"></i></div>
                                 <div class = "item-inner">
                                    <div class = "item-title">Sachin</div>
                                    <div class = "item-after">India</div>
                                 </div>
                              </div>
                              <div class = "sortable-handler"></div>
                           </li>
                           <li>
                              <div class = "item-content">
                                 <div class = "item-media"><i class = "icon icon-form-name"></i></div>
                                 <div class = "item-inner">
                                    <div class = "item-title">Smith</div>
                                    <div class = "item-after">Australia</div>
                                 </div>
                              </div>
                              <div class = "sortable-handler"></div>
                           </li>
                           <li>
                              <div class = "item-content">
                                 <div class = "item-media"><i class = "icon icon-form-name"></i></div>
                                 <div class = "item-inner">
                                    <div class = "item-title">Morgan</div>
                                    <div class = "item-after">England</div>
                                 </div>
                              </div>
                              <div class = "sortable-handler"></div>
                           </li>
                           <li>
                              <div class = "item-content">
                                 <div class = "item-media"><i class = "icon icon-form-name"></i></div>
                                 <div class = "item-inner">
                                    <div class = "item-title">Dhoni</div>
                                    <div class = "item-after">India</div>
                                 </div>
                              </div>
                              <div class = "sortable-handler"></div>
                           </li>
                           <li>
                              <div class = "item-content">
                                 <div class = "item-media"><i class = "icon icon-form-name"></i></div>
                                 <div class = "item-inner">
                                    <div class = "item-title">Gayle</div>
                                    <div class = "item-after">West Indies</div>
                                 </div>
                              </div>
                              <div class = "sortable-handler"></div>
                           </li>
                        </ul>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
      <script type = "text/javascript" 
         src = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/js/framework7.min.js"></script>
      <script>
         // here initialize the app
         var myApp = new Framework7();
         // If your using custom DOM library, then save it to $$ variable
         var $$ = Dom7;
         // Add the view
         var mainView = myApp.addView('.view-main', {
            // enable the dynamic navbar for this view
            dynamicNavbar: true
         });
         $$('.open-sortable').on('click', function () {
            myApp.sortableOpen('.sortable');
         });
         $$('.close-sortable').on('click', function () {
            myApp.sortableClose('.sortable');
         });
         $$('.toggle').on('click', function () {
            myApp.sortableToggle('.sortable');
         });
      </script>
   </body>
</html>

输出 (Output)

让我们执行以下步骤,看看上面给出的代码是如何工作的 -

  • 将上面给出的HTML代码保存为服务器根文件夹中的enable_disable_sortable_using_javascript.html文件。

  • 以http://localhost/enable_disable_sortable_using_javascript.html打开此HTML文件,输出显示如下。

  • 该示例指定启用,禁用和切换链接上HTML元素的排序。