Domanda

I am using below code with reference to this thread: Yes/No (check box) - who updated field on the custom list item

Basically when a user checks on a yes/no column, it shows/prints the user name who did that in a respective column. It works great and does the job. For example in the above thread, Updated By column shows 'app'.All i need to add is the time and date when user checks a yes/no column along with the user name. So it should look like: Updated by: app 8/15/2018 4:01PM Below is the code, can someone please help me add timestamp config to the below code.:

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var fieldValue=$("input[title='YesNoColumn']").is(":checked");
    var updatedby=$("input[title='Updated by']").val();
    $("input[title='Updated by']").attr("readonly","true").css('background-color','#F6F6F6');
    $("input[title='YesNoColumn']").change(function(){
        if($(this).is(":checked")!=fieldValue){
            $("input[title='Updated by']").val(getUserName());
        }else{
            $("input[title='Updated by']").val(updatedby);
        }
    });
});
function getUserName(){
    var username;
    var userid = _spPageContextInfo.userId;
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        async: false,
        headers: { "ACCEPT": "application/json;odata=verbose" },
        success: function (data) {
            username=data.d.Title;
        },
        error: function () {            
        }
    });
    return username;
}
</script>
È stato utile?

Soluzione

Sample script for your reference.

enter image description here

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var fieldValue = $("input[title='YesNoColumn']").is(":checked");
        var updatedby = $("input[title='Updated by']").val();
        $("input[title='Updated by']").attr("readonly", "true").css('background-color', '#F6F6F6');
        $("input[title='YesNoColumn']").change(function () {
            if ($(this).is(":checked") != fieldValue) {
                var currentDateTime =new Date();
                var fmtDateTime = currentDateTime.format('MMM/dd/yyyy hh:ss tt');
                $("input[title='Updated by']").val(getUserName()+' '+ fmtDateTime);
            } else {
                $("input[title='Updated by']").val(updatedby);
            }
        });
    });
    function getUserName() {
        var username;
        var userid = _spPageContextInfo.userId;
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
        // execute AJAX request
        $.ajax({
            url: requestUri,
            type: "GET",
            async: false,
            headers: { "ACCEPT": "application/json;odata=verbose" },
            success: function (data) {
                username = data.d.Title;
            },
            error: function () {
            }
        });
        return username;
    }
</script>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top