Home > Enterprise >  Logic for Sortable table using Drag and Drop in Django
Logic for Sortable table using Drag and Drop in Django

Time:07-06

I am trying to create a Sortable Table function, in which i am updating the position of the ids as per the given request:

def put(self, request):
    max_len = Device.objects.count()
    ids = int(request.query_params.get('id'))
    temp = ids
    old = int(request.query_params.get('old'))
    new = int(request.query_params.get('new'))

    if new == old:
        return Response("both are equal can't go ahead")

    elif new > max_len:
        return Response("Invalid Position")

    elif old < new:
        t = Device.objects.get(id=ids)
        t.position = 0
        print("new position Updated to 0")
        t.save()

        for i in range(old   1, new   1, 1):
            ids = ids   1
            print(ids)
            t = Device.objects.get(id=ids)
            print(t)
            t.position = t.position - 1
            print(t.position)
            t.save()
            print(i, "value saved")
        t = Device.objects.get(id=temp)
        t.position = new
        print("new position Updated with ", t.position)
        t.save()
        print("new position saved")
        return Response("old < New")

    elif old > new:
        t = Device.objects.get(id=ids)
        t.position = 0
        print("new position Updated to 0")
        t.save()

        for i in range(old - 1, new - 1, -1):
            ids = ids - 1
            print(ids)
            t = Device.objects.get(id=ids)
            print(t)
            t.position = t.position   1
            print(t.position)
            t.save()
            print(i, "value saved")
        t = Device.objects.get(id=temp)
        t.position = new
        print("new position Updated with ", t.position)
        t.save()
        print("new position saved")
        return Response("old > New")

Need help to make it more efficient, currently after two or three times value of positions starts redundant may you please run and test and suggest me the way to do that.

CodePudding user response:

As I have mentioned in my comment I think this approach may lead to some errors, so I would suggest you change your request a little bit.

Maybe do the calculations on the front end and then send an object with the new positions and ids and then process that?

request body could look like this

[
  {
    "id":1,
    "position":2,
  },
  {
    "id":2,
    "position":3,
  }, ... ,
  {
    "id":10,
    "position":1,
  }
]

and then you could iterate on this list and assign each device to it's new position

for item in request.data:
    d = Device.objects.get(item['id'])
    d.position = item['position']
    d.save()

CodePudding user response:

You may want to rethink this since the state on the server side may not match the client when multiple clients are using this. (ie. the device in "position 2" changes depending on what requests came before)

One way is to always send the full list of device ids and renumber the positions according to that list.

Another way is to reposition relative to other devices. So, send a request saying "place Device id=3 after Device id=5" and then reposition all the devices accordingly (and probably respond with the updated list to update the client side).

  • Related