Sharepoint JS 獲取Choice到頁面Html的select選項。



function TestChoice() {

    var clientContext = new SP.ClientContext();
    var targetList = clientContext.get_web().get_lists().getByTitle("TestChoice");//列表名
    fieldCollection = targetList.get_fields();
    //clientContext.load(fieldCollection, 'Include(Title,hidden)');
    clientContext.load(fieldCollection);
    clientContext.executeQueryAsync(Function.createDelegate(this, onSucceeded), Function.createDelegate(this, onFailed));
    function onSucceeded() {
        var message = "The following fields are available in the Announcements list:\n\n";
        //var fields = '';
        var fieldEnumerator = fieldCollection.getEnumerator();
        while (fieldEnumerator.moveNext()) {
            //fields += listEnumerator.get_current().get_title() + "; ";

            var oField = fieldEnumerator.get_current();
            var title = oField.get_title();
            var internalName = oField.get_internalName();

            //var myhidden = oField.get_hidden();
            var myfieldtype = oField.get_fieldTypeKind();
            var myfieldhidden = oField.get_hidden();
            var myreadonly = oField.get_readOnlyField();
            if (myfieldtype == 6) {
                //alert(title + " Type:" + myfieldtype + " ReadOnly:" + myreadonly + "Hidden:"+ myfieldhidden);
                var html;
                html = title + "<select id= "+ internalName + "'/></select>";

                $("#Test").append(html);
                getChoices();

            }
        }
    }
    function onFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

}
function getChoices() {
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
   

    // Get task list
    var testList = web.get_lists().getByTitle("TestChoice");//列表名

    // Get Priority field (choice field)
    var choiceField = context.castTo(testList.get_fields().getByInternalNameOrTitle("TestChoice"),//欄名
                                       SP.FieldChoice);
    context.load(choiceField);
    // Call server
    context.executeQueryAsync(Function.createDelegate(this, onSuccessMethod),
                              Function.createDelegate(this, onFailureMethod));

    function onSuccessMethod(sender, args) {
        // Get string arry of possible choices (but NOT fill-in choices)
        var choices = choiceField.get_choices();
        alert(choices[0]);//得到的是一個數組。那麼我們就可以這麼寫
        for (var i = 0; i<choices.length; i++){
            var optionHtml = "<option value='" + choices[i] + "'>" + choices[i] + "</option>" ;
            $('#Test select').append(optionHtml);
        }

        //var myoption = choices.join(",");
        //var selectOptions = new Array;
        //selectOptions = myoption.split(",");
        //for (var i = 0; i < selectOptions.length; i++) {

        //var optionHtml = "<option value='" + selectOptions[i] + "'>" + selectOptions[i] + "</option>";
        //$('#Test select').append(optionHtml);
        //}
    }

    function onFailureMethod(sender, args) {
        alert("oh oh!");
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章