Pregunta

Me di cuenta de que no puedo incrustar en línea JavaScript en una página web de SPO 2013, pero puede hacer referencia a una biblioteca.

Estoy generando una mesa de ese JS.Si pongo alertas que se muestra la tabla, pero luego desaparece una vez que el código procede.

¿Alguna forma de hacer esto sin JSRender?

Sospechoso que la tabla debe estar en el código de marcado incrustado.

Esta es una función en una biblioteca de jQuery. Referencia en Sitessets.

      $.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);
         }

¿Fue útil?

Solución

Una de las formas de lograr el mismo resultado es usar knockout.js.

Puede usar KnockOut.js y rellenar los datos utilizando un bucle de foreach en una matriz observable.Y en la parte web, debe asignar el Miembro de Data Knockout.

Esto es bueno porque usará JavaScript orientado a objetos y MVVM.Y el código será poco menos.Por favor, compruebe el siguiente código:

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>

Olvidé adjuntar la refrena KO.Lo he actualizado.Por favor, compruebe.

Licenciado bajo: CC-BY-SA con atribución
scroll top