Martin Overgaard
Martin Overgaard

Reputation: 355

Change sortorder in database using jstree and asp.net mvc

I'm trying to change the sortorder in a database table looking like this :

id parentid sitename sortorder
1  0        home     0 
2  0        about    1 
3  0        product  2 
4  0        contact  3

Now when i drag "home" down between "product" and "contact" using jstree, I need to change the sortorder of all 4 sites. The thing i know when I drop the "home" site is the id, parentid and the new sortorder-index, but how would my update linq sql look like to get this to work?

Upvotes: 1

Views: 625

Answers (1)

FloydThreepwood
FloydThreepwood

Reputation: 1585

With a Adjacency-Model you do not have much choice, but need to select and then update each sibling.

SELECT id, sortorder FROM table WHERE parentid = _MOVED ELEMENT PARENT ID_ ORDER BY sortorder DESC

This will give you all sibblings of this element. Now you need to reorder the result array according to your input. Search for the moved element and move in the array hierarchy.

The update the nodes and you are done with a sortorder (a simple counter should be enough) and you are done.

I could provide you some pseudocode, but ASP is not my language. Hope the idea can help too.

Update

PHP it is.

//search reorder element
$movedElementOldOrderIndex = $movedElementFromDb['sortorder'];
//target array
$newOrderedElements = array();
foreach($elementsFromDb as $order => $el) {
   //duplicate detection
   if($order == $movedElementOldOrderIndex) {
        continue;
   }

   //reordering is here
   if($movedElementNewOrderIndex == $order) {
       $newOrderedElements[] = $elementsFromDb[$movedElementOldOrderIndex];
   }

   //push all other elements
   $newOrderedElements[] = $el;
}

This will give you a array, where the index == sortorder and you can update your database records accordingly.

UPDATE

$movdeElementFromDB - actual db element that gets moved in request

$el - is one sibling of the current element / result of query above

Upvotes: 1

Related Questions