So what I'm trying to do, and I've made a small example project here, is when a user is editing a cell in a table, the cell disables for everyone else on that page. All good. Here I set it up so that when the user enters a cell, it disables for everyone else, and on blur/exiting the cell, it clears. Now the problem is, if a person is in a cell, it disables on other people's screens. But if anyone refreshes or if a new user goes on the page, it is not disabled, even if the main user is still in that cell. Is there a way with SignalR to know that someone is using a particular cell while he's using it, and not just when he enters/exits the cell?
C# Code:
public class ChatHub : Hub
{
public void Send(string name, string message, bool boolean)
{
// Call the broadcastMessage method to update clients.
Clients.Others.broadcastMessage(name, message, boolean);
}
}
HTML Code:
<table id="table">
<thead>
<tr>
<th>HeaderOne</th>
<th>HeaderTwo</th>
<th>HeaderThree</th>
</tr>
</thead>
<tbody>
@for(int i = 0; i < 3; i++)
{
<tr>
<td><input class="tdInput" /></td>
<td><input class="tdInput" /></td>
<td><input class="tdInput" /></td>
</tr>
}
</tbody>
</table>
Javascript Code:
$(function () {
var conn = $.connection.chatHub;
conn.client.broadcastMessage = function (col, row, boolean) {
var cell = $("#table tr:eq(" + row + ") td:eq(" + col + ")");
cell.find("input").prop('disabled', boolean);
};
$.connection.hub.start().done(function () {
$(".tdInput").on('focus', function () {
var col = $(this).parent().index();
var row = $(this).closest('tr').index() + 1;
conn.server.send(col, row, true);
});
$(".tdInput").on('blur', function () {
var col = $(this).parent().index();
var row = $(this).closest('tr').index() + 1;
conn.server.send(col, row, false);
});
});
});
Here's a simple implementation of option #2 from Christoph's comment:
In connection.hub.start(), add:
conn.server.refresh();
In the JS:
conn.client.resendStatus = function () {
if ($('input:focus').length > 0) {
var focused = $(":focus");
var col = focused.parent().index();
var row = focused.closest('tr').index() + 1;
conn.server.send(col, row, true);
}
};
In the Hub:
public void Refresh()
{
Clients.Others.resendStatus();
}