<ej-Button ID="refresh" Type="Button" Text="Refresh4" click="refresh" />
<ej-grid id="FlatGrid" allow-sorting="true" allow-paging="true" allow-reordering="true">
<e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" show-delete-confirm-dialog="true" edit-mode="@(EditMode.Normal)"></e-edit-settings>
<e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","update","cancel","excelExport","wordExport","pdfExport" })"></e-toolbar-settings>
<e-datamanager json="(IEnumerable<object>)Model" update-url="/Employees/Edit" insert-url="/Employees/Create" remove-url="/Employees/Delete" adaptor="remoteSaveAdaptor" />
<e-columns>
<e-column field="EmployeeID" header-text="EmployeeID" text-align="Left" width="70" is-primary-key="true" type="number"></e-column>
<e-column field="Name" header-text="Name" text-align="Left" width="70"></e-column>
<e-column field="LastName" header-text="Last Name" text-align="Left" width="80" tooltip="#colTip" clip-mode="EllipsisWithTooltip"></e-column>
<e-column field="DOB" header-text="Birth Date" text-align="Left" width="80" edit-type="Datepicker" format="{0:MM/dd/yyyy}"></e-column>
<e-column field="DepartmentID" datasource="(IEnumerable<object>)ViewBag.DataSource2"
header-text="Department" foreign-key-field="ID" foreign-key-value="Name" text-align="Left"
width="80" edit-type="@(EditingType.Dropdown)"></e-column>
<e-column header-Text="Manage Records" width="100">
<e-column-commands>
<e-column-command type="edit">
<e-button-options content-type="TextOnly" text="Edit"></e-button-options>
</e-column-command>
<e-column-command type="delete">
<e-button-options content-type="TextOnly" text="Delete"></e-button-options>
</e-column-command>
<e-column-command type="save">
<e-button-options content-type="TextOnly" text="Save"></e-button-options>
</e-column-command>
<e-column-command type="cancel">
<e-button-options content-type="TextOnly" text="Cancel"></e-button-options>
</e-column-command>
</e-column-commands>
</e-column>
</e-columns>
</ej-grid>
<script type="text/javascript">
function refresh() {
var gridObj = $("#FlatGrid").data("ejGrid");
gridObj.refreshContent(true);
}
</script>
What is the correct way to refresh the grid data from the server without refreshing the full page? Ajax?
The CRUD operations in the grid work just fine. All I need is a way to refresh my grid every 30 seconds, to retrieve data from the server. According to your documentation this should do it.
<script type="text/javascript">
function refresh() {
var gridObj = $("#FlatGrid").data("ejGrid");
gridObj.refreshContent(true);
}
</script>
But it is not refreshing the data from the server.
@Grid
<ej-Button ID="refresh" Type="Button" Text="Refresh4" click="refresh" />
<ej-grid id="FlatGrid" allow-sorting="true" allow-paging="true" allow-reordering="true">
<e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" show-delete-confirm-dialog="true" edit-mode="@(EditMode.Normal)"></e-edit-settings>
<e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","update","cancel","excelExport","wordExport","pdfExport" })"></e-toolbar-settings>
<e-datamanager url="/Home/DataSource" update-url="/Home/Edit" insert-url="/Home/Create" remove-url="/Home/Delete" adaptor="UrlAdaptor" />
<e-columns>
<e-column field="OrderID" header-text="Order ID" text-align="Left" width="70" is-primary-key="true" type="number"></e-column>
<e-column field="EmployeeID" header-text="EmployeeID" text-align="Left" width="70" type="number"></e-column>
<e-column field="CustomerID" header-text="Name" text-align="Left" width="70"></e-column>
</e-columns>
</ej-grid>
<script type="text/javascript">
function refresh() {
var gridObj = $("#FlatGrid").data("ejGrid");
gridObj.refreshContent(true);
}
</script> |
@Grid
<ej-Button ID="refresh" Type="Button" Text="Refresh4" click="refresh" />
<ej-grid id="FlatGrid" allow-sorting="true" load="onLoad" allow-paging="true" allow-reordering="true">
. . .
<e-columns>
. . .
</e-columns>
</ej-grid>
<script>
function onLoad(args) {
$("#"+this._id).ejWaitingPopup({ template: " " }); //Disable the default waiting popup shown while fetching the data
}
</script>
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Chat
{
public class Startup
{
. . .
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
. . .
app.UseStaticFiles();
app.UseSignalR();
. . .
}
}
|
{
"name": "asp.net",
"private": true,
"dependencies": {
"bootstrap": "3.3.7",
"jquery": "2.2.0",
"jquery-validation": "1.14.0",
"jquery-validation-unobtrusive": "3.2.6",
"signalr": "2.2.1",
}
}
|
<ej-grid id="Editing" datasource="@ViewBag.data" action-complete="actionComplete"allow-paging="true" data-bound="dataBound">
<e-edit-settings allow-adding="true" allow-deleting="true" allow-editing="true"></e-edit-settings>
<e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","cancel","update" })"></e-toolbar-settings>
<e-columns>
<e-column field="OrderID" is-primary-key="true" header-text="Order ID" text-align="Right" width="70"></e-column>
<e-column field="CustomerID" header-text="Customer ID" width="80"></e-column>
<e-column field="EmployeeID" header-text="Employee ID" text-align="Left"width="75"></e-column>
<e-column field="Freight" header-text="Freight" text-align="Right" format="{0:C2}" width="75"></e-column>
</e-columns>
</ej-grid>
<script src="~/lib/signalr/jquery.signalR.min.js"></script>
<script src="signalr/hubs"></script>
|
. . .
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Hubs;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Chat.Controllers
{
public class SignalRTestHub : Hub
{
public void Modify(string name, string action, Object data)
{
Clients.Others.modify(name, action, data);
}
}
}
|
<ej-grid id="Editing" datasource="@ViewBag.data" action-complete="actionComplete"allow-paging="true" data-bound="dataBound">
. . .
<e-columns>
. . .
</e-columns>
</ej-grid>
<script src="~/lib/signalr/jquery.signalR.min.js"></script>
<script src="signalr/hubs"></script>
window.signal = $.connection.signalRTestHub;
window.signal.client.modify = function (userIp, action, details) {
if (action == "delete") {
var gridObj = $("#Editing").ejGrid("instance");
var dm = ej.DataManager(gridObj.model.dataSource);
var data = dm.executeLocal(ej.Query().where("OrderID", ej.FilterOperators.equal, details.OrderID))
if (data.length)
$("#log").append("<li>" + ej.format(new Date(), "hh:mm:ss") + " : " + userIp + " has " + action + " a record with OrderID =" + details.OrderID + "</li>");
}
else
$("#log").append("<li>" + ej.format(new Date(), "hh:mm:ss") + " : "+ userIp + " has " + action + " a record with OrderID =" + details.OrderID + "</li>");
if (action == "add")
$("#Editing").ejGrid("addRecord", details);
else if (action == "beginedit") {
$("#Editing").ejGrid("updateRecord", "OrderID", details);
}
else
$("#Editing").ejGrid("deleteRecord", "OrderID", details);
};
$.connection.hub.start().done(function () {
window.actionComplete = function (args) {
if (args.requestType == "save" || args.requestType == "delete")
window.signal.server.modify($("#userName").text(), args.requestType == "delete" ? args.requestType : window.previousAction, args.data);
if (args.requestType != "delete")
window.previousAction = args.requestType;
}
}); |
This is great. Thank You!
I updated this code to ASP.net core 2.0. Since I need to write to a database a changed the grid code to:
<ej-grid id="objGrid" action-complete="actionComplete" allow-paging="true" data-bound="dataBound">
<e-edit-settings allow-adding="true" allow-deleting="true" allow-editing="true"></e-edit-settings>
<e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","cancel","update" })"></e-toolbar-settings>
<e-datamanager id="objDataMgr" json="(IEnumerable<object>)ViewBag.DataSource1" update-url="/Employees/Edit2" insert-url="/Employees/Create" remove-url="/Employees/Delete" adaptor="remoteSaveAdaptor" />
<e-columns>
<e-column field="EmployeeID" is-primary-key="true" header-text="EmployeeID" text-align="Right" width="70"></e-column>
<e-column field="Name" header-text="Name" width="80"></e-column>
<e-column field="LastName" header-text="LastName" text-align="Left" width="75"></e-column>
</e-columns>
</ej-grid>
Changed the java scrit for:
<script type="text/javascript">
window.signal = $.connection.signalRHub;
window.signal.client.modify = function (userIp, action, details) {
if (action == "delete") {
var gridObj = $("#objGrid").ejGrid("instance");
//var dm = ej.DataManager(gridObj.model.dataSource);
var dm = ej.DataManager($("#objDataMgr"));
var data = dm.executeLocal(ej.Query().where("EmployeeID", ej.FilterOperators.equal, details.EmployeeID))
}
if (action == "add")
$("#objGrid").ejGrid("addRecord", details);
else if (action == "beginedit") {
$("#objGrid").ejGrid("updateRecord", "EmployeeID", details);
}
else
$("#objGrid").ejGrid("deleteRecord", "EmployeeID", details);
};
$.connection.hub.start().done(function () {
window.actionComplete = function (args) {
if (args.requestType == "save" || args.requestType == "delete")
window.signal.server.modify($("#userName").text(), args.requestType == "delete" ? args.requestType : window.previousAction, args.data);
if (args.requestType != "delete")
window.previousAction = args.requestType;
}
});
</script>
It is working and if I open three browsers the three get refreshed. The only problem is that the update method in the server side is called as many times as clients I have open. How can I prevent that?
Thanks,
if (action == "add")
$("#objGrid").ejGrid("addRecord", details);
else if (action == "beginedit") {
$("#objGrid").ejGrid("updateRecord", "EmployeeID", details);
}
else
$("#objGrid").ejGrid("deleteRecord", "EmployeeID", details);
};
</script>
|
I see what you are saying the problem is that if I comment out the code inside:
window.signal.client.modify = function (userIp, action, details) {
}
The changes are saved to the server but the other clients are not refreshed. Can you please provide a sample using your suggestion remoteSaveAdaptor. What is the code I have to use in window.signal.client.modify to refresh the grid data ?
Thanks.
Is it possible to refresh a datasource when using remoteSaveAdaptor? I was told in another forum thread that based on my editing needs, I have to use this type of adaptor. However, I am not able to get "refreshContent" to work for me. I have two grids on my page that relate to each other. Changing the content of one grid will effect what exists in the other grid, so I need to be able to refresh the datasources to see those changes. The only way that I am currently able to do that is by refreshing the entire page, which will completely rebuild the datasources in the controller. This is not ideal, because of the time it takes to refresh.
<ej-grid id="Grid1" allow-paging="true" action-complete="onActionComplete">
<e-datamanager json="ViewBag.datasource" update-url="/Home/UpdateUrl" adaptor="remoteSaveAdaptor"/>
</ej-grid>
<ej-grid id="Grid2" allow-paging="true" action-complete="onActionComplete">
</ej-grid>
<script>
function onActionComplete(args) {
//while request type is save
if (args.requestType == "save") {
//collect dataSource
var src = this.model.dataSource.dataSource.json;
var id = this._id == "Grid1" ? "#Grid2" : "#Grid1";
//update the another Grid
$(id).ejGrid("dataSource", src);
}
}
</script> |
ej-grid id="MasterGrid" allow-paging="true" action-begin="actionBegin" action-complete="complete" row-selected="rowSelected" row-deselected="rowDeselected">
<e-datamanager json="@ViewBag.master" update-url="Home/NormalUpdate" insert-url="Home/NormalInsert" remove-url="Home/NormalDelete" adaptor="remoteSaveAdaptor" />
<e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" show-delete-confirm-dialog="true" edit-mode="@(EditMode.Normal)"></e-edit-settings>
<e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","update","cancel" })"></e-toolbar-settings>
<e-columns>
<e-column field="Name" header-text="Name" width="80"></e-column>
</e-columns>
</ej-grid>
<ej-grid id="DetailGrid" allow-paging="true" databound="dataBound" action-begin="actionBegin" action-complete="complete" row-selected="rowSelected"
<e-datamanager json="@ViewBag.details" update-url="Home/NormalUpdate" insert-url="Home/NormalInsert" remove-url="Home/NormalDelete" adaptor="remoteSaveAdaptor" />
<e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" show-delete-confirm-dialog="true" edit-mode="@(EditMode.Normal)"></e-edit-settings>
<e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","update","cancel" })"></e-toolbar-settings>
</ej-grid>
<script src="~/Scripts/jsondata.min.js"></script>
<script type="text/javascript">
$(function () {
window.rowSelected = function (args) {
var employeeID = args.data.EmployeeID;
var detaildata = ej.DataManager(window.gridData).executeLocal(ej.Query().where("EmployeeID", ej.FilterOperators.equal, employeeID, false).take(10));
var gridObj = $("#DetailGrid").ejGrid("instance");
gridObj.dataSource(ej.DataManager(detaildata.slice(0, 5)));
}
}); |