# Draggable table rows

**URL:** <https://forums.knack.com/t/draggable-table-rows/9226>\
**Category:** API & Custom Code\
**Created:** [February 28, 2019, 6:05pm UTC](https://forums.knack.com/t/draggable-table-rows/9226 "2019-02-28T18:05:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin11773](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@Justin11773](https://forums.knack.com/u/Justin11773)\
**Post date:** [February 28, 2019, 6:05pm UTC](https://forums.knack.com/t/draggable-table-rows/9226/1 "2019-02-28T18:05:00Z")

</div>

//Simple code for dragging table rows.

&nbsp;

LazyLoad.js([  
'[https://cdn.jsdelivr.net/npm/table-dragger@1.0.2/dist/table-dragger.min.js](https://cdn.jsdelivr.net/npm/table-dragger@1.0.2/dist/table-dragger.min.js)'  
], function () {  
$(document).on("knack-view-render.view\_102 ", function(event, view, data) {  
var myTable = $('#view\_102 \> table');  
$("table.kn-table").attr('id', 'myTable');  
tableDragger(document.querySelector("#myTable"), { mode: "row", onlyBody: true });

});  
  
});

---

<div class="post-metadata">

**Author:** ![Justin11773](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@Justin11773](https://forums.knack.com/u/Justin11773)\
**Post date:** [April 18, 2019, 7:28pm UTC](https://forums.knack.com/t/draggable-table-rows/9226/2 "2019-04-18T19:28:51Z")

</div>

Hi Egil,

&nbsp;I'm not really doing much coding here - Just lazyloading someone else's work and then calling their functions. You can visit the git and read their code to see how they're doing it. To be honest I have not read it through. Best, Justin

[https://github.com/sindu12jun/table-dragger](https://github.com/sindu12jun/table-dragger)

---

<div class="post-metadata">

**Author:** ![EgilHellan16159](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/egilhellan16159/32/183_2.png) [@EgilHellan16159](https://forums.knack.com/u/EgilHellan16159)\
**Post date:** [April 18, 2019, 9:48am UTC](https://forums.knack.com/t/draggable-table-rows/9226/3 "2019-04-18T09:48:48Z")

</div>

Would you mind share a Knack solution where you have this working, Justin? I am looking to see how you are able to write back to Knack which row is in which position. If you cannot share a link, could you just in broad strokes tell me what you are doing? Are you using an auto-increment field that you are changing the values for the rows in question for using the API, for instance, or is this just used for display purposes, not actually changing data in Knack?

---

<div class="post-metadata">

**Author:** ![Justin11773](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@Justin11773](https://forums.knack.com/u/Justin11773)\
**Post date:** [March 15, 2019, 3:40am UTC](https://forums.knack.com/t/draggable-table-rows/9226/4 "2019-03-15T03:40:43Z")

</div>

That's actually all built into the plugin. I'm not really doing any coding here - Just lazyloading someone else's work and then calling their functions.&nbsp; I've set to rows only because that's what I needed for my use case but you can do lots more with this. Check out the documentation. :)

[https://github.com/sindu12jun/table-dragger](https://github.com/sindu12jun/table-dragger)

&nbsp;

---

<div class="post-metadata">

**Author:** ![PhilWesley67576](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/philwesley67576/32/157_2.png) [@PhilWesley67576](https://forums.knack.com/u/PhilWesley67576)\
**Post date:** [March 15, 2019, 3:22am UTC](https://forums.knack.com/t/draggable-table-rows/9226/5 "2019-03-15T03:22:35Z")

</div>

@justin

Thanks for that, I get it now, nice. Be great to find a way to move columns or manually drag and drop column order too.  
  
This is a nice script I found to show and hide table columns has a cookie too to retain&nbsp;

I'll find the credit to author somewhere but this is nice

&nbsp;

// add a table column adjuster

$(document).on('knack-view-render.view\_35', function(event, view, data) {  
addTableColumnChoose(view);  
});

var addTableColumnChoose = function(view) {

var clearFix = $('#' + view.key + ' div.kn-records-nav.clearfix');  
clearFix.append("\<div class='clear'\>\<a href='#' class='choose-columns'\>Choose Columns\</a\>\</div\>");  
var hstring = getCookie("hstring\_"+view.key);  
if (hstring != ''){  
var headers = JSON.parse(hstring);  
$.each(headers, function(i, show) {  
var cssIndex = i + 1;  
var tags = $('#' + view.key + ' table th:nth-child(' + cssIndex + '), #' + view.key + ' table td:nth-child(' + cssIndex + ')');  
if (show)  
tags.show();  
else  
tags.hide();  
  
});  
}  
  
$('#' + view.key + ' .choose-columns').click(function() {  
// remove other open columns set dialog on the same page  
if( $('#tableChooseColumns')!=null) {$('#tableChooseColumns').remove();}  
var headers = $('#' + view.key + ' table th').map(function() {  
var th = $(this);  
return {text: th.text(), shown: th.css('display') != 'none'};  
});  
var hs;  
h = ['\<div id=tableChooseColumns\>\<table\>\<thead\>\<tr\>\<th style="text-align:center;padding:5px;"\>\<button id=done class="button\_a"\>OK\</button\>\</th\>\</tr\>\</thead\>\<tbody\>\<tr\>'];  
$.each(headers, function() {  
h.push('\<tr\>\<td style="padding:5px;"\>\<input type=checkbox',  
(this.shown ? ' checked ' : ' '),  
'/\> ',  
this.text,  
'\</td\>\</tr\>');  
});  
h.push('\</tr\>\</tbody\>\</table\>\</div\>');  
hs = h.join('');

$('body').append(hs);  
var pos = $('#' + view.key + ' .choose-columns').position();  
$('#tableChooseColumns').css({'position': 'absolute','left': '20px', 'top': pos.top,'padding': '5px', 'border': '1px solid #666', 'border-radius':'3px','background': '#fff'});  
$('#done').click(function() {  
var showHeaders = $('#tableChooseColumns input').map(function() { return this.checked; });  
var columns =[];  
$.each(showHeaders, function(i, show) {  
var cssIndex = i + 1;  
var tags = $('#' + view.key + ' table th:nth-child(' + cssIndex + '), #' + view.key + ' table td:nth-child(' + cssIndex + ')');  
if (show)  
tags.show(300,"swing");  
else  
tags.hide(300,"swing");  
columns.push(show);  
});  
  
$('#tableChooseColumns').remove();  
setCookie("hstring\_"+view.key,JSON.stringify(columns),100);

mixpanel.track("Preferences changes",{"Page":("Knack: "+view.scene.name), "View":view.title});  
return false;  
});  
return false;  
});  
}

/\* Cookie support -----------------------------------------------\*/

function setCookie(cname, cvalue, exdays) {  
var d = new Date();  
d.setTime(d.getTime() + (exdays\*24\*60\*60\*1000));  
var expires = "expires="+ d.toUTCString();  
document.cookie = cname + "=" + cvalue + "; " + expires;  
}

function getCookie(cname) {  
var name = cname + "=";  
var ca = document.cookie.split(';');  
for(var i = 0; i \<ca.length; i++) {  
var c = ca[i];  
while (c.charAt(0)==' ') {  
c = c.substring(1);  
}  
if (c.indexOf(name) == 0) {  
return c.substring(name.length,c.length);  
}  
}  
return "";  
}

// handler to dismiss dialog just with outside click  
$(document).click(function(event) {   
if(!$(event.target).closest('#tableChooseColumns').length) {  
if($('#tableChooseColumns').is(":visible")) {  
$('#tableChooseColumns').remove();  
}  
}   
})

---

<div class="post-metadata">

**Author:** ![Justin11773](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@Justin11773](https://forums.knack.com/u/Justin11773)\
**Post date:** [March 15, 2019, 12:37am UTC](https://forums.knack.com/t/draggable-table-rows/9226/6 "2019-03-15T00:37:54Z")

</div>

Hi Phil -Just looked at your app. It's working. You need to grab the first field from the left before dragging. When it turns red you know the script is running. Cheers

---

<div class="post-metadata">

**Author:** ![PhilWesley67576](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/philwesley67576/32/157_2.png) [@PhilWesley67576](https://forums.knack.com/u/PhilWesley67576)\
**Post date:** [March 14, 2019, 10:24pm UTC](https://forums.knack.com/t/draggable-table-rows/9226/7 "2019-03-14T22:24:07Z")

</div>

This looks like it would be useful, thanks. But cant get it to work on mockup [https://wasabi.knack.com/address#home/](https://wasabi.knack.com/address#home/)

---

<div class="post-metadata">

**Author:** ![BradStevens](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/bradstevens/32/140_2.png) [@BradStevens](https://forums.knack.com/u/BradStevens)\
**Post date:** [March 13, 2019, 10:15am UTC](https://forums.knack.com/t/draggable-table-rows/9226/8 "2019-03-13T10:15:21Z")

</div>

Thanks for sharing Justin!

---

<div class="post-metadata">

**Author:** ![MichaelMoo15586](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/michaelmoo15586/32/1569_2.png) [@MichaelMoo15586](https://forums.knack.com/u/MichaelMoo15586)\
**Post date:** [March 7, 2019, 11:03am UTC](https://forums.knack.com/t/draggable-table-rows/9226/9 "2019-03-07T11:03:35Z")

</div>

Justin, thanks for this. It worked first time, nice post.

&nbsp;

M

&nbsp;
