|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * GwtMaterial |
| 4 | + * %% |
| 5 | + * Copyright (C) 2015 - 2021 GwtMaterialDesign |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package gwt.material.design.client.base.helper; |
| 21 | + |
| 22 | +import static gwt.material.design.jquery.client.api.JQuery.$; |
| 23 | + |
| 24 | +public class UiSortHelper { |
| 25 | + |
| 26 | + /** |
| 27 | + * Calls a Jquery sort api |
| 28 | + * |
| 29 | + * @param containerSelector - The container Id i.e UnorderedList - (ul) |
| 30 | + * @param childSelector - The child element selector (i.e List - li) |
| 31 | + * @param dataAttribute - The data attribute that indicate the order of each children (i.e order -> data-attribute='order') |
| 32 | + */ |
| 33 | + public static void sort(String containerSelector, String childSelector, String dataAttribute) { |
| 34 | + if (containerSelector != null && !containerSelector.isEmpty() && childSelector != null && !childSelector.isEmpty() && dataAttribute != null && !dataAttribute.isEmpty()) { |
| 35 | + $(containerSelector + " " + childSelector + "[data-order]").sort((elem1, elem2) -> { |
| 36 | + Object elem1Order = $(elem1).data(dataAttribute); |
| 37 | + Object elem2Order = $(elem2).data(dataAttribute); |
| 38 | + if (elem1Order != null && elem2Order != null) { |
| 39 | + return Integer.parseInt(elem2Order.toString()) < Integer.parseInt(elem1Order.toString()) ? 1 : -1; |
| 40 | + } |
| 41 | + return -1; |
| 42 | + }).appendTo(containerSelector); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments