How to Implement row dragging using javascript

Source: Internet
Author: User

How to Implement row dragging using javascript

This example describes how to implement row dragging in javascript. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

<! Doctype html>

<Html>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> drag rows </title>

<Script>

Window. onload = function (){

// Bind events

Var addEvent = document. addEventListener? Function (el, type, callback ){

El. addEventListener (type, callback ,! 1 );

}: Function (el, type, callback ){

El. attachEvent ("on" + type, callback );

}

// Determine the style support

Var getStyleName = (function (){

Var prefixes = ['', '-ms-', '-moz-', '-webkit-', '-khtml-', '-o-'];

Var reg_cap =/-([a-z])/g;

Function getStyleName (css, el ){

El = el | document.doc umentElement;

Var style = el. style, test;

For (var I = 0, l = prefixes. length; I <l; I ++ ){

Test = (prefixes [I] + css). replace (reg_cap, function ($0, $1 ){

Return $1. toUpperCase ();

});

If (test in style ){

Return test;

}

}

Return null;

}

Return getStyleName;

})();

Var userSelect = getStyleName ("user-select ");

// Obtain the style accurately

Var getStyle = document. defaultView? Function (el, style ){

Return document. defaultView. getComputedStyle (el, null). getPropertyValue (style)

}: Function (el, style ){

Style = style. replace (/\-(\ w)/g, function ($, $1 ){

Return $1. toUpperCase ();

});

Return el. currentStyle [style];

}

Var dragManager = {

Y: 0,

DragStart: function (e ){

E = e | event;

Var handler = e.tar get | e. srcElement;

If (handler. nodeName = "TD "){

Handler = handler. parentNode;

DragManager. handler = handler;

If (! Handler. getAttribute ("data-background ")){

Handler. setAttribute ("data-background", getStyle (handler, "background-color "))

}

// Display as movable

Handler. style. backgroundColor = "# ccc ";

Handler. style. cursor = "move ";

DragManager. y = e. clientY;

If (typeof userSelect = "string "){

Return document.doc umentElement. style [userSelect] = "none ";

}

Document. unselectable = "on ";

Document. onselectstart = function (){

Return false;

}

}

},

Draging: function (e) {// drag the row when mousemove

Var handler = dragManager. handler;

If (handler ){

E = e | event;

Var y = e. clientY;

Var down = y> dragManager. y; // whether to move down

Var tr = document. elementFromPoint (e. clientX, e. clientY );

If (tr & tr. nodeName = "TD "){

Tr = tr. parentNode

DragManager. y = y;

If (handler! = Tr ){

Tr. parentNode. insertBefore (handler, (down? Tr. nextSibling: tr ));

}

};

}

},

DragEnd: function (){

Var handler = dragManager. handler

If (handler ){

Handler. style. backgroundColor = handler. getAttribute ("data-background ");

Handler. style. cursor = "default ";

DragManager. handler = null;

}

If (typeof userSelect = "string "){

Return document.doc umentElement. style [userSelect] = "text ";

}

Document. unselectable = "off ";

Document. onselectstart = null;

},

Main: function (el ){

AddEvent (el, "mousedown", dragManager. dragStart );

AddEvent (document, "mousemove", dragManager. draging );

AddEvent (document, "mouseup", dragManager. dragEnd );

}

}

Var el = document. getElementById ("table ");

DragManager. main (el );

}

</Script>

<Style>

. Table {width: 60%; border: 1px solid red; border-collapse: collapse ;}

. Table td {border: 1px solid red; height: 20px ;}

</Style>

</Head>

<Body>

<H1> drag rows

<Table id = "table" class = "table">

<Tbody>

<Tr> <td> 1 </td> <td> One </td> <td> dom. require </td> </tr>

<Tr id = "2"> <td class = "2"> 2 </td> <td> Two </td> <td> ControlJS </td> </tr>

<Tr id = "3"> <td class = "3"> 3 </td> <td> Three </td> <td> HeadJS </td> </tr>

<Tr id = "4"> <td class = "4"> 4 </td> <td> Four </td> <td> LAB. js </td> </tr>

<Tr id = "5"> <td class = "5"> 5 </td> <td> Five </td> <td> $ script. js </td> </tr>

<Tr id = "6"> <td class = "6"> 6 </td> <td> Six </td> <td> NBL. js </td> </tr>

</Tbody>

</Table>

</Body>

</Html>

I hope this article will help you design javascript programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.