Have recently upgraded from a 2013 to 2015 versions of Kendo UI for MVC and am having a bit of an issue with datasource. Can anyone point me at a place in the docs or elsewhere for a few examples of the differences.
Here is one link
http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-data
eg: The 2013xxx version way of setting the data
MyDataSource.data(data); //where data was jason array returned from ajax
the 2015 way, at least that works but not sure if right
MyDataSource.data = data;
Also, as for setting values of dataitems, they too seem to have a different approach?
Old way:
var MyDataSource = $('#SalesGrid').data().kendoGrid.dataSource;
var raw = statementBatchDS.data; //raw will be undefined with 2015 version ???
var length = raw.length;
// iterate and reset items
var item, i;
for (i = length - 1; i >= 0; i--) {
item = raw[i];
item.set("SaleAmount", 45.22);
//more code...
}
Now if I change the code to use Data, I get the data items, but then the dataitem set methods fail
Kendo UI Datasource has method data. Its not property so you use it as function. Data method gets or sets the data items of the data source.
var items = [
{ name: "Jane Doe" },
{ name: "John Doe" }
];
var dataSource = new kendo.data.DataSource({});
dataSource.data(items);
var data = dataSource.data();
So, change this line
var raw = statementBatchDS.data;
To this
var raw = statementBatchDS.data();
Related
I am getting the following error when I try to update tree of object using asp.net webapi OData:
"UpdateRelatedObject method only works when the sourceProperty is not collection."
My code is provided below. I got this error when the mehod "UpdateRelatedObject" is called. Can you please advise what is wrong with my code and how to update tree of objects (meaning object contains collection of child objects) using asp.net webapi odata v4.
var container = new Container(new Uri("http://JohnAlbert.com/MyOdataTest/odata"));
Product product = container.Products.Expand(p=> p.ProductItems).Expand(p=>p.ProductInvoices).Where(p => p.PId == Guid.Parse("28C508B8-F2DC-45C2-B401-7F94E79AB347")).FirstOrDefault();
if (product != null)
{
product.Name = product.Name + "_Modified";
var pitem1 = product.ProductItems[0];
product.ProductItems.Remove(pitem1);
container.UpdateRelatedObject(product, "ProductItems", pitem1);
var pitem2 = product.ProductItems[0];
pitem2.Price = 999;
container.UpdateRelatedObject(product, "ProductItems", pitem1);
var pInv1 = product.ProductInvoices[0];
product.ProductInvoices.Remove(pInv1);
container.UpdateRelatedObject(product, "ProductInvoices", pInv1);
}
container.UpdateObject(product);
container.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset);
What you actually want to delete the relationship between some items in a collection-valued navigation property of an entity and itself. In such case DeleteLink() is the right method to use. In this case the following code should do the work:
if (product != null)
{
var pitem1 = product.ProductItems[0];
var pitem2 = product.ProductItems[0];
var pInv1 = product.ProductInvoices[0];
container.DeleteLink(product, "ProductItems", pitem1);
container.DeleteLink(product, "ProductItems", pitem2);
container.DeleteLink(product, "ProductInvoices", pInv1);
container.SaveChanges();
}
You may think the above way isn't as intuitive as directly removing the navigation items from the entity using .Remove() as you did. For this problem, the entity tracking enabled by using DataServiceCollection<T> can help. You can use this blog post as a tutorial for how to use DataServiceCollection<T>: http://blogs.msdn.com/b/odatateam/archive/2014/04/10/client-property-tracking-for-patch.aspx
To delete a contained navigation property, you can use DataServiceContext.DeleteObject.
To delete a relationship between an entity and its navigation property, you can use DataServiceContext.DeleteLink
To update an object, you can use DataServiceContext.UpdateObject.
So according to your scenario, you could use following code
if (product != null)
{
product.Name = product.Name + "_Modified";
dsc.UpdateObject(product);
var pitem1 = product.ProductItems[0];
//This is to remove relationship
container.DeleteLink(product, "ProductItems", pitem1);
// This is to remove the object
//container.DeleteObject(pitem1);
var pitem2 = product.ProductItems[0];
pitem2.Price = 999;
container.UpdateObject(pitem2);
var pInv1 = product.ProductInvoices[0];
//This is to remove relationship
container.DeleteLink(product, "ProductInvoices", pInv1);
// This is to remove the object
//container.DeleteObject(pInv1);
container.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset);
}
I am using DotNet.Highcharts in conjunction with Visual Studio 2010. I have created an array of Series:
List<Series> allSeries = new List<Series>();
I then looped through a database and added several different Series. Then I created a Highchart and need to add the allSeries list to it. I have the code below that I used to create a Series one at a time. How can I take the allSeries list and pass it to SetSeries?
.SetSeries(new[]
{
new Series { Name = "Combiner 2", Data = new Data(myData2) },
new Series { Name = "Combiner 3", Data = new Data(myData3) }
});
if I am left to assume that the myData2 and myData3 objects are contained in or could be extracted from allSeries, then you should be able to do something like this:
.SetSeries(allSeries.Select(s=> new Series { Name = s.Name, Data = s.Data }));
EDIT:
If set series isn't looking for an IEnumerable<Series> but instead needs Object[] or Series[], then you could do this:
//casts series elements to object, then projects to array
.SetSeries(allSeries.Select(s=> (object)new Series { Name = s.Name, Data = s.Data }).ToArray());
or maybe this:
//projects series elements to array of series
.SetSeries(allSeries.Select(s=> new Series { Name = s.Name, Data = s.Data }).ToArray());
it all depends on what the method signature for SetSeries is.
As part of an EvenReceiver the itemAdded on a custom List (source) creates a Calendar entry in another List (target).
I now want to add an itemUpdated event so that when the the source List is updated the change is filtered through to the target List.
I am using c# in Visual Studio to develop the Event Receiver.
Can anyone please advise the best way to do this and how I create the link between the two Lists to ensure I can update from source to target?
Thank you.
You will have to udpate the target list yourself...
var sourceItem = this.properties.ListItem;
//you can use other properties to search for the item in the targetlist aswell
string query = string.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>", sourceItem.Title);
var spQuery = new SPQuery() { Query = query };
var foundItems = targetList.GetItems(spQuery);
if(foundItems.Count == 1)
{
var foundItem = foundItems[0];
//update the properties you want
foundItem["Property1"] = sourceItem["Property1"];
foundItem["Property2"] = sourceItem["Property2"];
foundItem["Property3"] = sourceItem["Property3"];
foundItem.Update();
}
Note that this piece of code is straight out of my head & untested ;-)
I'm working on an ASP.Net MVC application. My action is returning a view with a model that is an array of objects (a class with properties like Name, ID, IsViewable).
var model = #Model.ToJson(); // done via extension call
I want to observe this array, so whenever it changes I can update a table that has been bound to a template.
var viewModel = {
accounts = ko.observableArray(model)
}
This works just fine for adding and deleting elements from the array. However, I also want the template to update when a property in one of the accounts changes (ie, Name or ID).
On the KnockoutJS website, it says: Of course, you can make those properties observable if you wish, but that’s an independent choice. This is what I cannot figure out how to do.
I tried something like this with no avail:
var viewModel = {
accounts = ko.oservableArray([])
}
for(var i = 0; i < model.length; i++) {
ko.observableArray(model[i]);
viewModel.accounts.push(model[i]);
}
I can post the template and the table if it's needed.
You should look into the knockout.mapping plugin. I think it does everything you are looking to do.
I ended up getting this to work, so I thought I would share with anyone that might have having the same problem.
You need to wrap your array items in a JavaScript class. Then in the constructor, set each property to obserable:
var model = #Model.ToJson();
var viewModel = {
accounts = ko.observableArray(ko.utils.arrayMap(model, function(account) {
return new AccountWrapper(account);
}))
};
function AccountWrapper(account) {
this.Property1 = ko.observable(account.Propery1);
this.Property2 = ko.observable(account.Propery2);
this.Property3 = ko.observable(account.Propery3);
}
ko.applyBindings(viewModel);
And if you want to modify one of the items directly to see the change, you could do something like:
viewModel.accounts()[3].Name('My Name Changed');
And you can still get notified when items are added or remove:
viewModel.accounts.remove(viewModel.accounts()[4]);
Here's another approach that works and doesn't require the mapping plugin:
var model = #Model.ToJson();
var viewModel = {
accounts: ko.observableArray([]),
fromJS: function(js) {
for (var i = 0; i < js.length; i++) {
this.accounts.push({
Property1: ko.observable(js[i].Property1),
Property2: ko.observable(js[i].Property2),
Property3: ko.observable(js[i].Property3)
});
}
}
};
viewModel.fromJS(model);
ko.applyBindings(viewModel);
Hopefully this is a quick one!
I have an editable grid using 'clientSide' (local) data and I now wish to iterate all the rows in javascript and process/package the data myself before sending it to the server via a jQuery.ajax call.
The problem is that, unexpectedly (for me at least), using the following code only retrieves the rows for the currently visible grid page! How can I get ALL the rows in the grid (i.e. I have four pages of 10 records each and this code only returns the first 10 when I'm on page 1)? They must be present in the client somewhere because I can page around and edit the rows and the data is persisted without calling the server! :)
cacheCONF = [];
var rows= $('#myGrid').getRowData(); //<--Need to get ALL rows here!!!
var cacheRowID = 0;
for (var row in rows) {
if (rows[row].Action == 'Yes') {
cacheCONF.push({ RowID: rowID, System: rows[row].System, Action: rows[row].Action, Account: '-', Required: '-' });
rowID++;
}
}
Solution from Tony:
var mydata = $("#grid").jqGrid('getGridParam','data');
Had encountered a similar problem, below is what I ended up using
var data = $("#table-id").jqGrid('getGridParam', 'data');
for (var i = 0; i < data.length; i++) {
var f_name = data[i].FirstName;
var l_name = data[i].LastName;
// blah... blah..
}
Reference : http://www.trirand.com/blog/?page_id=393/help/jqgrid-getdata-only-returns-data-for-current-page/