Friday, 9 March 2012

JavaScript CEWP Query and Set Data

JavaScript help from http://www.mredkj.com/tutorials/tablebasics3.html

Create a simple list call Books, and then try pasting this into a CEWP.


<table id="mytbl">
<thead>
<tr>
<th colspan="2">header...</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script type="text/javascript">




SP.SOD.executeOrDelayUntilScriptLoaded(example, 'SP.js');

function example() {
var ctx = new SP.ClientContext.get_current();
var oBooksList = ctx.get_web().get_lists().getByTitle('Books');
var caml = new SP.CamlQuery();
caml.set_viewXml("<View></View>");
this.allBooks = oBooksList.getItems(caml);
ctx.load(this.allBooks);

ctx.executeQueryAsync(
Function.createDelegate(this, this.onSucceededCallback),
null);
}

function onSucceededCallback(sender, args) {
var enumerator = this.allBooks.getEnumerator();
while (enumerator.moveNext()) {
var listItem = enumerator.get_current();
addRowInnerHTML("mytbl","ID: " + listItem.get_id() + ", Title: " +
listItem.get_item("Title"));
}
}

function addRowInnerHTML(tblId, txt)
{
var tblBody = document.getElementById(tblId).tBodies[0];
var newRow = tblBody.insertRow(-1);
var newCell0 = newRow.insertCell(0);
newCell0.innerHTML = '<input type="input" value="book" style="color: blue;" />';
var newCell1 = newRow.insertCell(1);
newCell1.innerHTML = txt;
}</script>

No comments:

Post a Comment