0

I have a jquery table with property set to sortable( I can drag and drop rows),

<script type="text/javascript">
    $(function () {
        $("#tblLookup1 tbody").sortable({
            items: 'tr',
            cursor: 'pointer',
            axis: 'y',
            dropOnEmpty: false,
            start: function (e, ui) {
                ui.item.addClass("selected");
            },

            stop: function (e, ui) {
                $("#tblLookup1 tbody tr").each((idx, tr) => {
                    $("td:nth-child(4)", tr).text(idx+1);
                });
                ui.item.removeClass("selected");
               
            },
            receive: function (e, ui) {
                $(this).find("tbody").append(ui.item);
            }
        });
    });

Now after sorting a column with the name preference gets updated, 1st row will have preference value=1, 2nd row =2 and so on...

Now these values are available until I refresh the page. So as to make permanent changes I want to update the database with the new preference values.
This is a .net MVC ado based web app
Ques) How can I store these preference values into some list and later pass on/post to sql database.

view file code

                    <thead>
                        <tr >
                            <th>ID</th>
                            <th>Name</th>
                            <th>Active</th>
                            <th>Order By</th>
                        </tr>
                    </thead>
                    <tbody>
                        @if (ViewBag.data != null)
                        {
                            foreach (var item in ViewBag.data)
                            {
                                <tr>
                                    <td>@item.AutoID</td>
                                    <td>@item.Text<br </td>
                                    <td >@item.Active</td>                                      
                                    <td>@item.OrderBy</td>
                                </tr>

                            }
                        }
                    </tbody>
3
  • 1
    Store each value in a different input per row, all with the same name='OrderBy[]` - <input name='OrderBy[]'>` / wrap the whole table in a <form> and submit the form. Some more info on posting array objects is here: haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx Commented Jun 23, 2022 at 16:18
  • Use a for instead of a foreach with inputs or helpers inside the cell tags Commented Jun 23, 2022 at 16:21
  • Welcome to Stack Overflow. Please see api.jqueryui.com/sortable/#method-serialize or api.jqueryui.com/sortable/#method-toArray These can help you create the Object or Array to POST back to your script. The script will need to accept the POST and add it to the Data base or update the OrderBy column. Commented Jun 23, 2022 at 16:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.