Question

J'ai remarqué que je ne peux pas intégrer du javascript en ligne dans une page de composants WebPart SPO 2013, mais que je peux référencer une bibliothèque.

Je produis une table à partir de ce js.Si je mets des alertes, ce tableau s'affiche mais disparaît une fois le code exécuté.

Y a-t-il un moyen de faire cela sans jsrender ?

Je suppose que le tableau doit être dans le code de balisage intégré.

Il s'agit d'une fonction dans une bibliothèque jquery que je référence dans SiteAssets.

      $.ajax({
                url: 'https://xxxx/SearchValues/' ,
                data: 'SearchData=' + searchData,
                type: 'GET',
                dataType: 'json',
              //  jsonp: 'jsonp-callback',
                success: function (data) {
           //       alert("before write response");
                    WriteResponses(data);
                },
                error: function (x, y, z) {
                    alert(x + '\n' + y + '\n' + z);
                }
            }); 



            //Display results  in a table
            function WriteResponses(SitesList) {
                var strResult = "<table border='1px'><th width='20px'>ID</th><th width='100px'>TITLE</th><th 

width='100px'>FARM</th><th width='250px'>URL</th><th width='100px'>OWNERS</th><th width='200px'>ADMINISTRATORS</th>";
                $.each(SitesList, function (index, oSite) {
                    strResult += "<tr><td>" + oSite.LS_ID + "</td><td> " + oSite.LS_TITLE + "</td><td>" + oSite.LS_FARM + 

"</td><td>" + oSite.LS_SITE_URL + "</td><td>" + oSite.LS_OWNERS + "</td><td>" + oSite.LS_ADMINISTRATORS + "</td></tr>";
                });
                strResult += "</table>";
                $("#divResult").html(strResult);
alert(strResult);
            }

        }


    function WriteResponses(SitesList) {
        var strResult = "<table border='1px'><th width='20px'>ID</th><th width='100px'>TITLE</th><th width='100px'>FARM</th><th 

        width='250px'>URL</th><th width='100px'>OWNERS</th><th width='200px'>ADMINISTRATORS</th>";
        $.each(SitesList, function (index, oSite) {
        strResult += "<tr><td>" + oSite.LS_ID + "</td><td> " + oSite.LS_TITLE + "</td><td>" + oSite.LS_FARM + "</td><td>" + 

        oSite.LS_SITE_URL + "</td><td>" + oSite.LS_OWNERS + "</td><td>" + oSite.LS_ADMINISTRATORS + "</td></tr>";
        });
        strResult += "</table>";
        $("#divResult").html(strResult);
         }
Était-ce utile?

La solution

L'un des moyens d'obtenir le même résultat consiste à utiliser knockout.js.

Vous pouvez utiliser knockout.js et remplir les données à l'aide d'un foreach boucle dans un tableau observable.Et dans le composant WebPart, vous devez mapper le membre de données knock-out.

C'est bien car vous utiliserez du JavaScript et MVVM orientés objet.Et le code le sera un peu moins.Veuillez vérifier le code suivant :

Javascript :

var my = my || {}; //my namespace
my.ListItem = function () {
    this.LS_ID = ko.observable();
    this.LS_TITLE = ko.observable();
    this.LS_FARM = ko.observable();
    this.LS_SITE_URL = ko.observable();
    this.LS_OWNERS = ko.observable();
    this.LS_ADMINISTRATORS = ko.observable();
};
//My View Model
my.vm = function () {
    var
    Listdata = ko.observableArray([]),
    PopulateListdata = function(SitesList){
        $.each(SitesList, function (index, oSite) {
            Listdata.push(new my.ListItem()

            .LS_ID(oSite.LS_ID)

            .LS_TITLE(oSite.LS_TITLE)

            .LS_FARM(oSite.LS_FARM)

            .LS_SITE_URL(oSite.LS_SITE_URL)

            .LS_OWNERS(oSite.LS_OWNERS)

            .LS_ADMINISTRATORS(oSite.LS_ADMINISTRATORS)
            }
        };           
    return {
        Listdata: Listdata,
        PopulateListdata: PopulateListdata
    };
}();
my.vm.PopulateListdata(SitesList);
ko.applyBindings(my.vm);

HTML :

<script src='http://knockoutjs.com/downloads/knockout-3.1.0.js" type="text/javascript"></script> 
<div id='divResult'> 
<table border='1px'>
    <th width='20px'>ID</th> 
    <th width='100px'>TITLE</th> 
    <th width='100px'>FARM</th> 
    <th width='250px'>URL</th> 
    <th width='100px'>OWNERS</th> 
    <th width='200px'>ADMINISTRATORS</th> 
    <tbody data-bind='foreach: Listdata'> 
        <tr> 
            <td data-bind='text: LS_ID'></td>
            <td data-bind='text: LS_TITLE'></td> 
            <td data-bind='text: LS_FARM'></td> 
            <td data-bind='text: LS_SITE_URL'></td> 
            <td data-bind='text: LS_OWNERS'></td> 
            <td data-bind='text: LS_ADMINISTRATORS'></td> 
        </tr>
    </tbody> 
</table>
</div>

J'ai oublié de joindre la référence KO.Je l'ai mis à jour.Vérifiez s'il vous plaît.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top