如何在Sharepoint上为这个淘汰javascript添加额外的列表功能?

时间:2015-01-30 12:07:35

标签: javascript jquery sharepoint knockout.js

所以我之前从未真正使用过Knockout所以我对此非常陌生。

预先存在的淘汰意味着当在下拉列表中单击某个值时,可以在下一个下拉列表中选择某个值列表。我现在在第一个列表中添加第二个值,它会更改第二个下拉列表中的值。这些下拉列值都是从2个不同的共享点列表中导入的。

$.when(
        $.getJSON('../_vti_bin/listdata.svc/ApplicationList?$select=Id,ApplicationName,ApplicationDescription'),
        $.getJSON('../_vti_bin/listdata.svc/ApplicationRoleList?$select=ApplicationID,RoleID,RoleNameValue,Description,PrivilegedValue') 

    ).then(function(apps, roles){
        // both ajax calls are finished now
        var rolesMap = {}; // {AppID1: [role1, role2], AppID2: [role3, role4]}
        if (roles[0].d && roles[0].d.results) {
            var r = roles[0].d.results;
            for (var i = 0; i < r.length; i++) {
                if (!rolesMap[r[i].ApplicationID]) {
                    rolesMap[r[i].ApplicationID] = [];
                }
                rolesMap[r[i].ApplicationID].push(r[i]);
            }
        }
        if (apps[0].d && apps[0].d.results) {
            var a = apps[0].d.results;
            for (var i = 0; i < a.length; i++) {
                var app = {
                    ApplicationID: a[i].Id,
                    ApplicationName: a[i].ApplicationName,
                    ApplicationDescription: a[i].ApplicationDescription,
                    roles: rolesMap[a[i].Id]
                };
                model.applications.push(app);
                model.applicationMap[app.ApplicationID] = app;
            }
        }


        else if(apps[1].d && apps[0].d.results) {
            var a = apps[0].d.results;
            for (var i = 0; i < a.length; i++) {
                var app = {
                    ApplicationID: a[i].Id,
                    ApplicationName: a[i].ApplicationName,
                    ApplicationDescription: a[i].ApplicationDescription,
                    roles: rolesMap[a[i].Id]
                };
                model.applications.push(app);
                model.applicationMap[app.ApplicationID] = app;
            }
        }


    });

ASPX:

<td class="ms-vb">
            Application: 
            <select data-bind="value: $data.selectedApp, options: $parent.applications, optionsText: 'ApplicationName', optionsCaption: 'Choose an Application'" style="width: 32px" name="Application list" id="dataBox">
            </select>
            <img src="../SiteAssets/helpbutton.png" class="helpbutton" onmouseover="displayAppHelpText(this);"/>
            &nbsp; Role: <select data-bind="value: selectedRole, options: roles, optionsText: 'RoleNameValue', optionsCaption: 'Choose a Role'"></select>
            <button data-bind="click: addSelectedRole" id="add_button">Add</button> 
            <img src="../SiteAssets/helpbutton.png" class="helpbutton" onmouseover="displayRoleHelpText(this);"/>

            <span class="hidden">
                <select class="appnames" data-bind="value: $data.selectedApp, options: $parent.applications, optionsText: 'ApplicationName', optionsCaption: 'App'"></select>
                <select class="appdescriptions" data-bind="value: $data.selectedApp, options: $parent.applications, optionsText: 'ApplicationDescription', optionsCaption: ''"></select>
                <select class="rolenames" data-bind="value: selectedRole, options: roles, optionsText: 'RoleNameValue', optionsCaption: 'Please select an Application first'"></select>
                <select class="roledescriptions" data-bind="value: selectedRole, options: roles, optionsText: 'Description', optionsCaption: ''"></select>
            </span>

所以,当我点击一个应用程序时,我想要更改滚动,但是我遇到了问题。感谢

1 个答案:

答案 0 :(得分:0)

要处理选择更改,您可以使用以下两种方法之一:

  1. 订阅observable $ data.selectedApp应该是什么。 [这种方式更像'Knockout-ish']

  2. 或使用淘汰赛event binding

  3. var viewModel = {
        choices: [
          {label:"one", code: 1}, 
          {label:"two", code: 2},
          {label:"three", code: 3}
        ],
        selectedChoice: ko.observable(2) ,
        selectionChanged: function(event) {
             alert("The event binding way. New value is " + $( "#eventBindingWay option:selected" ).val());  
        } 
    };
    
    viewModel.selectedChoice.subscribe(function(newValue) {
       alert("The observable subscribtion way. New value is " + newValue); 
    });
    
    
    ko.applyBindings(viewModel);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
    
    Subscribtion way: <select id="subscribtionWay" data-bind="options: choices, value: selectedChoice, optionsText:'label', optionsValue:'code',"></select>
    
    <br /><br />
    
    Event binding way: <select id="eventBindingWay" data-bind="event:{ change: 
     selectionChanged }">
        <option value="A">A label</option>
        <option value="B">B label</option>
        <option value="C">C label</option>
    </select>