获取表格单元格值并按字母顺序排序

时间:2016-06-01 18:48:20

标签: jquery

我创建了一个表。首先,我用一个按钮获得第二列的td值。我将值发送到数组。后来我调用了排序函数,我想将排序值写入第二列的td值。我的错误在哪里?还有什么不同的方式吗?

package SoftLayer_Java_Scripts.Examples;

import java.util.ArrayList;
import java.util.List;

import com.softlayer.api.*;
import com.softlayer.api.service.user.customer.notification.Hardware;

public class EG
{
  private static String user = "set me";
  private static String apiKey = "set me";

  private static ApiClient client = new RestApiClient().withCredentials(user, apiKey);

  public static void main( String[] args )
  {
    deleteUserTonotify();
  }

  private static void deleteUserTonotify(){

    // Define the SoftLayer_User_Customer_Notification_Hardware id here
    Long userNotificationfHardwareId = new Long(280346);

    // Define SoftLayer_User_Customer_Notification_Hardware service
    Hardware.Service hardwareService = Hardware.service(client);

    // Declare SoftLayer_User_Customer_Notification_Hardware List
    List<Hardware> templateObjects = new ArrayList<Hardware>();

    // Creating a template object
    Hardware templateObject = new Hardware();

    templateObject.setId(userNotificationfHardwareId);

    // Add templateObject to templateObjects
    templateObjects.add(templateObject);

    try {
      boolean result = hardwareService.deleteObjects(templateObjects);
      System.out.println(" delete result : " + result);

    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
  }

  // This method can be used to retrieve and print 
  // the SoftLayer_User_Customer_Notification_Hardware ids by hardware id. 
    private static void printUserNotificationListByHardwareId(Long hardwareId, Long userId){
    Hardware.Service hardwareService = Hardware.service(client);

    try {
      List<Hardware> userNotificationByHardwareList = hardwareService.findByHardwareId(hardwareId);
      if(userNotificationByHardwareList.isEmpty()){
        System.out.println("There's no User Customer Notification for hardware id: " + hardwareId);
      }
      else{
        for(Hardware h : userNotificationByHardwareList){
          if((long)h.getUserId() == (long)userId){
            System.out.println(h.getId());
            break;
          }
        }
      }
    } catch (Exception e) {
      System.out.println("Error: " + e);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

一些事情:您实际上并没有在您发布的代码中将任何内容写回表中。

每次从另一个SortElements()读取值时,都会调用

<tr>。只需先收集所有值,然后在完成$('#mytable tr').each()循环后,再调整一次SortElements()

您通过执行arr,在$('#mytable tr').each()内的每次迭代中删除var arr=[];。在之前循环。

$("#sort").click(function(){
    var arr = [];

    $('#mytable tr').each(function() {
        var x = $(this).find("td").eq(1).text();        
        arr.push(x);
    }).promise().done(function(){
        arr = sort_elements(arr);
        var i = 0;

        // Simply loop again and insert the object with
        // that index.
        $('#mytable tr').each(function() {
            $(this).find("td").eq(1).text(arr[i++]);
        });
    });
});

使用$(elem).each().promise().done()构造来制作each()方法(参见this answer)。

您的sort_elements收到arr作为参数

function sort_elements(arr) {
    arr.sort(alphabetical);
    return arr
}