Trigger controller action when selecting item in table using KODataTable MVC

I am representing data in a table by invoking an action (which returns a list in Json) via an AJAX call.

Output: http://imageshack.com/a/img673/3484/zxZIqy.png

I would like to make each user (rows in the table) bind to the edit page (Admin / Edit / Id). Either by simply clicking on them, or using the "Edit" link at the end of each line. I don't know how to achieve this. This would be easy with normal razor syntax. But these patterned seams are well suited for achieving this kind of dynamic data.

I am working with the KODataTable template to make this table searchable and sorted.

View ..

<div id="kodt">
    <div>
        <input type="text" data-bind="value: searchText, valueUpdate: 'afterkeydown'" />
        <select data-bind="value: selectedColumn, options: columns"></select>
        <button data-bind="click: search">Search</button>
    </div>
    <div>
        <table class="table table-striped table-hover">
            <thead>
                <tr data-bind="foreach: columns">
                    <th data-bind="text: $data, click: function() { $parent.sort($index()) }" style="cursor: pointer"></th>
                </tr>
            </thead>
            <tbody data-bind="foreach: currentRows">
                <tr data-bind="foreach: $parent.columns, click: function () { $root.selectRow($data); }, css: { 'success': $root.selectedRow() == $data }">
                    <td data-bind="text: $parent[$data]" style="cursor: pointer; text-align: center"></td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <button data-bind="click: firstPage">First</button>
        <button data-bind="click: prevPage">Prev</button>
        Page <span data-bind="text: currentPage() + 1"></span> of <span data-bind="text: pageCount"></span>
        <button data-bind="click: nextPage">Next</button>
        <button data-bind="click: lastPage">Last</button>
    </div>
</div>

      

Script ..

<script>
    var users = new Object();
    $.getJSON("/Admin/GetUsers", function (data) {
        users = data;
        var TableDataVM = new KODataTable({
            columns: ["Id", "Username", "RoleId", "CompanyId", ""],
            rows: users,
        });
        ko.applyBindings(TableDataVM, document.getElementById("kodt"));
    });
</script>

      

+3


source to share


1 answer


Right now, it looks like when the user clicks on a row, it gets called $root.selectRow($data);

which passes the data of the row object to some function in the ViewModel called selectRow()

. If this function exists, you can use it on a $.post

string (representing an object user

) for the MVC controller in that function, and use the response to redirect to the Edit view.



var vm = function() {
    var self = this;
    var selectRow = function(rowClicked) {

        var data = {
           Id = rowClicked.Id,
           Username = rowCicked.Username,
           // etc.
        };

        // post the data to some MVC controller - something remotely like this
        $.ajax({
            url: '/Admin/Edit/' + rowClicked.Id,
            data: ko.toJSON(data),
            type: 'POST',
            success: function (result) {
                window.location.href = result.url;
            }
        });
    };
};

      

+1


source







All Articles