如何根据行ID列表更改表中行的顺序? [英] How to change order of rows in table based on list of rows id?

查看:163
本文介绍了如何根据行ID列表更改表中行的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ID test_table $ b

 <表id id = TEST_TABLE > < /表> 

和or有不同的id。

我已经订购了行列表,如

  [name,value,description] 

与屏幕上的呈现表有什么不同(用户可以在飞行中设置新的顺序,这就是我如何填充行列表'id)。



如何根据有序列表在该表中设置新订单?
(说明:
我有两个列表,第一个表中有一列,用户可以在第一个表中拖放行,并且我需要在第二个表中具有相同的顺序,第二个表中的ids具有与第一个前缀相同的标识
这是我在第一个表格中拖放的方式。)

  $(#cleanVert ).tableDnD({
onDragClass:myDragClass,
onDrop:function(table,row){
var rows = table.tBodies [0] .rows;
var debugStr =New Order:;
var outStr =;
for(var i = 0; i debugStr + = rows [i] .id + ;
outStr + = rows [i] .id;
if(i<(rows.length-1)){
str + =,
}

$(#debugArea)。html(debugStr);
},
onDragStart:function(table,row){
$(#debugArea) .html(开始拖动行+ row.id);
}
});


解决方案

如果您想在jQuery中完成所有工作,它看起来像你试图做的,然后我会研究 jQuery Sortable 但如果你想它就像一个链接/按钮,按照升序或降序排序每个字段,或通过类似的用户名等,并保持它的服务器端,那么我会建议是有按钮/链接重新加载自己的url中的子查询那会说类似于?order_by = asc ?order_by = username& dir = asc 然后运行检查在页面上,将该变量/子查询($ _GET ['order_by'])传递给一个函数。在函数中,执行一个类似于下面的查询:

  $ order_by =ORDER BY。 $ _GET ['order_by']。 $ _GET [ DIR]; 
$ query =SELECT *
FROM table
$ order_by;

然后,您可以使用相同的查询来进行主页面加载(尚未排序)使用变量时遇到困难,那么当查询重新运行并载入 $ order_by 时,列表将按参数排序。


I have table with id test_table

<table id="test_table"> </table>

and or has different id.

I have ordered list of rows' id like

["name","value","description"]

what is different from rendered table on screen ( user can on fly set new order, that is how I fill list of rows' id).

How to set new order in that table based on ordered list ? (Explanation: I have two lists one aside other, user can drag and drop rows in first table and I need to have same order in second, ids in second table have same id like in first +prefix "preview". This is how I drag/drop in first table. )

$("#cleanVert").tableDnD({
    onDragClass: "myDragClass",
    onDrop: function(table, row) {
        var rows = table.tBodies[0].rows;
        var debugStr = "New Order: ";
        var outStr = "";
        for (var i=0; i<rows.length; i++) {
            debugStr += rows[i].id+" ";
            outStr += rows[i].id;
            if(i<(rows.length-1)){
                outStr+=","
            }
        }
        $("#debugArea").html(debugStr);
    },
    onDragStart: function(table, row) {
        $("#debugArea").html("Started dragging row "+row.id);
    }
});

解决方案

If you are wanting to do everything in jQuery, which it looks like you are trying to do, then I would look into jQuery Sortable but if you want it to be like a link/button that sorts each field by say Ascending or Descending order, or by like usernames etc, and keep it server-side then what I would suggest is have the button/link reload itself with a sub-query in the url that would say something like ?order_by=asc or ?order_by=username&dir=asc then run a check for it on the page, passing that variable/sub-query ($_GET['order_by']) to a function. In the function, do a query that looks similar to this:

$order_by = "ORDER BY " . $_GET['order_by'] . $_GET['dir'];
$query = "SELECT *
          FROM table
          $order_by";

You could then use this same query for the main page load (that hasn't been sorted) without difficulty using the variable, then when the query is re-run with a loaded $order_by then the list will be ordered by parameter.

这篇关于如何根据行ID列表更改表中行的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆