I am using C# ASP MVC, JavaScript and jQuery for my web application.
I currently have a grid where you can click a row to select the item. Once you click the item, there is a css class named .highlight
that makes sure the item is selected.
When I select the item and click on a button, the grid refreshes, that is good as well. But now I am wondering how I can make it re-select the same item before it refreshes the page?
Here is what I've tried:
var $this = $selectedRow;
if ($this.hasClass("row-simple")){
// $(".highlight").removeClass("highlight");
$this.addClass("highlight");
}
After we click the button it will check if there is a row selected and do this Ajax request. You can see at the end of the function that I am actually trying to select the selected item again.
function StartToTravel() {
var $selectedRow = $(".highlight");
if ($selectedRow.length == 1) {
var id = $selectedRow.children(".td-dc")[0].innerText.trim();
if (id) {
$.ajax({
type: 'post',
url: appPath + '/service/StartTrip',
data: {
id: id
},
success: function (response) {
if (response.success) {
RefreshGrid();
}
$("#dialog .modal-body").html(response.message);
$("#dialog #dialog-title").html(response.title);
$("#dialog").modal("show");
},
error: function (response) {
$("#dialog .modal-body").html(msgErrorDuringRequest);
$("#dialog #dialog-title").html(errorTitle);
$("#dialog").modal("show");
}
});
var $this = $selectedRow;
if ($this.hasClass("row-simple") || $this.hasClass("highlight")) {
// $(".highlight").removeClass("highlight");
$this.addClass("highlight");
}
}
}
}
The function for refreshing:
function RefreshGrid() {
var numberPlate = $("#NumberPlate").val();
var url = '/Service/Grid?numberPlate='+numberPlate;
$("#Grid").load(url);
}