如何使用knockoutjs映射插件对数据绑定到表?(How to sort data bind to table using knockoutjs mapping plugin?)
我能够将数据绑定到表并使用列标题进行排序。
但是,我需要能够编辑数据,以便更新并反映在相应的表格单元格中。 为此,我需要使数据对象成为
observable
。使用文本绑定而不是像这样绑定:
<tbody data-bind="foreach: countries"> <tr> <td data-bind="text: name"></td> <td data-bind="text: capital"></td> <td data-bind="text: population"></td> </tr> </tbody>
我需要使用
value
或textInput
绑定进行绑定。 像这样:<tbody data-bind="foreach: countries"> <tr> <td><input data-bind="value: name" /></td> <td><input data-bind="value: capital" /></td> <td><input data-bind="value: population" /></td> </tr> </tbody>
而不是这样做:
$.getJSON('https://restcountries.eu/rest/v1/all', function(data){ self.countries(data); });
我这样做:
$.getJSON('https://restcountries.eu/rest/v1/all', function(data){ var mappedData = ko.mapping.fromJS(data); var array = mappedData(); self.countries(array); });
如果我尝试使用插件映射数据,则排序不起作用。
如何将数据映射为可观察数据并仍具有对其进行排序的能力?
这是一个有效的JSFiddle示例 :注意:我删除了映射,因为它破坏了代码。
I am able to bind data to a table and sort using the column headers.
However, I need to be able to edit the data so that it is updated and reflected in the corresponding table cell. To do this I need to make the data object an
observable
.Instead of binding like this, using text binding:
<tbody data-bind="foreach: countries"> <tr> <td data-bind="text: name"></td> <td data-bind="text: capital"></td> <td data-bind="text: population"></td> </tr> </tbody>
I need to bind using the
value
ortextInput
binding. Like so:<tbody data-bind="foreach: countries"> <tr> <td><input data-bind="value: name" /></td> <td><input data-bind="value: capital" /></td> <td><input data-bind="value: population" /></td> </tr> </tbody>
And instead of doing this:
$.getJSON('https://restcountries.eu/rest/v1/all', function(data){ self.countries(data); });
I do this:
$.getJSON('https://restcountries.eu/rest/v1/all', function(data){ var mappedData = ko.mapping.fromJS(data); var array = mappedData(); self.countries(array); });
If I attempt to mapped the data using the plugin, the sort does not work.
How do I mapped the data as observables and still have the capability to sort them?
Here is a working JSFiddle example: Note: I removed the mapping since it breaks the code.
原文:https://stackoverflow.com/questions/35398105
相关问答
更多-
而且我根本无法控制序列化过程。 是的你是。 为类提供一个toJSON方法,该方法返回具有预期属性的对象,它将被JSON.stringify识别 。 反序列化过程是相同的,JSON.parse将创建一个普通对象而不是类型化对象。 因为JSON确实代表普通对象,所以这很自然:-)但是,你可以将一个reviver函数传递给JSON.parse ,它可以操作返回的对象并为类实例交换它们。 为方便起见,将一个(静态) fromJSON函数添加到您可以在reviver中使用的类中。 有没有符合我需求的JavaScrip ...
-
当嵌套对象为空时,JSON反序列化会抛出异常(JSON deserialization throws exception when nested object is empty)[2023-10-31]
只需使用Newtonsoft的Json.NET 。 它工作正常。 这是LINQPad的简短片段: void Main() { JsonConvert.DeserializeObject(@"{ property: ""apweiojfwoeif"", nested: { prop1: ""wpeifwjefoiwj"", prop2: ""9ghps89e4aupw3r"" } }").Du ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如果Clerk 是一名 Worker ,那么你不应该有任何理由不能完全按照你在构造函数示例中展示的那样做。 Worker w = (new Gson()).fromJson( jsonData, Clerk.class ) ; 我不确定你为什么要这么强烈地向上投这么多,因为任何想要Worker东西应该能够直接带Clerk ...... If Clerk is a Worker, then there shouldn't be any reason you can't just do exactly wha ...
-
不,相反,你会遇到很大的问题。 在Joshua Bloch的Effective Java ,Item 74中,他注意到明智地实现了Serializable,因为它减少了一个类的实现一旦被释放就改变的灵活性。 他特别指出:“如果你没有努力设计一个自定义的可序列化表单,而只是接受默认值,那么序列化表单将永远与这个类的原始内部表示形式相关联。” No, on the contrary, you'll run into great problems. In Joshua Bloch's Effective Java ...
-
您正在寻找ExpandoObject 。 ExpandoObject是C#4中引入的一个动态对象。 在此处找到示例实现: public static IEnumerable
GetExpandoFromXml(string file, string descendantid) { var expandoFromXml = new List (); var doc = XDocument.Load(file); var nodes = doc.Ro ... -
JSON反序列化仅在实时构建中抛出循环引用(JSON deserialization throws circular reference ONLY in live build)[2022-09-18]
那个[ScriptIgnore]属性[ScriptIgnore] 。 此外,完全可以确定的是,您的所有URL路径(包括AJAX代码中)都正确解析为应用程序根目录。 当其中一些是错误的时候,这是从开发到生产过程中的一个臭名昭着的问题来源。 这听起来不一定是主要问题,但我对您的应用程序的设计一无所知。 这绝对值得一看。 That's great that the [ScriptIgnore] attribute is helping. Also, something to be completely sure ... -
看一下ValidatingObjectInputStream 。 基本上,您将允许反序列化的类列入白名单(您应该根据所引入的信息了解这些类)。 然后验证器将检查元数据到序列化数据并拒绝任何不在白名单中的类。 Take a look at the ValidatingObjectInputStream. Basically you whitelist the classes that you will allow to be deserialized (you should know these based ...
-
MVC5 - 如何为动作参数类型定义反序列化行为?(MVC5 - How do I define deserialization behavior for action parameter type?)[2022-06-25]
您必须创建自己的模型绑定器。 Guid示例: public class GuidModelBinder : DefaultModelBinder { public override object BindModel( ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext.ModelType == ... -
注释中提到了一个选项:强制将数据迁移到XML,然后删除旧类。 另一种选择是尝试在不使用Java Serialization机制的情况下解析它。 有一些工具可以解析java序列化数据,例如https://github.com/gagern/nodeJavaDeserialization我在java中找不到任何库,但是,如果你只有一个类数据要解析,解析它的任务不会看起来很可怕。 有关格式的文档可用: http : //docs.oracle.com/javase/6/docs/platform/serializ ...