This is a migrated thread and some comments may be shown as answers.

SignalR with sorting, Create, Destroy or Update

4 Answers 317 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Larry
Top achievements
Rank 1
Larry asked on 23 Jul 2016, 05:35 PM

I have a list view that has a SignalR datasource. I load the initial data fine and the sorting works properly on the timestamp. What I am trying to do, is update the list with either new items or an updated timestamp on an existing item using SignalR.

If I simply update a new item using the update method, I can see the timestamp change, but it stays in the same position. To work around this, I do a Destroy and then a Create. This works perfectly. The problem  I am having, is that when a new item is sent it doesn't get inserted in to the list. I don't see any Javascript errors and I see the proper SignalR communication using the browser developer tools.

4 Answers, 1 is accepted

Sort by
0
Larry
Top achievements
Rank 1
answered on 23 Jul 2016, 08:10 PM
I did some more checking. It appears that the create event always fires first. So when a new item is created, it is destroyed right after. The reason why it works with existing items is that the destroy method always seems to be matching on the first instance of the"record" in the datasource. There has to be a reliable way to handle something like updating/inserting sorted records for creating a monitoring dashboard.
0
Boyan Dimitrov
Telerik team
answered on 27 Jul 2016, 08:05 AM

Hello Larry,

Could you please share some code or a dojo example which demonstrates your exact case? 

Regards,
Boyan Dimitrov
Telerik by Progress
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
0
Larry
Top achievements
Rank 1
answered on 27 Jul 2016, 07:46 PM

Simple hub:

public class TestHub : Hub
{
    public void UpdateData()
    {
        var r = new Random();
        Clients.All.customerHit(new { Id = r.Next(1,5), HitTime = DateTime.Now });
    }
 
    public dynamic InitData()
    {
        var Now = DateTime.Now;
        return new[]
        {
            new { Id = 1, HitTime = Now.AddHours(-2) },
            new { Id = 2, HitTime = Now.AddMinutes(-2) },
            new { Id = 3, HitTime = Now.AddDays(-2) }
        };
    }
}

Razor Page:

<script src="~/signalr/hubs"></script>
<script>
    var testHub, hubStart, connection;
 
    $(function () {
        testHub = $.connection.testHub;
        testHub.client.customerHit = function (data) {
            $("#LastUpdate").text("Id: " + data.Id + " Time: " + kendo.toString(data.HitTime, "h:mm:ss"))
        };
        hubStart = $.connection.hub.start()
            .done(function () {
                $("#update").click(function () { testHub.server.updateData(); })
            });
    });
</script>
 
<button type="button" id="update">Update</button>
 
Update from SignalR: <span id="LastUpdate"></span>
 
<script type="text/x-kendo-tmpl" id="lvTemplate">
    <div>Id: #:Id#: #:kendo.toString(HitTime,'h:mm:ss')#</div>
</script>
 
@(Html.Kendo().ListView<dynamic>()
    .Name("lvTest")
    .TagName("lvTest")
    .ClientTemplateId("lvTemplate")
    .DataSource(dataSource => dataSource
        .SignalR()
        .ServerSorting(false)
        .Sort(s => s.Add("HitTime").Descending())
        .Schema(s => s.Model(m =>
        {
            m.Id("Id");
            m.Field("HitTime", typeof(DateTime));
        }))
        .Transport(tr => tr
            .Promise("hubStart")
            .Hub("testHub")
            .Client(c => c.Update("customerHit")) //Does not re-sort the list when an item is updated
            //.Client(c => c.Create("customerHit").Destroy("customerHit")) //Re-sorts the list but no new items can be added
            .Server(c => c.Read("initData"))
        )
    )
)

0
Boyan Dimitrov
Telerik team
answered on 01 Aug 2016, 09:03 AM

Hello Larry,

Have you tried to use the refresh method of the Kendo UI Grid. This will render all table rows using the current data items. 

Regards,
Boyan Dimitrov
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ListView
Asked by
Larry
Top achievements
Rank 1
Answers by
Larry
Top achievements
Rank 1
Boyan Dimitrov
Telerik team
Share this question
or