Javascript 默认情况下,数据表按数字排序

Javascript 默认情况下,数据表按数字排序,javascript,sorting,datatables,Javascript,Sorting,Datatables,我从后端提取一些数据,并在DataTables表中显示它们。问题是它没有根据末尾的追加数字对行进行排序,这正是我要做的。我正在粘贴问题的屏幕截图。谢谢你的帮助 编辑:通过这种方式,我将JSON提供给数据表 $finalTableSQL = "select * from players_data where store = $store"; $finalTableResult = $mysqli->query($finalTableSQL); $rows = array

我从后端提取一些数据,并在DataTables表中显示它们。问题是它没有根据末尾的追加数字对行进行排序,这正是我要做的。我正在粘贴问题的屏幕截图。谢谢你的帮助

编辑:通过这种方式,我将JSON提供给数据表

$finalTableSQL = "select * from players_data where store = $store";
$finalTableResult = $mysqli->query($finalTableSQL);
$rows = array();
while ($row = $finalTableResult->fetch_array(MYSQLI_ASSOC)) {
    $rows[] = $row;
}

$returnToCashier = [];
$returnToCashier["data"] = [];
foreach ($rows as $finalTableRows) {
    $playerID = (int) $finalTableRows["user_id"];
    $playerEmail = $userRow["email"];
    $lastintype = (int) $finalTableRows["last_in_type"];
    $lastIN = (float) $finalTableRows["last_in"] / 100;

    if ($lastintype == 0) {
        $intype = "Cash";
    } else if ($lastintype == 1) {
        $intype = "Privilege";
    } else {
        $intype = "Bonus";
    }

    $canCashOut = (int) $finalTableRows["can_cash_out"];

    if ($canCashOut == 0) {
        $credits = "<font style='color: red;'>" . ((float) $finalTableRows["credits"] / 100) . " €</font>";
    } else {
        $credits = ((float) $finalTableRows["credits"] / 100) . " €";
    }

    $playeruserSQL = "select * from directus_users where id = $playerID";
    $playeruserResult = $mysqli->query($playeruserSQL);
    $playeruserRow = $playeruserResult->fetch_array(MYSQLI_ASSOC);

    array_push($returnToCashier["data"], [
        "username" => str_replace("@mail.com", "", $playeruserRow["email"]),
        "credits" => $credits,
        "lastintype" => $lastIN . " € " . $intype,
        "lastplayed" => $finalTableRows["last_played"],
        "playerid" => $playerID,
    ]);
}

echo json_encode($returnToCashier);

要按编号排序,您需要执行以下操作:
columnDefs:[{targets:1,键入:'num'}],

要按数字排序,需要执行以下操作:
columnDefs:[{targets:1,键入:'num'}],

您可以组合以下技术:

1-创建仅包含每个值的数字部分的隐藏列

2-使用该选项允许根据隐藏列中的数据对可见列进行排序

在我的测试用例中,我是从JavaScript变量加载数据,而不是从ajax URL加载数据,但其他情况下,该方法的工作方式与您相同:

我的测试表:

<table id="example" class="display dataTable cell-border" style="width:100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>hidden column</th>
        </tr>
    </thead>
</table>
这将显示原始的“rodplayer”数据值。但当您对该列进行排序时,它将使用从每个字符串末尾提取的数字数据

我使用的正则表达式(
/([0-9]+)/
)假定数值数据总是出现在每个值的末尾。因此,只要您的数据从未偏离此格式,正则表达式将按预期工作


您可以结合以下技术:

1-创建仅包含每个值的数字部分的隐藏列

2-使用该选项允许根据隐藏列中的数据对可见列进行排序

在我的测试用例中,我是从JavaScript变量加载数据,而不是从ajax URL加载数据,但其他情况下,该方法的工作方式与您相同:

我的测试表:

<table id="example" class="display dataTable cell-border" style="width:100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>hidden column</th>
        </tr>
    </thead>
</table>
这将显示原始的“rodplayer”数据值。但当您对该列进行排序时,它将使用从每个字符串末尾提取的数字数据

我使用的正则表达式(
/([0-9]+)/
)假定数值数据总是出现在每个值的末尾。因此,只要您的数据从未偏离此格式,正则表达式将按预期工作


请包含一些代码。@NullDev刚刚添加了一些代码,谢谢。请包含一些代码。@NullDev刚刚添加了一些代码,谢谢。这不提供问题的答案。一旦你有足够的钱,你将能够;相反这并不能回答这个问题。一旦你有足够的钱,你将能够;相反-