通过get函数

时间:2016-07-28 15:17:33

标签: javascript datatables

我正在尝试创建一个可以按

修改的对象

Datatables - Data

我使用以下代码创建我的对象

function projectData(projid,projdesc,descdet){
  this._projid = 'HTML1'
  this._projdesc = 'HTML2'
  for(var i=0;i<=7;i++){
    this['_day'+i] = 'HTML3';
  }
  this.projid = function(){
    return this._projid
  }
  this.projdesc = function(){
    return this._projdesc
  }
  this.day0 = function(){ // More of these for day (0-7)
    return this._day0
  }
}  

然后我使用下表初始化。 (prjData是一个New projectData对象的数组)

  var table = $('#table-ProjectHours').DataTable({
    data: prjData,
    "columns": [
        { data: 'projid',"visible": true},
        { data: 'projdesc',"width": "45%" },
        { data: 'day0',"orderDataType": "dom-text-numeric" },
        { data: 'day1',"orderDataType": "dom-text-numeric" },
        { data: 'day2',"orderDataType": "dom-text-numeric" },
        { data: 'day3',"orderDataType": "dom-text-numeric" },
        { data: 'day4',"orderDataType": "dom-text-numeric" },
        { data: 'day5',"orderDataType": "dom-text-numeric" },
        { data: 'day6',"orderDataType": "dom-text-numeric" },
        { data: 'day7',"orderDataType": "dom-text-numeric" }
    ]
  });

我通过

访问我的表格
var dto = $('#table-ProjectHours').DataTable().data();

我得到的对象如下所示:

Datatable object

我不明白为什么当我尝试做dto [0] .day0时我没有得到_day0 ---我只是得到了函数字符串。

我可以通过_day0访问数据,但似乎错了......

2 个答案:

答案 0 :(得分:0)

尝试rows()。data()https://datatables.net/reference/api/rows().data()

e.g。

var table = $('#example').DataTable();

var data = table
    .rows()
    .data();

alert( 'The table has '+data.length+' records' );

答案 1 :(得分:0)

要在jquery-datatables中设置数据,必须使用function getListUsingSPSerivces(){ //SPServices Example //The Web Service method we are calling, to read list items we use 'GetListItems' var method = "GetListItems"; //IGNORE //The display name of the list we are reading data from var url = "BASE_URL/sp/XXYY/_vti_bin/listdata.svc/" var list = "RFCExtract"; //Acquire GUID //var listGUID = $().SPServices.SPListNameFromUrl(); //console.log (listGUID); //list = listGUID; var fieldsToRead = "<ViewFields>" + "<FieldRef Name='RFC_x0020_Number' />" + "<FieldRef Name='Title' />" + "<FieldRef Name='Status' />" + "<FieldRef Name='Date' />" + "<FieldRef Name='Class' />" + "</ViewFields>"; //this is that wonderful CAML query I was talking about earlier. This simple query returns //ALL rows by saying "give me all the rows where the ID field is not equal to 0". I then //tell the query to sort the rows by the Title field. FYI: a blank query ALSO returns //all rows, but I like to use the below query because it helps me know that I MEANT to //return all the rows and didn't just forget to write a query :) var query = "<Query>" + "<Where>" + "<Neq>" + "<FieldRef Name='RFC_x0020_Number'/><Value Type='Number'>0</Value>" + "</Neq>" + "</Where>" + "<OrderBy>" + "<FieldRef Name='RFC_x0020_Number'/>" + "</OrderBy>" + "</Query>"; //Here is our SPServices Call where we pass in the variables that we set above console.log("SPSerivces example"); //console.log(method); console.log(url); console.log(list); var rfcList = $().SPServices.SPGetListItemsJson({ webURL: url, async: false, //if you set this to true, you may get faster performance, but your order may not be accurate. listName: list, CAMLViewFields: fieldsToRead, CAMLQuery: query, debug: true }); for (var key in rfcList) { console.log("key " + key + " has value " + rfcList[key]); } }; <?xml version="1.0" encoding="utf-8" standalone="yes"?> <feed xml:base="<**BASE_URL**>/_vti_bin/listdata.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <title type="text">RFCExtract</title> <id><**BASE_URL**>/_vti_bin/listdata.svc/RFCExtract</id> <updated>2018-11-29T12:43:44Z</updated> <link rel="self" title="RFCExtract" href="RFCExtract" /> <entry m:etag="W/&quot;1&quot;"> <id><**BASE_URL**>/_vti_bin/listdata.svc/RFCExtract(1)</id> <title type="text">Test</title> <updated>2018-11-28T15:07:57-05:00</updated> <author> <name /> </author> <link rel="edit" title="RFCExtractItem" href="RFCExtract(1)" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="RFCExtract(1)/CreatedBy" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="RFCExtract(1)/ModifiedBy" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="RFCExtract(1)/Attachments" /> <category term="Microsoft.SharePoint.DataService.RFCExtractItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <content type="application/xml"> <m:properties> <d:ContentTypeID>0x010018E49BFAAA76C74C8D9E319D13220A60</d:ContentTypeID> <d:Title>Test</d:Title> <d:Class m:type="Edm.Double">1</d:Class> <d:Status>Closed</d:Status> <d:Date m:type="Edm.DateTime">2010-11-01T00:00:00</d:Date> <d:RFCNumber m:type="Edm.Double">1</d:RFCNumber> <d:Id m:type="Edm.Int32">1</d:Id> <d:ContentType>Item</d:ContentType> <d:Modified m:type="Edm.DateTime">2018-11-28T15:07:57</d:Modified> <d:Created m:type="Edm.DateTime">2018-11-28T15:07:57</d:Created> <d:CreatedById m:type="Edm.Int32">9929</d:CreatedById> <d:ModifiedById m:type="Edm.Int32">9929</d:ModifiedById> <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion> <d:Version>1.0</d:Version> <d:Path>/sp/CCSB/Lists/RFC Extract</d:Path> </m:properties> </content> </entry> <entry m:etag="W/&quot;1&quot;"> <id><**BASE_URL**>/_vti_bin/listdata.svc/RFCExtract(2)</id> <title type="text">Test Record 2</title> <updated>2018-11-29T07:42:19-05:00</updated> <author> <name /> </author> <link rel="edit" title="RFCExtractItem" href="RFCExtract(2)" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="RFCExtract(2)/CreatedBy" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="RFCExtract(2)/ModifiedBy" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="RFCExtract(2)/Attachments" /> <category term="Microsoft.SharePoint.DataService.RFCExtractItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <content type="application/xml"> <m:properties> <d:ContentTypeID>0x010018E49BFAAA76C74C8D9E319D13220A60</d:ContentTypeID> <d:Title>Test Record 2</d:Title> <d:Class m:type="Edm.Double">2</d:Class> <d:Status>Pending</d:Status> <d:Date m:type="Edm.DateTime">2018-11-02T00:00:00</d:Date> <d:RFCNumber m:type="Edm.Double">2</d:RFCNumber> <d:Id m:type="Edm.Int32">2</d:Id> <d:ContentType>Item</d:ContentType> <d:Modified m:type="Edm.DateTime">2018-11-29T07:42:19</d:Modified> <d:Created m:type="Edm.DateTime">2018-11-29T07:42:19</d:Created> <d:CreatedById m:type="Edm.Int32">9929</d:CreatedById> <d:ModifiedById m:type="Edm.Int32">9929</d:ModifiedById> <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion> <d:Version>1.0</d:Version> <d:Path>/sp/CCSB/Lists/RFC Extract</d:Path> </m:properties> </content> </entry> </feed>

您还可以使用.row(index).data(obj),如果您编写了方法的一部分,则可以像.cell(selector).data(obj)那样设置数据。您必须在更改数据后调用row(index).day0()才能使其正确显示。

在这种特殊情况下,。row(index).day0(thing_to_set)必须称为row(index).invalidate,因为它是一种方法。