javascript - JQuery 1.4.4 and Datatable 1.7 not working -
i needed datatables. might missing here:-
below html
<div id="cfcphonedirectory"> <table id="dynamicphonedirectory"> <thead> <tr><th>phone book</th><th>last name</th><th>first name</th><th>number</th></tr> </thead> </table>
and script
var rawxmldata = "<phonebooks><phonebook><name>phonebook1</name><type>global</type><contacts><contact><lname>elis</lname><fname>jason</fname><number>1234567890</number></contact><contact><lname>randell</lname><fname>mark</fname><number>7895896710</number></contact><contact><lname>flower</lname><fname>andy</fname><number>8796024589</number></contact></contacts></phonebook><phonebook><name>phonebook2</name><type>team</type><contacts><contact><lname>cullinan</lname><fname>david</fname><number>6027051573</number></contact><contact><lname>webber</lname><fname>mark</fname><number>4842051298</number></contact><contact><lname>mitchell</lname><fname>randy</fname><number>7012841562</number></contact></contacts></phonebook></phonebooks>"; var t = $('#dynamicphonedirectory').datatable(); var xmldata = json.stringify(xmltojson(rawxmldata)); ($(xmldata)).find("phonebook").each(function () { var phonebooktype = $(this).find("type").text() $(this).find("contact").each(function () { t.row.add([phonebooktype, $(this).find("lname").text(), $(this).find("fname").text(), $(this).find("number").text()]).draw(); }); }); // changes xml json function xmltojson(xml) { // create return object var obj = {}; if (xml.nodetype == 1) { // element // attributes if (xml.attributes.length > 0) { obj["@attributes"] = {}; (var j = 0; j < xml.attributes.length; j++) { var attribute = xml.attributes.item(j); obj["@attributes"][attribute.nodename] = attribute.nodevalue; } } } else if (xml.nodetype == 3) { // text obj = xml.nodevalue; } // children if (xml.haschildnodes()) { for(var = 0; < xml.childnodes.length; i++) { var item = xml.childnodes.item(i); var nodename = item.nodename; if (typeof(obj[nodename]) == "undefined") { obj[nodename] = xmltojson(item); } else { if (typeof(obj[nodename].push) == "undefined") { var old = obj[nodename]; obj[nodename] = []; obj[nodename].push(old); } obj[nodename].push(xmltojson(item)); } } } return obj; };
i using jquery version of 1.4.4 , datatable version of 1.7.5?
any appreciated.
thanks, hash
i gave debugging xml parsing - cryptic. there no need porting xml json @ all. made instead :
var table = $('#dynamicphonedirectory').datatable(); var parser = new domparser(), xmldoc = parser.parsefromstring(rawxmldata,"text/xml"), phonebook = xmldoc.queryselector('phonebooks'), phonebooks = phonebook.queryselectorall('phonebook'); (var i=0;i<phonebooks.length;i++) { var firstname, lastname, number, contacts, phonebookname = phonebooks[i].queryselector('name').textcontent, phonebookcontacts = phonebooks[i].queryselector('contacts'), contacts = phonebookcontacts.queryselectorall('contact'); (var c=0;c<contacts.length;c++) { lastname = contacts[c].queryselector('lname').textcontent; firstname = contacts[c].queryselector('fname').textcontent; number = contacts[c].queryselector('number').textcontent; //add contact datatables table.fnadddata([phonebookname, lastname, firstname, number]); } }
this works datatables 1.7.5 -> http://jsfiddle.net/nsmqg4n2/ used jquery 1.6.4, lowest possible in jsfiddle, not inflict on above code after all. jquery datatables 1.7.5 need jquery 1.3.x or better.
to retrieve data (for example when table clicked) use api method fngetdata()
, example (jquery 1.6.4, datatables 1.7.5) :
$("#dynamicphonedirectory").delegate('tbody tr', 'click', function() { var data = table.fngetdata(this); alert('number dial : '+data[3]); });
forked fiddle -> http://jsfiddle.net/bu87ke7a/
Comments
Post a Comment