html+js table中數據上下移動 練習

3 篇文章 0 订阅

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <link href="https://unpkg.com/bootstrap-table@1.18.3/dist/bootstrap-table.min.css" rel="stylesheet">

    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

    <script src="https://unpkg.com/bootstrap-table@1.18.3/dist/bootstrap-table.min.js"></script>

 

    

 

</head>

<body>

    <div>

        <span>工號:</span><input id="txtEmpNo" />

        <span>姓名:</span><input id="txtEmpName" />

        <button id="btnTest" οnclick="addOneData()">增加</button>

    </div>

    <div>

        <table id="SignList" style="border: 1 solid black;">

        

        </table>

    </div>


 

    <script>

        $(function(){

            loadData();

 

        })

 

        function initTable(){

            $("#SignList").bootstrapTable('destroy').bootstrapTable({

                columns:[[

                    {

                        title:"工號",

                        field:"empNo",

                        align:"center"

                        

                    },

                    {

                        title:"姓名",

                        field:"empName",

                        align:"center"

                    },

                    {

                        title:"上移",

                        field:"empName",

                        align:"center",

                        formatter:function(value,row,index,field){

                            if(index>0){

                                return['<a href="javascript:void(0);" οnclick="upMove(\''+row.empNo+'\',\''+row.empName+'\')" >'+'上移'+'</a>'

                                    ].join('');

                            }

                            

 

                        }

                    },

                    {

                        title:"下移",

                        field:"empName",

                        align:"center",

                        formatter:function(value,row,index,field){

                            if(index<myData.length-1){

                                return['<a href="javascript:void(0);" οnclick="downMove(\''+row.empNo+'\',\''+row.empName+'\')" >'+'下移'+'</a>'

                                    ].join('');

                            }

                            

 

                        }

                    },

                    

            ]]

            })

        }

 

        

        var myData=[{"empNo":"F1","empName":"gs1"},{"empNo":"F2","empName":"gs2"},{"empNo":"F3","empName":"gs3"}];

 

        function loadData(){

            initTable();

           

            console.log(myData);

            $("#SignList").bootstrapTable('load',myData)

        }

 

        function upMove(pempno,pempname){

            for(var i=0;i<myData.length;i++){

                if(myData[i].empNo==pempno && myData[i].empName==pempname){

                    if(i>0){

                        myData.splice(i,1)

                        myData.splice(i-1,0,{"empNo":pempno,"empName":pempname})

                        loadData();

                    }

                    

                    alert("當前位置:"+i);

                    break;

                }

            }

        }

        function downMove(pempno,pempname){

            for(var i=0;i<myData.length;i++){

                if(myData[i].empNo==pempno && myData[i].empName==pempname){

                    if(i<myData.length){

                        myData.splice(i,1)

                        myData.splice(i+1,0,{"empNo":pempno,"empName":pempname})

                        loadData();

                    }

                    

                    alert("當前位置:"+i);

                    break;

                }

            }

 

        }

 

        function addOneData(){

            var empNo=$("#txtEmpNo").val();

            var empName=$("#txtEmpName").val();

            var obj={"empNo":empNo,"empName":empName};

            myData.push(obj);

            loadData();

 

        }

 

        


 

        function getIndex(){

            for(var i=0;i<myData.length;i++){

                if(myData[i].empNo=="F2"){

                    myData.splice(i,0,{"empNo":"F4","empName":"gs4"})

                    loadData();

                    alert(i);

                    break;

                }

            }

        }

    </script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值