2sxc | removing file path by trimming w/ Javascrip

2019-09-13 23:43发布

问题:

I have the following file path displaying:

And I want to display only file name 'Doc1' (minus path and extension).

I have tried unsucessfully the following and would appreciate any further light you could share as to what I am doing wrong...

    @functions{
    public static string SplitWord(string text, int length)
    {
        string str = text;
        int n = str.LastIndexOf(".");
        string str1=str.Substring(n,str.LastIndexOf("/"));
        str1=str1.Substring(1,str1.Length);
        return str1;
    }
}
<ol>
    @foreach (var q in AsDynamic(App.Data["CatFilter"]))
    {
    <li class="sc-element faq-set faq-setOne" data-tags="@String.Join(",", ((List<DynamicEntity>)q.Categories).Select(a => AsDynamic(a).EntityId))">
        @q.Toolbar @Edit.Toolbar(actions: "edit,new", contentType: "CatFilter")
        <a class="faq-question" style="cursor: pointer">
            @if(!String.IsNullOrEmpty(q.LinkText))
            {
               SplitWord(@q.LinkText,@q.LinkText.Length);
            } else {
               SplitWord(@q.Link,@q.Link.Length);
            }
        </a>
    </li>
    }
</ol>

I have also tried variations of the following within the IF condition but again no luck.

,,,,@:var str = q.Link;
,,,,@:var n = str.lastIndexOf(".");
,,,,@:var str1=str.Substring(n,str.lastIndexOf("/"))
,,,,@:str1=str1.Substring(1,str1.Length);

Thx,

回答1:

You're actually just using Substring the wrong way. You probably want

public static string SplitWord(string text, int length)
{
    int slash = text.LastIndexOf("/");
    int dot = text.LastIndexOf(".");
    return text.Substring(slash + 1, dot - slash);
}

Give it a try - might need another +1 or -1 on one of the values, but that should do the trick.



回答2:

You were right (with a minor tweak as you said)....

I also tried to integrate a date field so no matter what date a user selects, it will always display current date (of upload).

Is there a string to enter into the default value for datepicker field to display 'Today' as opposed to user having to click it in picker? (mixing questions again :) )

@functions{

    public static string SplitWord(string text,DateTime datetime)
    {

        int slash = text.LastIndexOf("/");

        int dot = text.LastIndexOf(".");

        dot--;

        var data = text.Substring(slash + 1, dot - slash);

        return data + " " + datetime.ToLongDateString();

    }

}
@{
    var all = AsDynamic(App.Data["CatFilter"]);
}
<ol>
    @foreach (var q in AsDynamic(App.Data["CatFilter"]))
    {
    <li class="sc-element faq-set faq-setOne" data-tags="@String.Join(",", ((List<DynamicEntity>)q.Categories).Select(a => AsDynamic(a).EntityId))">
        @q.Toolbar @Edit.Toolbar(actions: "edit,new", contentType: "CatFilter")
        <a class="faq-question" style="cursor: pointer">
            @if(!String.IsNullOrEmpty(q.LinkText))
            {

        @q.LinkText
            } else {
                @SplitWord(q.Link,q.Date);
            }
        </a>
    </li>
    }
</ol>

UPDATE

This is another template of yours that I applied the SplitWord function to but results in an error. I can't see any difference from code which works fine above.

This is the code I'm referring to:

@using ToSic.SexyContent

@* put all necessary link/script tags here *@
@RenderPage("_Shared - Assets.cshtml", new { parts = "styles,scripts,height"})

@{
    // get helper commands
    var Helpers = CreateInstance("_Helpers.cshtml");
}
    @functions{
        public static string SplitWord(string text)
        {
            int slash = text.LastIndexOf("/");
            var data = text.Substring(slash + 1);
            return data;
        }
    }
<div class="clearfix">
    <div class="co-container-outer">
        <div class="co-container-inner row co-navigation co-navigation-@(Dnn.Module.ModuleID) co-navigation-icon co-navigation-text">
            @foreach (var Content in AsDynamic(Data["Default"].List))

            {
                var linkInfo = Helpers.LinkInfos(Content.Link, Content.Window, Content.Icon);

                <div class="col col-xs-12 col-sm-6 col-md-4 sc-element">
                    @Edit.Toolbar(Content)
                    @if(linkInfo.Found)
                    {
                        @:<a class="co-link-box" title="@Content.Title" href="@Content.Link" target="@linkInfo.Window">
                    }
                    <div class="row">
                        <div class="col-xs-2 text-center">
                            <i class="co-icon text-primary co-icon fa @linkInfo.Icon" aria-hidden="true"></i>
                        </div>
                        <div class="col-xs-10">
                            <h3>@Content.Link</h3>
                            <div class="co-ul">
                                <a class="" title="@Content.Link" href="@Content.SubpageOne">@SplitWord(Content.SubpageOne)</a>
                            </div>
                            @if(!String.IsNullOrEmpty(Content.Link))
                            {
                                <a href="@Content.Link)"><span class="text-primary">
                                    <!--<i class="glyphicon glyphicon-chevron-right text-primary" aria-hidden="true"></i>&nbsp;-->
                                    @Content.LinkText</span></a>
                            }
                        </div>
                    </div>
                    @if(linkInfo.Found)
                    {
                        @:</a>
                    }
                </div>
            }
        </div>
    </div>
</div>

<script>
    /* Call syncHeightResponsive here - makes sure that sync height also works after ajax reload */
    $(".co-navigation-@(Dnn.Module.ModuleID) .col").syncHeightResponsive();
</script>